0

making a rock paper scissors game to learn better bash. For some reason it's not liking my elif statements and I can't figure out why. Also if there are any better ways of doing this let me know, thanks!

#!/bin/bash

#rock beats scissors, scissors beats paper, paper beats rock, 
printf "Welcome to rock paper scissors. Type 'r', 'p', or 's' to start: "

#get users selection
read user

#detect input and assign it to a num that will be used to check against computer.
if [[ $user == [rR] || $user == "Rock" || $user == "rock" ]]
  then
    user="r"
elif [[ $user == [sS] || $user == "scissors" || $user == "Scissors" ]]
  then
    user="s"
elif [[ $user == [pP] || $user == "paper" || $user == "Paper" ]]
  then
    user="p"
else
    printf "Not a valid submission, you entered: $user\n"
fi

#get random number between 1 and 9
comp=$(shuf -i 1-9 -n 1)

#detect what number it was and assign either r p s to it. 
if ((1<=$comp && $comp<=3))
  then 
    comp="r"
    printf "Rock\n"
elif ((4<=$comp && $comp<=6))
  then
    comp="s"
    printf "Scissors\n"
elif ((6<=$comp && $comp<=9))
  then
    comp="p"
    printf "Paper\n"
else
    echo "not in range?"
fi

#find out who won
if [[ $user == $comp ]]
  then
    echo "same!"
elif [[ $user == "r" && $comp == "p" ]]
    echo "user rock comp paper COMP WINS"
elif [[ $user == "p" && $comp == "r" ]]
    echo "comp rock user paper USER WINS"
elif [[ $user == "s" && $comp == "r" ]]
    echo "user scissors comp rock COMP WINS"
elif [[ $user == "r" && $comp == "s" ]]
    echo "user rock comp scissors USER WINS"
elif [[ $user == "p" && $comp == "s" ]]
    echo "user paper comp scissors COMP WINS"
elif [[ $user == "s" && $comp == "p" ]]
    echo "user scissors comp paper USER WINS"
else
    echo "something is borked"
fi

And I get this error:

./rps.sh: line 49: syntax error near unexpected token `elif'
./rps.sh: line 49: `elif [[ $user == "p" && $comp == "r" ]]'

closed as off-topic by Jeff Schaller, jimmij, steeldriver, Anthon, Thomas Dickey Jun 18 '16 at 23:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, jimmij, steeldriver, Anthon, Thomas Dickey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Missing your "then"s – Jeff Schaller Jun 18 '16 at 19:51
  • 5
    Pro tip: shellcheck.net – Jeff Schaller Jun 18 '16 at 19:54
  • Mmmmmmmmm crap. thanks for that site. I can give you credit if you wanna submit an answer! – Egrodo Jun 18 '16 at 19:54
  • 1
    It'll be a bit before I could write something up. FYI also: unix.stackexchange.com/q/155551/41104 – Jeff Schaller Jun 18 '16 at 19:56
  • I probably shouldn't have answered in the comments anyway, so I'd be in favor of just closing this Q as a typo-fix. – Jeff Schaller Jun 18 '16 at 20:07
2

A much more readable way to do this sort of this is with a case statement:

move=''
while [[ -z "$move" ]]; do
   echo "Please specify paper, scissors, or rock.  >"
   read userinput
   case "$userinput" in
      [Rr]*)
         move="rock"
         ;;
      [Pp]*)
         move="paper"
         ;;
      [Ss]*)
         move="scissors"
         ;;
      *)
         echo "Invalid input."
         ;;
   esac
done

You can do something similar with your randomly selected computer player's move and for outcome determination.

Not the answer you're looking for? Browse other questions tagged or ask your own question.