SoFunction
Updated on 2025-03-10

Shell scripts implement numbers guessing game

This article shares the specific code of Shell to implement the number guessing game for your reference. The specific content is as follows

The guess here can be written casually, such as A, B, etc.

vim 

Edit script

#!/bin/bash
init()
{
echo "Pretty guessing game!!! "
}
game()
{
number=$[$RANDOM%100]
while :
do
read -p "Enter the number you want to guess: " n1
n2=`echo $n1 | sed 's/[0-9]//g'`
if [ ! -z $n2 ]
  then
    echo "You're not entering a number."
    continue
fi
if [ $n1 == $number ]
  then
    echo "You guessed it."
    read -p "Do you want to play again? (yes/no)" an1
    while true
        do
            case $an1 in
            yes)
            game
            break
            ;;
            no)
            break
            ;;
            *)
            exit
            esac
    done
    break
#!/bin/bash
init()
{
echo "                   "
echo "Pretty guessing game!!! "
}
game()
{
number=$[$RANDOM%100]
while :
do
read -p "Enter the number you want to guess: " n1
n2=`echo $n1 | sed 's/[0-9]//g'`
if [ ! -z $n2 ]
  then
    echo "You're not entering a number."
    continue
fi
if [ $n1 == $number ]
  then
    echo "You guessed it."
    read -p "Do you want to play again? (yes/no)" an1
    while true
        do
            case $an1 in
            yes)
            game
            break
            ;;
            no)
            break
            ;;
            *)
            exit
            esac
    done
    break
            break
            ;;
            no)
            break
            ;;
            *)
            exit
            esac
    done
    break
  elif [ $n1 -gt $number ]
  then
    echo "I guess it's big."
    continue
  else
    echo "I guess it's too small."
    continue
  fi
done
}
 
 
while true
do
init
echo "1: Start the game"
echo "0: Exit the game"
read -p "Please enter 0 or 1:" NUM
 
case $NUM in
1)
    game
    break
    ;;
0)
    break
    ;;
esac

done

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.