如何保持脚本设置运行?

如何保持脚本设置运行?

我正在编写一个名为 pickanumber.sh 的脚本。我正在设置脚本,所以我会要求用户选择一个数字。如果他们输入的数字不是“8”,则脚本将继续运行。当用户选择“8”以外的数字时,我无法使脚本继续运行。我应该使用 for 循环还是 while 循环?这是我到目前为止的脚本。

rand_num=0
let "rand_num = $RANDOM % 10 + 1"
echo $rand_num
echo -n "pick a number from 1 - 10! "
read pickanumber
if [ "$pickanumber" = "8" ]; then
   echo "GOOD JOB"
else
   read $pickanumber
fi          

答案1

使用while循环,或until.

until [ "$pickanumber" = "8" ]; do
    echo -n "pick a number from 1 - 10! "
    read pickanumber
done
echo "GOOD JOB"

答案2

#!/bin/bash
let "rand_num = $RANDOM % 10 +1"
pickanumber=0
printf "Pick a number on this range [1-10]:"
read pickanumber

while [ true ]; do
 if [ $pickanumber -eq $rand_num ];then
  echo "Good Job!"
  exit
 else
  printf "Wrong, try again?"
  read pickanumber
 fi
done

相关内容