2008/7/24 吴雨龙 <wylong.0...@gmail.com>:
> 2008/7/23 Niu Tao <niutao0...@gmail.com>:
>> ---------- Forwarded message ----------
>> From: 刘周平 <sanweiday...@gmail.com>
>> Date: 2008/7/23
>> Subject: Re: 这个小Shell脚本运行时有问题!!
>> To: linuxerfamily@googlegroups.com
>> 2008/7/23 Niu Tao <niutao0...@gmail.com>:
>> > 2008/7/23 吴雨龙 <WYlong.0...@gmail.com>:
>> > > 我在学习Shell脚本时将书上一个Shell脚本敲进电脑,可是运行时出现了问题。不知道该怎么修改,请高手指教:
>> 程序主要问题在第六行,按理说$?表示上一条指令的返回结果,所以当选择yes后应该返回0,no后应该返回1,可是不管做什么选择,在信息框中打印出
>> > > 来一看都是%66,所以导致了下面的判断语句无效,程序无法继续运行。
>> > > #!/bin/sh
>> > > # Ask some questions and collect the answer
>> > > dialog --title "Questionnaire" --msgbox "Welcome to my simple survey"
>> > > 9 18
>> > > dialog --title "Confirm" --yesno "Are you welling to take part?" 9 18
> 按大家说的都改过以后还是不行,现在作主要的问题在于下面的两个判断语句根本就不进,在上一句的选择做出后程序就自动结束了。大家谁知道这究竟是怎么回事?
现在终于完全把这个问题搞清楚了,原来是因为if [ $? != 0 ];then 这句在执行完后就修改了$?的值,致使后面在执行elif [ $? =
0 ];then 时$?这是代表的是上面的if语句的执行结果,已不是上一个dialog语句的执行结果。
解决的方法很简单,就是在dialog --title "Confirm" --yesno "Are you welling to take part?"
9 18这句执行完后将$?的值保存起来,用a=$?就搞定了,然后将后面的判断语句中的$?全部改成$a便可。具体的完整代码如下:
#!/bin/sh
# Ask some questions and collect the answer
dialog --title "Questionnaire" --msgbox "Welcome to my simple survey" 9 18
dialog --title "Confirm" --yesno "Are you welling to take part?" 9 18
a=$?
if [ $a != 0 ];then
dialog --infobox "Thank you anyway" 5 20
elif [ $a = 0 ];then
dialog --title "Questionnaire" --inputbox "Please enter your name" 9
30 2>_1.txt
Q_NAME=$(cat _1.txt)
dialog --menu "$Q_NAME,what music do you like best?" 15 30 4 1
"Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>_1.txt
Q_MUSIC=$(cat _1.txt)
else
exit 1
fi
if [ "$Q_MUSIC" = "1" ];then
dialog --msgbox "Good choice!" 12 25
fi
exit 0
>> > > if[ $? != 0 ];then
>> > > dialog --infobox "Thank you anyway" 5 20
>> > > sleep 2
>> > > dialog --clear
>> > > exit 0
>> > > elif[ $? = 0 ];then
>> > > dialog --title "Questionnaire" --inputbox "Please enter your
>> name" 9
>> > > 30 2>_1.txt
>> > > Q_NAME=$(cat _1.txt)
>> > > dialog --menu "$Q_NAME,what music do you like best?" 15 30 4 1
>> > > "Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>_1.txt
>> > > Q_MUSIC=$(cat _1.txt)
>> > > if [ "$Q_MUSIC" = "1" ];then
>> > > dialog --msgbox "Good choice!" 12 25
>> > > fi
>> > > sleep 5
>> > > dialog --clear
>> > > fi
>> > > exit 0
>> > 我今天下午试了一下,原来是if后面忘加空格了,加上空格,那一行应该没问题了,不过上面的if,then,eilf结构不对,我把代码改为: 1
>> #!/bin/sh
>> > 2 # Ask some questions and collect the answer
>> > 3
>> > 4 dialog --title "Questionnaire" --msgbox "Welcome to my simple
>> survey" 9 18
>> > 5 dialog --title "Confirm" --yesno "Are you welling to take part?" 9
>> 18
>> > 6 echo $i
>> > 7 if [ $? != 0 ];then
>> > 8 dialog --infobox "Thank you anyway" 5 20
>> > 9 sleep 2
>> > 10 dialog --clear
>> > 11 # exit 0
>> > 12 fi
>> > 13 if [ $? = 0 ];then #原代码中if后面忘加空格
>> > 14 dialog --title "Questionnaire" --inputbox
>> "Please enter your name" 9 30 2>_1.txt
>> > 15 Q_NAME=$(cat _1.txt)
>> > 16 dialog --menu "$Q_NAME,what music do you
>> like best?" 15 30 4 1 "Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>_1.txt
>> > 17 Q_MUSIC=$(cat _1.txt)
>> > 18 fi
>> > 19 if [ "$Q_MUSIC" = "1" ];then
>> > 20 dialog --msgbox "Good choice!" 12 25
>> > 21 fi
>> > 22 sleep 5
>> > 23 dialog --clear
>> > 24
>> > 25 exit 0
>> > 在试一下,应该没问题。
>> 顺便再说几句,shell中的"空格"还挺重要的,有时是必须要加的,但我们经常会忽视它,所以以后在学shell时要多加留心"空格"。
>> > 第六行改为:
>> > if [ $? -ne 0];then
>> > 下面的elif也改为"
>> > elif [ $? -ge 0];then
>> > 但之后还有问题,你在看看吧。
>> 很好,不过我还是要在多说两句:
>> 1、如果你要贴出代码,那最好是"纯代码",而不要含有像上面那样的多余的行号,
>> 发之前最好整理好再发。
>> 2、程序一定要有格式,是别人很容易就可以看明白。
>> 下面是我对你上面发的作的一些改进:
>> #!/bin/sh
>> # Ask some questions and collect the answer
>> dialog --title "Questionnaire" --msgbox "Welcome to my simple survey" 9 18
>> dialog --title "Confirm" --yesno "Are you welling to take part?" 9 18
>> echo $i
>> if [ $? != 0 ];then
>> dialog --infobox "Thank you anyway" 5 20
>> fi
>> if [ $? = 0 ];then #原代码中if后面忘加空格
>> dialog --title "Questionnaire" --inputbox "Please enter your name"
>> 9
>> 30 2>_1.txt
>> Q_NAME=$(cat _1.txt)
>> dialog --menu "$Q_NAME,what music do you like best?" 15 30 4 1
>> "Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>_1.txt
>> Q_MUSIC=$(cat _1.txt)
>> fi
>> if [ "$Q_MUSIC" = "1" ];then
>> dialog --msgbox "Good choice!" 12 25
>> fi
>> dialog --clear
>> clear
>> exit 0
>> --
>> My Bolg: http://niutao.cublog.cn