如何跟蹤用戶進行了多少次猜測并列印一條訊息告訴他們他們輸了?5 次猜測后退出。
num=$(( $RANDOM % 100 1 ))
while [ "$input" -ne "$num" ]; do
if [ "$input" -gt "$num" ]; then
echo "The number is too high."
read input
elif [ "$input" -lt "$num" ]; then
echo "The number is too low."
read input
fi
done
echo "Great, you picked the right number."
uj5u.com熱心網友回復:
請您嘗試以下方法:
#!/bin/bash
num=$(( $RANDOM % 100 1 ))
for (( i = 0; i < 5; i )); do
read -p "Enter a number: " input
if (( input == num )); then
echo "Great, you picked the right number."
break
elif (( input > num )); then
echo "The number is too high."
elif (( input < num )); then
echo "The number is too low."
fi
done
順便說一句,我通過將亂數生成器修改為$RANDOM % 10 1但仍然不容易:) 來測驗腳本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/440725.html
標籤:重击
上一篇:為什么我的腳本在第一次操作后停止
