Linux脚本计数器无法正常工作

时间:2014-11-20 09:56:27

标签: linux ubuntu counter

我正在做一个我必须编写脚本的作业,当计数器达到一定数量时,它似乎没有添加到计数器以显示线索这里是下面的代码

#!/bin/bash
#This is a game that is played by the user having to guess a number that the computer thinks of.
counter=0
# functions
function clueOne() {
 echo “The number is over 50”
 return
}
function clueTwo() {
 echo “The number is smaller than 100 and is devided by 7, 10 times”
 return
}


function selectANumber() {
 dialog --backtitle "Number Game" --title "Number Game" --infobox "Thinking of a number..." 
10 50 ; clear
}
selectANumber
# game starts here
while [ $counter  -le 10 ];  do
 read -p ”Guess my number human!”
 if [ $counter -eq 5 ]; 
 then 
 clueOne
 counter=$((counter+1))
 elif [ $counter -eq 9 ]; 
 then
 clueTwo
 counter=$((counter+1))
 elif [ $REPLY = 70 ]; 
 echo “Yes that is my number!”; 
 exit 1
 echo “Wrong number”
 counter=$((counter+1))
fi
done
echo “Sorry you ran out of attempts”

1 个答案:

答案 0 :(得分:0)

因为如果你的反击我们0说你不会进入任何其他的梯子。因此,您可能希望在完成之前递增计数器变量,例如在while循环中:

counter=$((counter+1))

并从if else梯形图中删除增量操作。