这是一个简单的学校项目。控制台一直声明[5:命令未找到
#!/bin/bash
num=28
echo "Guess a number a number between 1 and 100"
read guess
while [$guess -ne $num]
do
if [$guess -lt $num]
then
echo "number is higher"
elif [$guess -gt $num]
then
echo "number is lower"
else
echo "Correct! The number is $number"
fi
done
答案 0 :(得分:2)
while [$guess -ne $num]
通过首先扩展参数来解释:
while [5 -ne 28]
然后导致命令[5
被执行,并传递参数-ne
和28]
您想执行命令[
,因此您需要编写:
while [ $guess -ne $num ]
(请注意[
和]
周围的空格。如果没有空格,则字符会成为另一个字的一部分。)
与if
和elif
后面的陈述相同。
答案 1 :(得分:0)
这样做:
#!/bin/bash
num=28
echo "Guess a number a number between 1 and 100"
read guess
while [ $guess -ne $num ]; do
if [ $guess -lt $num ]
then
echo "number is higher"
read guess
elif [ $guess -gt $num ]
then
echo "number is lower"
read guess
fi
done
echo "Correct! The number is $number"