Google Groups Home
Help | Sign in
这个小Shell脚本运行时有问题!!
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
吴雨龙  
View profile
 More options Jul 22, 11:40 pm
From: 吴雨龙 <WYlong.0...@gmail.com>
Date: Tue, 22 Jul 2008 20:40:01 -0700 (PDT)
Local: Tues, Jul 22 2008 11:40 pm
Subject: 这个小Shell脚本运行时有问题!!
我在学习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
        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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Niu Tao  
View profile
 More options Jul 23, 3:57 am
From: "Niu Tao" <niutao0...@gmail.com>
Date: Wed, 23 Jul 2008 15:57:41 +0800
Local: Wed, Jul 23 2008 3:57 am
Subject: Re: 这个小Shell脚本运行时有问题!!
2008/7/23 吴雨龙 <WYlong.0...@gmail.com>:

第六行改为:
if [ $? -ne 0];then
下面的elif也改为"
elif [ $? -ge 0];then
但之后还有问题,你在看看吧。

--
My Bolg: http://niutao.cublog.cn


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
刘周平  
View profile
 More options Jul 23, 6:37 am
From: "刘周平" <sanweiday...@gmail.com>
Date: Wed, 23 Jul 2008 18:37:45 +0800
Local: Wed, Jul 23 2008 6:37 am
Subject: Re: 这个小Shell脚本运行时有问题!!

2008/7/23 Niu Tao <niutao0...@gmail.com>:

我今天下午试了一下,原来是if后面忘加空格了,加上空格,那一行应该没问题了,不过上面的if,then,eilf结构不对,我把代码改为:  1

顺便再说几句,shell中的"空格"还挺重要的,有时是必须要加的,但我们经常会忽视它,所以以后在学shell时要多加留心"空格"。


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Niu Tao  
View profile
 More options Jul 23, 7:19 am
From: "Niu Tao" <niutao0...@gmail.com>
Date: Wed, 23 Jul 2008 19:19:17 +0800
Local: Wed, Jul 23 2008 7:19 am
Subject: 这个小Shell脚本运行时有问题!!


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
吴雨龙  
View profile
 More options Jul 24, 8:44 am
From: "吴雨龙" <wylong.0...@gmail.com>
Date: Thu, 24 Jul 2008 20:44:18 +0800
Local: Thurs, Jul 24 2008 8:44 am
Subject: Re: 这个小Shell脚本运行时有问题!!

2008/7/23 Niu Tao <niutao0...@gmail.com>:

按大家说的都改过以后还是不行,现在作主要的问题在于下面的两个判断语句根本就不进,在上一句的选择做出后程序就自动结束了。大家谁知道这究竟是怎么回事?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
李磊  
View profile
 More options Jul 25, 2:39 am
From: "李磊" <lilei1...@gmail.com>
Date: Fri, 25 Jul 2008 14:39:17 +0800
Local: Fri, Jul 25 2008 2:39 am
Subject: Re: 这个小Shell脚本运行时有问题!!
#!/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 [ $? = 1 ];then
       dialog --infobox "Thank you anyway" 5 20
       sleep 2
       dialog --clear
       exit 0
else
       dialog --title "Questionnaire" --inputbox "Please enter your
name" 9 30 2>temp.txt
         Q_NAME=$(cat temp.txt)
        result=echo "Q_NAME"  
       dialog --menu "$Q_NAME,what music do you like best?" 15 30 4 1
"Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>temp.txt
       Q_MUSIC=$(cat temp.txt)
       if [ "$Q_MUSIC" = "1" ];then
               dialog --msgbox "Good choice!" 12 25
        else
                dialog --msgbox "Bad choice!" 12 25
       fi
        result=`echo $Q_NAME $Q_MUSIC`
        echo $result >>result.txt
       dialog --clear
fi
        rm temp.txt
exit 0


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
吴雨龙  
View profile
 More options Jul 25, 5:31 am
From: "吴雨龙" <wylong.0...@gmail.com>
Date: Fri, 25 Jul 2008 17:31:43 +0800
Local: Fri, Jul 25 2008 5:31 am
Subject: Re: 这个小Shell脚本运行时有问题!!

2008/7/24 吴雨龙 <wylong.0...@gmail.com>:

现在终于完全把这个问题搞清楚了,原来是因为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


    Reply