当前位置: 技术问答>linux和unix
谁能帮我看一下这个Shell脚本
来源: 互联网 发布时间:2014-12-23
本文导语: #!/bin/sh echo "Is it morning? Please answer yes or no" read timeofday if test "$timeofday"="yes" then echo "Good morning" elif test "$timeofday"="no" then echo "Good afternoon" else echo "Sorry, $timeofday not recognized. Enter yes of no" exit 1 fi exit...
#!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
if test "$timeofday"="yes"
then
echo "Good morning"
elif test "$timeofday"="no"
then
echo "Good afternoon"
else
echo "Sorry, $timeofday not recognized. Enter yes of no"
exit 1
fi
exit 0
在执行的时候,不管输入yes还是no,都输出的是Good morning
这是怎么回事呢?
还有Red Hat Linux 7.3中有没有[]这个比较函数呢?
echo "Is it morning? Please answer yes or no"
read timeofday
if test "$timeofday"="yes"
then
echo "Good morning"
elif test "$timeofday"="no"
then
echo "Good afternoon"
else
echo "Sorry, $timeofday not recognized. Enter yes of no"
exit 1
fi
exit 0
在执行的时候,不管输入yes还是no,都输出的是Good morning
这是怎么回事呢?
还有Red Hat Linux 7.3中有没有[]这个比较函数呢?
|
可以用 [ $timeofday="yes" ] 来代替,注意 [ ] 号前后都有空格,他与test命令的作用一样。
还有,不能说linux是不是支持[]表示,是bash支持,早期的bsh并不支持。
还有,不能说linux是不是支持[]表示,是bash支持,早期的bsh并不支持。
|
你在这里面少了分号,所以不行,不符合语法,但是编译器将它当做一个字符串,所以当然是真的了,也理所当然的显示good morning
#!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
if test $timeofday = "yes"; then
echo "Good morning"
elif test $timeofday = "no"; then
echo "Good afternoon"
else
echo "Sorry, $timeofday not recognized. Enter yes of no";
exit 1;
fi
exit 0
#!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
if test $timeofday = "yes"; then
echo "Good morning"
elif test $timeofday = "no"; then
echo "Good afternoon"
else
echo "Sorry, $timeofday not recognized. Enter yes of no";
exit 1;
fi
exit 0