mercredi 26 avril 2017

If statement Issues with alternate endings in bash

trying to write a script to play a guessing game. The script should generate a number and request a guess from the user. The script will then compare the number the user entered with the generated number, it should make sure that the game continues until the user begs for mercy or correctly guesses the number. it should give hints to the user about higher or lower to help them. Once one of the win conditions has happened the user should be congratulated in some way and then the script should end. If the user begs for mercy it should echo out a different phrase.

#!/bin/sh

number="$(( ( RANDOM % 100 )  + 1 ))"

echo "Guess a number between 1 and 100, or say 'mercy'"
read answer
until [[ $answer -eq $number || $answer -eq "mercy" ]]
do
    if [ $answer -lt $number ]
    then
        echo "Guess higher"
        read answer
    else
        echo "Guess lower"
        read answer
    fi
done

if [ $answer -eq "mercy" ]
then
    echo "You are a quitter"
else
    echo "CONGRATS"
fi

It keeps just saying congrats every time you guess right or say mercy along with the error code:line 19: [: mercy: integer expression expected

Aucun commentaire:

Enregistrer un commentaire