【发布时间】:2019-05-04 17:40:46
【问题描述】:
该程序旨在创建一个随机数,并检查它是否小于、大于或等于用户在计数小于 3 时输入的数字。
#!/bin/sh
ranNum=$(($RANDOM % (2 - 1)))
ranNum=$((1 + $ranNum))
c=1
echo "The entity with the greatest number wins"
while [ $c -lt 3 ]
do
echo "Enter a number"
read usrIn
if ["$usrIn" -gt "$ranNum"]
then
echo "You won"
((c++))
if ["$usrIn" -lt "$ranNum"]
then
echo "You lost"
((c++))
else
echo "Its a tie"
((c++))
break
fi
done
当我在 shell 中运行代码时,返回 2 个错误:
第 24 行:意外标记 done'
line 24:done' 附近的语法错误
我不确定我的代码语法有什么问题,也不知道从哪里开始。
【问题讨论】:
-
两个
ifs 但一个fi是开始调试的好地方。 -
看看ShellCheck,它就是为这种事情而生的
-
没想到,谢谢
-
除了测试命令中的
ifvselif问题和间距之外,$(($RANDOM % (2 - 1)))将始终扩展为“0”(因为 2-1 是 1,而任何 %1 都是0).