Question

At the moment I am working on a blackjack game using shell script. I have most of the script working with functions however the method I am using to find out if the player/computer goes bust doesn't seem to work. Could anyone point me in the right direction. (I am new to shell script.) When running it it will throw syntax errors around the lines that begin elif and sometimes if. It also prints all of the 'echo' outputs in bustConfirm instead of only the one that is true.

Also yes, one of my functions is called bustCheck.

bustConfirm(){
bust='bust'
under='under'

if [ $userBust -eq $bust -a $systemBust -eq $bust ]
then
    echo "You both went bust! Be more careful!" 
    endGameRepeat
elif  [ $userBust -eq $bust -a $systemBust -eq $under ]
    echo $userName "went bust! Congratulations" $systemName"!"
    endGameRepeat
elif  [ $userBust -eq $under -a $systemBust -eq $bust ]
then
    echo $systemName "went bust! Congratulations" $userName"!"
    endGameRepeat
else
    echo "Nobody went bust! Well played!"
    endGameScores
fi
}



bustCheck(){
if [ "$userScore" -gt 21 ]
then
    echo $userName "is bust!"
    userBust='bust'
else
    userBust='under'
fi

if [ "$systemScore" -gt 21 ]
then
    echo $systemName "is bust!"
    systemBust='bust'
else
    systemBust='under'
fi      
bustConfirm

}

The idea is that I wanted to use an && in the bustConfirm function and then an || to get the player is bust or system is bust result if only one of them was bust.

Also just a pointer but in the bustCheck I am seeing userBust and systemBust to contain the words bust or under. I created the variables bust and under for the bustConfirm function.

systemScore, userScore, systemName and userName are set before when the script is running. Hope I've given enough detail and formatted it properly, first proper post so I apologize if not!

Was it helpful?

Solution

Taking a quick look, I see that the first if statement doesn't have a space after the opening square bracket.

I also recommend you put quotes around your variable names in if statements. This is due to the way shell actually works. The bash shell is extremely intelligent, and before your program has a chance to do anything, it grabs the line, does its magic, and then presents the line to the processor.

For example:

foo=""
if [ $foo = "" ]
then
   echo "Foo is blank"
fi

Seems simple enough. However, what happens is that your shell will grab the line, substitute the value of $foo for the string "$foo", and then execute the line. Since $foo is blank, your if statement will become:

if [ = "" ]   # That's not right!
then
   echo "Foo is blank"
fi

By using quotes, this:

foo=""
if [ "$foo" = "" ]
then
   echo "Foo is blank"
fi

becomes:

foo=""
if [ "" = "" ]
then
   echo "Foo is blank"
fi

And that is valid. Another thing you can do is use the new test format that uses double square brackets:

foo=""
if [[ $foo = "" ]]
then
   echo "Foo is blank"
fi

This will always work even without the extra quotes, and is now recommended unless you have to have your program compatible with the original Bourne shell syntax.

One more thing you can do in debugging your shell script is to use set -xv which turns on verbose debugging. Each statement, before it is executed will be printed, then it will print again after the shell fills in variables, patterns, etc., and then execute. It's a great way to debug your program. Just put set -xv on the line before you want this verbose debugging mode and use set +xv to turn it off. (Yes, the - turns it on and + turns it off.)


Thanks alot David, great answer, could you also tell me what the best way to get the && or equivalent of it within this as I need to find out if they are both bust, or just one etc

As already mentioned in a comment, you can use either one of these two forms:

if [ "$foo" = "bar" ] && [ "$bar" = "foo" ]

or

if [[ $foo = "bar" && $bar = "foo" ]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top