用户输入问题在一个简单的bash脚本中的乘法问题

时间:2014-03-26 22:38:35

标签: bash shell math

简单的程序来计算方块,然后检查用户是否正确。

它抛出错误说" guess.sh:[4:未找到" 4是用户输入的答案。我知道我错过了一些非常简单的东西......

echo "Please enter a number "

read number

SQUARE=$((number*number))

echo "please enter the answer"

read answer

if ["$SQUARE" -eq "$answer"]
then
    echo "well done"
else
    echo "sorry wrong answer"
fi

1 个答案:

答案 0 :(得分:4)

[之后添加一个空格,因为它是一个命令

ls -l /usr/bin/[
-rwxr-xr-x 1 root root 35248 Jan 26  2013 /usr/bin/[

(当然也在]之前)

if [ "$SQUARE" -eq "$answer" ]
then
    echo "well done"
else
    echo "sorry wrong answer"
fi

read -p "Enter a number: " number
square=$[number**2]
read -p "Enter his square: " second
if ((second==square)) ;then
    echo well done
  else
    echo wrong square
fi

也可以工作,但它基于所谓的 bashisms