對(duì)shell不熟,偶爾會(huì)現(xiàn)一些我無(wú)法理解的現(xiàn)象。此時(shí)該進(jìn)行debug了,可選的方法有:
a. echo變量的值
b. shell –x
此外,
Remember that the shell spends a lot of its life substituting text.(
http://linuxcommand.org/wss0100.php)例如,對(duì)于下面的程序:
hex108@Gentoo ~ $ cat test.sh
#!/bin/sh
var=
if [ $var = "y" ] ;then
echo "yes"
fi
if語(yǔ)句里的var變量經(jīng)替換后變?yōu)?if [ = "y" ],些時(shí)當(dāng)然會(huì)出錯(cuò)。
hex108@Gentoo ~ $ ./test.sh
./test.sh: line 3: [: =: unary operator expected
ps:現(xiàn)在寫腳本的時(shí)候傾向于使用perl,而較少使用shell ,因?yàn)閷?duì)于經(jīng)常使用的腳本,可能會(huì)經(jīng)常需要對(duì)它不停地進(jìn)行改進(jìn),慢慢的,程序越來(lái)越大,該考慮重構(gòu)了, 此時(shí)才會(huì)發(fā)現(xiàn)perl(python等“真正的”腳本語(yǔ)言)比shell相對(duì)來(lái)說(shuō)更好重構(gòu)。
posted on 2011-04-23 00:23
hex108 閱讀(429)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
Program