用script 讀檔然後做變數 - Linux

Table of Contents

PATH=$PATH
export PATH

cat ttest.dat | awk '{total = $1 + $2 + $3 + $4;
printf "%d %d %d %d %d\n", $1, $2, $3, $4, total; system("echo " total)}'

echo "$total"

if ["$total" -le "20" ]; then
pwd

fi

exit 0

ttest.dat內容
10 10 10 10
11 11 11 11
5 5 5 1

為什麼awk執行完後 我的total裡面就什麼都沒有了。
要怎麼修改我的pwd才會執行到?
謝謝

--

All Comments

Thomas avatarThomas2012-04-09
awk的變數,shell變數,環境變數三者各有scope
Valerie avatarValerie2012-04-12
total沒有被初始化所以echo不出來
而你想要的結果只有在awk中有效
Caroline avatarCaroline2012-04-16
那我要怎麼修改呢?
Suhail Hany avatarSuhail Hany2012-04-20
比較簡單的方法是將if那部份移到awk中去做
Ethan avatarEthan2012-04-23
你這個程式其實可以完全在 awk 中完成~
Skylar Davis avatarSkylar Davis2012-04-27
ok thanks