dimanche 26 janvier 2020

How to implement a spell checker in random number generator in a Bash script

Just finished scripting a number generator game that requires a user to guess a number between 1 and 100, I was curious if there is a way to make the script only accept numbers in between the 1-100 number range and reject any letter characters? I tried using

typeset -i

But i'm not sure if I am fully utilizing it or missing something, here is the rest of the code

#!/bin/bash
num=$((RANDOM%100))
typeset -i attempts=0
until [[ $guess == $num ]]
do
    echo -n "Enter your guessing number: "
    read -r guess
    if (( guess < num ));
    then echo "Guess Higher..."
    elif (( guess > num ));
    then echo "Guess Lower..."
    fi
(( attempts++ )) 
done
printf "Congradulations! it took $attempts guesses!\n"



Aucun commentaire:

Enregistrer un commentaire