如何在bash上编写一个名为guess_number和RANDOM if语句的脚本?

时间:2014-09-20 03:01:32

标签: linux bash

需要帮助编写脚本就像这样想要在0到10之间猜出数字。

num=$(($RANDO%11) 

read -p "Enter an integer between 0 and 10" input

if [$RANDOM%=4]; then

echo "correct" 

else

echo "you got it wrong guess again

fi

exit 0

2 个答案:

答案 0 :(得分:0)

为此,您可以尝试这种方法:

#!/bin/bash
read -p "Enter an integer between 0 and 10:  " input
if [ "$input" -ge "0" ] && [ "$input" -le "10" ]; then
     echo "correct"
else 
    echo "wrong"
fi

答案 1 :(得分:0)

#!/bin/bash

rand=$(( ( RANDOM % 10 )  + 1 ))

while : ; do
     read -p "Enter an integer between 0 and 10 :  " input
    [ "$input" -eq "$rand" ] && echo -e "\nCorrect\n" && break
    echo -e "\n  Try again...\n"
done

<强>输出:

$ bash rand.sh

Enter an integer between 0 and 10 :  8

  Try again...

Enter an integer between 0 and 10 :  9

  Try again...

Enter an integer between 0 and 10 :  1

  Try again...

Enter an integer between 0 and 10 :  2

  Try again...

Enter an integer between 0 and 10 :  3

  Try again...

Enter an integer between 0 and 10 :  4

Correct