SoFunction
Updated on 2025-03-10

Shell script implements the game of guessing numbers

Write a number guessing game using shell script

A recent guessing game written in shell, including 4 different difficulties, the script is as follows:

#Author: p_Xiao Wang
echo 'Welcome to the number guessing game!  '
while true
do
 read -p 'Start/Exit (y/n):' x #Prompt the user to enter if [ -z $x ] #Judge user input is empty then
 echo 'Can't be empty' #hint echo ''
 continue
 elif [ $x = y ] #Judge user input 'y' then
 echo ''
 echo 'The game is about to begin'
 echo ''
 while true
 do
 sleep 1s #Delay 1s echo 'Please select the difficulty' #Difficulty selection   echo ' 1.Simple'
   echo ' 2.generally'
  echo ' 3.difficulty'
 echo ' 4.hell'
 echo ' 5.return'
 echo ''
   read -p 'Please enter the corresponding serial number:' y #Prompt the user to enter if [ -z $y ] #Judge user input is empty then
 echo 'Can't be empty' #hint echo ''
 continue
 elif [ $y = 1 ] #Judge the user input ‘1’ is simple then 
 while true
 do
  num=$[RANDOM%100+1] # Randomly generate numbers within 100  echo 'Guess a number of 1-100'
  n=0 #Define variables  while true
  do
  read -p 'Please enter:' a #Prompt the user to enter  let n++ #Count times  if [ -z $a ] #Judge user input is empty  then
  echo 'The input cannot be empty!  ' #hint  continue
  elif [ -n "`echo $a | sed 's#[0-9]##g'`" ] #Judge user input non-digits  then
  echo 'Please enter a number between 1-100!  ' #hint  continue
  elif [ $a -lt 1 ] || [ $a -gt 100 ] #Judge the number entered by the user is between 1-100  then
  echo 'Please enter a number between 1-100!  ' #hint  continue
  elif [ $a -gt $num ] #Judge the number entered by the user is greater than the randomly generated number  then
  echo 'Guess it big,Guess again!  ' #hint  continue
  elif [ $a -lt $num ] #Judge the number entered by the user is less than the randomly generated number  then
  echo 'Guess it's small, guess again!  ' #hint  continue
  else
  echo 'Congratulations, you guessed it right, the correct number is'$num #Prompt the user to guess correctly  echo 'Guessed' $n 'Second-rate' #Show the number of guesses  echo ''
  sleep 1s #Delay 1s  echo 'Will there be another game?' #Tip whether to play again  while true
  do
  read -p 'y/n:' i #Prompt the user to enter  if [ -z $i ] #Judge user input is empty  then
   echo 'Can't be empty' #hint   echo ''
   continue
    elif [ $i = y ] #Judge user input 'y'  then
   break 2
  elif [ $i = n ] #Judge user input 'n'  then
   break 3
  else
   echo 'The input was wrong' #hint   echo ''
   continue
  fi
  done
  fi
  done
 done

 elif [ $y = 2 ] #Judge user input '2' then
 while true
 do
  num=$[RANDOM%1000+1] # Randomly generate numbers within 1000  echo 'Guess a number of 1-1000' #hint  n=0 #Define variables  while true
  do
  read -p 'Please enter:' a #Prompt the user to enter  let n++ #Number of times  if [ -z $a ] #Judge user input  then
  echo 'The input cannot be empty!  ' #hint  continue
  elif [ -n "`echo $a | sed 's#[0-9]##g'`" ] #Judge user input non-digits  then
  echo 'Please enter a number between 1-1000!  ' #hint  continue
  elif [ $a -lt 1 ] || [ $a -gt 1000 ] #Judge the user input is between 1-1000  then
  echo 'Please enter a number between 1-1000!  ' #hint  continue
  elif [ $a -gt $num ]
  then
  echo 'Guess it big,Guess again!  ' #hint  continue
  elif [ $a -lt $num ]
  then
  echo 'Guess it's small, guess again!  ' #hint  continue
  else
  echo 'Congratulations, you guessed it right, the correct number is'$num #Prompt the user to guess correctly  echo 'Guessed' $n 'Second-rate' #Show the number of guesses  echo ''
  sleep 1s #Delay 1s  echo 'Will there be another game?' #Tip whether to continue playing  while true
  do
  read -p 'y/n:' i #Prompt the user to enter  if [ -z $i ] #Judge user input is empty  then
   echo 'Can't be empty' #hint   echo ''
   continue
    elif [ $i = y ] #Judge user input 'y'  then
   break 2
  elif [ $i = n ] #Judge user input 'n'  then
   break 3
  else
   echo 'The input was wrong' #hint   echo ''
   continue
  fi
  done
  fi
  done
 done

 elif [ $y = 3 ] #Judge user input difficulty then
 while true
 do
  num=$[RANDOM%10000+1] # Randomly generate numbers within 10,000  echo 'Guess a number of 1-10000' #hint  n=0 #Define variables  while true
  do
  read -p 'Please enter:' a #Prompt the user to enter  let n++ #Number of times  if [ -z $a ] #Judge user input is empty  then
  echo 'The input cannot be empty!  ' #hint  continue
  elif [ -n "`echo $a | sed 's#[0-9]##g'`" ] #Judge user input non-digits  then
  echo 'Please enter a number between 1-10000!  ' #hint  continue
  elif [ $a -lt 1 ] || [ $a -gt 10000 ] #Determine whether the user input is between 1-10000  then
  echo 'Please enter a number between 1-10000!  ' #hint  continue
  elif [ $a -gt $num ] #Judge the number input is greater than the randomly generated number  then
  echo 'Guess it big,Guess again!  ' #hint  continue
  elif [ $a -lt $num ] #Judge the input number is less than the randomly generated number  then
  echo 'Guess it's small, guess again!  ' #hint  continue
  else
  echo 'Congratulations, you guessed it right, the correct number is'$num #Prompt the user to guess correctly  echo 'Guessed' $n 'Second-rate' #hint  echo ''
  sleep 1s #Delay 1s  echo 'Will there be another game?' #Tip whether to continue playing  while true
  do
  read -p 'y/n:' i #Prompt the user to enter  if [ -z $i ] #Judge user input is empty  then
   echo 'Can't be empty' #hint   echo ''
   continue
    elif [ $i = y ] #Judge user input 'y'  then
   break 2
  elif [ $i = n ] #Judge user input 'n'  then
   break 3
  else
   echo 'The input was wrong' #hint   echo ''
   continue
  fi
  done
  fi
  done
 done

 elif [ $y = 4 ] #Judge user input '4' hell then
 while true
 do
  num=$[RANDOM%100000+1] # Randomly generate numbers within 100,000  echo 'Guess a number of 1-100000' #hint  n=0 #Define variables  while true
  do
  read -p 'Please enter:' a #Prompt the user to enter  let n++ #Number of times  if [ -z $a ] #Judge user input is empty  then
  echo 'The input cannot be empty!  ' #hint  continue
  elif [ -n "`echo $a | sed 's#[0-9]##g'`" ] #Judge user input non-digits  then
  echo 'Please enter a number between 1-100,000!  ' #hint  continue
  elif [ $a -lt 1 ] || [ $a -gt 100000 ] #Judge user input between 1-100000  then
  echo 'Please enter a number between 1-100,000!  ' #hint  continue
  elif [ $a -gt $num ] #Judge the number input is greater than the randomly generated number  then
  echo 'Guess it big,Guess again!  ' #hint  continue
  elif [ $a -lt $num ] #Judge the input number is less than the randomly generated number  then
  echo 'Guess it's small, guess again!  ' #hint  continue
  else
  echo 'Congratulations, you guessed it right, the correct number is'$num #Prompt the user to guess correctly  echo 'Guessed' $n 'Second-rate' #Show the number of guesses  echo ''
  sleep 1s #Delay 1s  echo 'Will there be another game?' #Tip whether to continue playing  while true
  do
  read -p 'y/n:' i #Prompt the user to enter  if [ -z $i ] #Judge user input is empty  then
   echo 'Can't be empty' #hint    echo ''
   continue 
    elif [ $i = y ] #Judge user input 'y'  then
   break 2
  elif [ $i = n ] #Judge user input 'n'  then
   break 3
  else
   echo 'The input was wrong' #hint   echo ''
   continue
  fi
  done
  fi
  done
 done

 elif [ $y = 5 ] #Judge the user's choice to return then
 echo ''
 break #return else #Judge user input other echo 'The input was wrong' #hint echo ''
 continue
 fi 
 done
 elif [ $x = n ] #Judge the user to enter ‘n’ to exit then
 echo 'The game is about to exit' #hint echo ''
 sleep 1s
 echo 'Exit successfully!' #hint echo ''
 exit 0 #End script else #Judge user input other echo 'The input was wrong' #hint echo ''
 continue
 fi
done

Note: I have tested it, no bug was detected

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.