|
蓝森林 http://www.lslnet.com 2006年6月26日 11:18
[求助]简单的shell问题
今天刚学shell,用的是freebsd的bash,一段很简单的书上的代码,看上去没错,可是执行起来总是不对。
代码如下:
#!/usr/local/bin/bash
WORD="tele"
echo "what's your name?"
read NAME
echo
echo "Helllo,$NAME,please guess the word.Your"
echo "choices are iguana,tele,slurp"
read GUESS
if [ $GUESS=$WORD ]
then
echo "You are correct!"
else
echo "Incorrect!"
fi
可是我不管输入什么词,最后显示的总是You are correct!
请问哪里错了啊? |
[求助]简单的shell问题
$GUESS=$WORD 改为: $GUESS -eq $WORD |
[求助]简单的shell问题
if [ $GUESS == $WORD ]; #注意空格 |
[求助]简单的shell问题
多谢二位,见笑了 |
[求助]简单的shell问题
如果不用变量,而直接和字符比较,如:$GUESS == "a",好象就执行错误了吧,$GUESS="a"也错了,书上好象又写错了 |
[求助]简单的shell问题
注意空格 |
[求助]简单的shell问题
[code]
[ "$GUESS" = "$WORD" ][/code] |
[求助]简单的shell问题
$GUESS = "a",加了空格了,也不对呀。
运行完总是出现这句话:./sh5: line 9: [: iguana: integer expression expected |
[求助]简单的shell问题
在命令行下试试这个 :oops:
[code]
read;[[ $REPLY == "a" ]]&&echo ok||echo error[/code] |
[求助]简单的shell问题
error |
[求助]简单的shell问题
read;[[ $REPLY="a" ]]&&echo ok||echo error
结果ok
可是写进脚本去,还是出现那行字,也不能显示正确的结果 |
[求助]简单的shell问题
-->
sorry,没办法了,我在命令行和脚本中都可以得到正确结果@_@ |
[求助]简单的shell问题
#!/usr/local/bin/bash
WORD="tele"
echo "what's your name?"
read NAME
echo
echo "Helllo,$NAME,please guess the word.Your"
echo "choices are iguana,tele,slurp"
read GUESS
if [ $GUESS=$WORD ]
then
echo "You are correct!"
elif [ $GUEST="iguana" ]
then
echo "it's not iguana"
elif [ $GUEST="slurp" ]
then
echo "it's not slurp"
else
echo "$GUESS is not even in the list!"
fi
现在的问题就是我无论输入什么,包括tele,iguana,slurp,或其他随意的字母组合,程序都显示it's not iguana,并且在这句话之前有类似这样的:./sh5: line 9: [: iguana: integer expression expected
东西 |
[求助]简单的shell问题
| [求助]简单的shell问题
等号两边加空格也出错,也有那句怪信息,而且不管输入什么都显示最后echo的那句了 |
[求助]简单的shell问题
[color=red]if [ $GUESS == $WORD ][/color]
then
echo "You are correct!"
[color=red]elif [ $GUEST == "iguana" ][/color]
then
echo "it's not iguana"
[color=red]elif [ $GUEST == "slurp" ]
[/color]then
echo "it's not slurp"
else
echo "$GUESS is not even in the list!"
fi |
[求助]简单的shell问题
#!/usr/local/bin/bash
WORD="tele"
echo "what's your name?"
read NAME
echo
echo "Helllo,$NAME,please guess the word.Your"
echo "choices are iguana,tele,slurp"
[color=red]read GUESS[/color]
[color=red]if [ $GUESS = "$WORD" ][/color]
then
echo "You are correct!"
[color=red]elif [ $GUESS = "iguana" ][/color]then
echo "it's not iguana"
[color=red]elif [ $GUESS = "slurp" ][/color]then
echo "it's not slurp"
else
echo "$GUESS is not even in the list!"
fi
我想他想要的应该是上面的结果吧
注:
1、=左右的空格
2、elif后面的变量应该都是GUESS而不应该是GUEST[/color] |
[求助]简单的shell问题
#!/usr/local/bin/bash
WORD="tele"
echo "what's your name?"
read NAME
echo
echo "Helllo,$NAME,please guess the word.Your"
echo "choices are iguana,tele,slurp"
[color=red]read GUESS[/color]
[color=red]if [ $GUESS = "$WORD" ][/color]
then
echo "You are correct!"
[color=red]elif [ $GUESS = "iguana" ][/color]then
echo "it's not iguana"
[color=red]elif [ $GUESS = "slurp" ][/color]then
echo "it's not slurp"
else
echo "$GUESS is not even in the list!"
fi
我想他想要的应该是上面的结果吧
注:
1、=左右的空格
2、elif后面的变量应该都是GUESS而不应该是GUEST[/color] |
[求助]简单的shell问题
sorry
加代码加的把回车给弄没了,
上面的红色字体后面有then的要换行,或者then前面加; |
[求助]简单的shell问题
#!/usr/local/bin/bash
WORD="tele"
echo "what's your name?"
read NAME
echo
echo "Helllo,$NAME,please guess the word.Your"
echo "choices are iguana,tele,slurp"
[color=red]read GUESS[/color]
[color=red]if [ $GUESS = "$WORD" ][/color]
then
echo "You are correct!"
[color=red]elif [ $GUESS = "iguana" ][/color]then
echo "it's not iguana"
[color=red]elif [ $GUESS = "slurp" ][/color]then
echo "it's not slurp"
else
echo "$GUESS is not even in the list!"
fi
我想他想要的应该是上面的结果吧
注:
1、=左右的空格
2、elif后面的变量应该都是GUESS而不应该是GUEST[/color] |
| |