预期的整数表达式

时间:2015-01-23 20:57:24

标签: linux shell scripting

正在编写一个脚本,提示用户猜测1到100之间的数字。必须满足一些条件才能让用户输入有效的猜测。数字不能高于100且低于1.数字必须是有效整数而不是字母!

我不知道如何制作它以便脚本检查用户是否输入了数字或字母,然后忽略并让他们在输入信件时再试一次。请保持你的答案简单,如新的脚本。

#!/bin/bash

clear
let num=$((($RANDOM%100)+1))
let counter=0
echo "Guess a number between 1 and 100: "
read -r input

while [ $input != $num ] ; do
        ((counter++))
if [ $input -lt 1 ] || [ $input -gt 100 ] ; then
        echo "Invalid Guess, Please try again..."
        read -r input
elif [ $input -gt $num ] ; then
        echo "Your guess is higher then the number..."
        echo "Try again..."
        read -r input
elif [ $input -lt $num ] ; then
        echo "Your guess is lower then the number..."
        echo "Try again..."
        read -r input
fi
done

echo "Your guess was correct!"
echo "It took you $counter guesses to get the right answer!"

0 个答案:

没有答案