May 23, 2021 That's what Linux should learn
A while conditional loop statement is a statement that allows a script to repeat a command based on certain conditions, and its loop structure often does not determine the number of final executions before execution, which is completely different from the targeted, wide-ranging usage scenario in the for loop statement. T he while loop statement determines whether to proceed with the command by determining whether the condition test is true or false, and if the condition is true, continues to execute, ending the loop if the condition is false. T he syntax format of the while statement is shown in Figure 4-21.
Figure 4-21 the syntax format of the while loop statement
Next, use a multi-branch if condition test statement with a while conditional loop statement to write a script to guess the size of the value Guess.sh. T he script uses the $RANDOM variable to adjust a random number (ranging from 0 to 32767), takes the random number to 1000, and uses the expr command to obtain its results, and then compares the value entered by the user with the read command. T his judgment statement is divided into three cases, which are to determine whether the value entered by the user is equal to, greater than, or less than the value obtained using the expr command. Currently, these are not the key points, and our current concern is that the conditional test in the while conditional loop statement is always true, so the judgment statement will execute indefinitely until the value entered by the user is equal to the value obtained by the expr command, the two are equal before running the exit 0 command and terminating the execution of the script.
[root@linuxprobe ~]# vim Guess.sh
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品实际价格为0-999之间,猜猜看是多少?"
while true
do
read -p "请输入您猜测的价格数目:" INT
let TIMES++
if [ $INT -eq $PRICE ] ; then
echo "恭喜您答对了,实际价格是 $PRICE"
echo "您总共猜测了 $TIMES 次"
exit 0
elif [ $INT -gt $PRICE ] ; then
echo "太高了!"
else
echo "太低了!"
fi
done
In Guess.sh script, we added some interactive information to make the user more interactive with the system. A nd whenever you loop to the let TIMES command, you have the values in the TIMES variable plus 1 to count how many times the loop totals. This lets the user know how many times in total they guessed before guessing the right price.
The root@linuxprobe price of Guess.sh goods is between 0-999, guess what? P lease enter the number of prices you have guessed: 500 is too low! P lease enter the number of prices you have guessed: 800 is too high! P lease enter the number of prices you have guessed: 650 is too low! P lease enter the number of prices you have guessed: 720 is too high! P lease enter the number of prices you have guessed: 690 is too low! P lease enter the number of prices you have guessed: 700 is too high! P lease enter the number of prices you have guessed: 695 is too high! P lease enter the number of prices you have guessed: 692 is too high! Please enter the number of prices you have guessed: 691 Congratulations on your correct answer, the actual price is 691 You have guessed a total of 9 times