Help on Bash Scripting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keyvanrahmadi
    New Member
    • Feb 2007
    • 57

    Help on Bash Scripting

    Hello there guys,

    i think i am at the end of this, which i have to edmit has been a huge learning curve for me, but i still have couple of problems, which i was hoping you can help me. I think tbh its a very simple thing which i am over looking and just require a pair of fresh eyes to look at. I greatly appriciate if you could have a look at the script and let me know what you think. if you have any solution, or advise just email me or pm here..

    player 1 option scripts:


    Code:
    #!/bin/bash
     
    function stored_word()
    {
      case $(( $$ % 8 )) in
        0 ) echo "energy"    ;;  1 ) echo "touch" ;;
        2 ) echo "climbing" ;; 3 ) echo "declare" ;;
        4 ) echo "marry"  ;;    5 ) echo "relax"   ;;
        6 ) echo "bugs"     ;;    7 ) echo "inaccessible" ;;
      esac
    }
     
    function startup() {
     
    echo "Choose number of Players:"
    read players
     
    if [ $players -eq 1 ] ; then
            echo " A Random word has been chosen... Enjoy the Game"
    elif [ $players -eq 2 ] ; then
            echo "choose a word for player two to guess:"
            read player2word
    else
            echo "incorrect option..."
            replay
    fi
    }
     
    function replay() {
     
    echo " Do you wish to play again? TYPE \"y\" to play again or \"n\" to quit"
    read choice
     
    if [ $choice == y ] ; then
    startup
    else
    echo "Hope you enjoyed the game"
    exit 0
    fi
    }
     
    startup
    life=10
    word=$(stored_word)
    letters=$(echo $word | wc -c)
    letters=$(( $letters - 1 ))
    template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
    remaining=$letters
     
    echo "  ****The word you are trying to guess has \"$letters\" letters ****"
     
    while [ $template != $word ] ; do
            echo""
            echo -n "Word is: $template Please choose a Letter: " ;
            read guess
             guesses=$(( $guesses + 1 ))
             echo " you have chosen $guess"
         if echo $guessed | grep -i $guess > .temp1 ; then
             echo "You've already guessed that letter. Try again!"
            guessed="$guessed$guess"
            echo $letter
         elif ! echo $word | grep -i $guess > .temp1 ; then
             echo "Sorry, the letter \"$guess\" is not in the word."
             guessed="$guessed$guess"
             badguesses=$(( $badguesses + 1))
             echo "$guessed"
             life=$(( $life -1 ))
             echo " you have $life life left"
         else
              echo "Good going! The letter $guess is in the word!"
              guessed="$guessed$guess"
              echo $letter
         fi
     
    if [ $life == 0 ]; then
            echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
            replay
            exit 0
     
    fi
     
          lettercount=0
          template2=""
          while [ $lettercount != $(( $letters )) ] ; do
              val1=${word:lettercount:1}
     val2=${template:lettercount:1}
      if [ "$val1" == "$guess" ] ; then
              template2="$template2$guess"
              elif [ "$val2" != '.' ] ; then
              template2="$template2$val2"
              else
              template2="$template2."
              fi
              lettercount=$(( $lettercount + 1 ))
          done
          template=$template2
    done
     
    echo -n "Congratulations! You guessed \"$word\" in \"$guesses\" guesses"
    echo " with $badguesses incorrect guesses"
    replay
    exit 0
    The problem is when asked if you want to play again, when you say yes, it kicks out of the script after printing the followig line:

    Code:
    echo " A Random word has been chosen... Enjoy the Game"

    player 2 option scripts:


    Code:
    startup
     
    life=10
    word=$player2word
    Thats the only line which is different everything else is the same . The script again kicks out after it has asked you choose a word for player 2 to guess, during replay.

    I created 3 function:

    1- stored_words: words used for 1-player game.
    2- Replay
    3- Startup

    so to sumerise and stop boring a hell out of you, the problems are:

    1- the looping so the players can continue if they decide to play again.

    2- combine both scripts into one

    K
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by keyvanrahmadi
    so to sumerise and stop boring a hell out of you, the problems are:
    1- the looping so the players can continue if they decide to play again.
    The problem is that there is no loop. You just ask for a replay,
    if 'y' then you call startup, but all 'startup' does is to print the
    line you see :-)

    Try to put a loop around your game which exits when the
    answer is not 'y'. This should help.

    HTH,
    arne

    Comment

    • keyvanrahmadi
      New Member
      • Feb 2007
      • 57

      #3
      Originally posted by arne
      The problem is that there is no loop. You just ask for a replay,
      if 'y' then you call startup, but all 'startup' does is to print the
      line you see :-)

      Try to put a loop around your game which exits when the
      answer is not 'y'. This should help.

      HTH,
      arne
      tbh i am not sure i understand what you mean.. i thought as startup is a function, it will get called up when its mentioned and should run... and if there is a loop which exist when the answer is not 'y', surely that will just end the game??

      am i being really thick here?

      K

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by keyvanrahmadi
        tbh i am not sure i understand what you mean.. i thought as startup is a function, it will get called up when its mentioned and should run... and if there is a loop which exist when the answer is not 'y', surely that will just end the game??

        am i being really thick here?

        K
        Sorry, if I wasn't clear.

        If you call replay in line 101, it will call startup, which will print and return. The program will exit afterwards (which is probably not what you want). Depending on the return code of startup, you should jump back to line 43 in order to repeat the game. This is usually done by a loop.

        arne

        Comment

        • keyvanrahmadi
          New Member
          • Feb 2007
          • 57

          #5
          Originally posted by arne
          Sorry, if I wasn't clear.

          If you call replay in line 101, it will call startup, which will print and return. The program will exit afterwards (which is probably not what you want). Depending on the return code of startup, you should jump back to line 43 in order to repeat the game. This is usually done by a loop.

          arne
          I am not sure about how i can find the return code of startup.. but am i correct this is what you mean, instead of having replay at the end of the scrip have:

          Code:
          echo " to play again press 'y' or 'n' to exit"
          read choice
          
          while [ $choice == y ] ; do
          startup
          else
          exit 0
          done
          I am sure my while statement is incorrect, but is that what you meant?

          K

          Comment

          • arne
            Recognized Expert Contributor
            • Oct 2006
            • 315

            #6
            Originally posted by keyvanrahmadi
            I am not sure about how i can find the return code of startup.. but am i correct this is what you mean, instead of having replay at the end of the scrip have:

            Code:
            echo " to play again press 'y' or 'n' to exit"
            read choice
            
            while [ $choice == y ] ; do
            startup
            else
            exit 0
            done
            I am sure my while statement is incorrect, but is that what you meant?

            K
            No, what I meant was:
            Code:
                  #!/bin/bash
            
                  function stored_word()
            
                  {
            
            	  case $(( $$ % 8 )) in
            
            	      0 ) echo "energy"    ;;  1 ) echo "touch" ;;
            
            	      2 ) echo "climbing" ;; 3 ) echo "declare" ;;
            
            	      4 ) echo "marry"  ;;    5 ) echo "relax"   ;;
            
            	      6 ) echo "bugs"     ;;    7 ) echo "inaccessible" ;;
            
            	  esac
            
                  }
            
                  
            
                  function startup() {
            
            	  echo "Choose number of Players:"
            
            	  read players
            
            	  if [ $players -eq 1 ] ; then
            
                          echo " A Random word has been chosen... Enjoy the Game"
            
            	  elif [ $players -eq 2 ] ; then
            
                          echo "choose a word for player two to guess:"
            
                          read player2word
            
            	  else
            
                          echo "incorrect option..."
            
                          replay
            
            	  fi
            
                  }
            
                  function replay() {
            
            	  echo " Do you wish to play again? TYPE \"y\" to play again or \"n\" to quit"
            
            	  read choice
            
            	  if [ $choice == y ] ; then
            	      
            	      echo 
            	      
            	  else
            
            	      echo "Hope you enjoyed the game"
            
            	      exit 0
            
            	  fi
            
                  }
            
                  
                  choice="y"
            
                  while [ $choice == y ]; do 
            
            	  startup	  
            
            	  life=10
            
            	  word=$(stored_word)
            
            	  letters=$(echo $word | wc -c)
            
            	  letters=$(( $letters - 1 ))
            
            	  template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
            
            	  remaining=$letters
            
            	  
            
            	  echo "  ****The word you are trying to guess has \"$letters\" letters ****"
            
            	  
            
            	  while [ $template != $word ] ; do
            
            	      echo""
            
            	  echo -n "Word is: $template Please choose a Letter: " ;
            
            	  read guess
            
            	  guesses=$(( $guesses + 1 ))
            
            	  echo " you have chosen $guess"
            
            	  if echo $guessed | grep -i $guess > .temp1 ; then
            
            	      echo "You've already guessed that letter. Try again!"
            
            	      guessed="$guessed$guess"
            
            	      echo $letter
            
            	  elif ! echo $word | grep -i $guess > .temp1 ; then
            
            	      echo "Sorry, the letter \"$guess\" is not in the word."
            
            	      guessed="$guessed$guess"
            
            	      badguesses=$(( $badguesses + 1))
            
            	      echo "$guessed"
            
            	      life=$(( $life -1 ))
            
            	      echo " you have $life life left"
            
            	  else
            
            	      echo "Good going! The letter $guess is in the word!"
            
            	      guessed="$guessed$guess"
            
            	      echo $letter
            
            	  fi
            
            	  
            
            	  if [ $life == 0 ]; then
            
            	      echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
            
            	      replay
            
            	      exit 0
            
            	      
            
            	  fi
            
            	  
            
            	  lettercount=0
            
            	  template2=""
            
            	  while [ $lettercount != $(( $letters )) ] ; do
            
            	      val1=${word:lettercount:1}
            
            	      val2=${template:lettercount:1}
            
            	      if [ "$val1" == "$guess" ] ; then
            		  
            		  template2="$template2$guess"
            		  
            	      elif [ "$val2" != '.' ] ; then
            		  
            		  template2="$template2$val2"
            		  
            	      else
            		  
            		  template2="$template2."
            
            	      fi
            
            	      lettercount=$(( $lettercount + 1 ))
            
            	  done
            
            	  template=$template2
            
            	  done
            
            	  
            
            	  echo -n "Congratulations! You guessed \"$word\" in \"$guesses\" guesses"
            
            	  echo " with $badguesses incorrect guesses"
            
            	  choice= replay
            
                  done
            
                  exit 0
            Note my changes in lines 57, 70+72 and 192+194. The game is now surrounded by a big loop, which will be left if the player says not 'y'.

            arne

            Comment

            • keyvanrahmadi
              New Member
              • Feb 2007
              • 57

              #7
              Originally posted by arne
              Note my changes in lines 57, 70+72 and 192+194. The game is now surrounded by a big loop, which will be left if the player says not 'y'.

              arne

              wow.. you have tested it? beacuse it only loops around the choose number of players for me?

              K
              Last edited by keyvanrahmadi; May 21 '07, 03:56 PM. Reason: more information

              Comment

              • arne
                Recognized Expert Contributor
                • Oct 2006
                • 315

                #8
                Originally posted by keyvanrahmadi
                wow.. you have tested it? beacuse it only loops around the choose number of players for me?

                K
                Yes, I have. Works fine for me. You only have to refine your "random" word (it's always the same right now :) )

                Comment

                • keyvanrahmadi
                  New Member
                  • Feb 2007
                  • 57

                  #9
                  Originally posted by arne
                  Yes, I have. Works fine for me. You only have to refine your "random" word (it's always the same right now :) )
                  code now is a bit tider i think, but i am having a sever trouble with the line:

                  Code:
                  if [ $life -eq 0 ]; then
                           echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
                          choice= replay
                          #startup
                          #replay
                         exit 0
                  fi
                  i have tried all the combination to get it to replay again, but i have no idea why it dosent.. when life become 0 it kicks me out of the game?

                  Any Idea?

                  K


                  Code:
                  #!/bin/bash
                  
                  stored_word()
                  {
                    case $(( $$ % 10 )) in
                      0 ) echo "energy"    ;;  1 ) echo "touch" ;;
                      2 ) echo "climbing" ;;   3 ) echo "declare" ;;
                      4 ) echo "marry"  ;;     5 ) echo "relax"   ;;
                      6 ) echo "bugs"     ;;   7 ) echo "inaccessible" ;;
                      8 ) echo "country" ;;    9 ) echo "folder"
                    esac
                  }
                  
                  function life() {
                  life=1
                  }
                  
                  
                  function startup() {
                  
                  echo "Choose number of Players:"
                  read players
                  
                  if [ $players -eq 1 ] ; then
                          echo " A Random word has been chosen... 
                  
                  Enjoy the Game"
                          word=$(stored_word)
                          letters=$(echo $word | wc -c)
                          letters=$(( $letters - 1 ))
                          template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
                          remaining=$letters
                          guess=""
                          guessed=0
                          badguesses=0
                  
                  elif [ $players -eq 2 ] ; then
                          echo "choose a word for player two to guess:"
                          read player2word
                         # clear
                          word=$player2word
                          letters=$(echo $word | wc -c)
                          letters=$(( $letters - 1 ))
                          template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
                          remaining=$letters
                          guess=""
                          guessed=0
                          badguesses=0
                  
                  else
                          echo "incorrect option..."
                          choice= replay
                  fi
                  }
                  
                  function replay() {
                  echo " Do you wish to play again? TYPE \"y\" to play 
                  
                  again or \"n\" to quit"
                  read choice
                  
                  if [ $choice == y ] ; then
                          echo
                  else
                  echo "Hope you enjoyed the game"
                  exit 0
                  fi
                  }
                  
                  choice=y
                  
                  while [ $choice == y ]; do
                  startup
                  life
                  
                  echo "                     ************** The word you are 
                  
                  trying to guess has \"$letters\" letters **************"
                  echo""
                  echo""
                  
                  while [ $template != $word ] ; do
                          echo""
                          echo -n "Word is: $template Please choose a 
                  
                  Letter: " ;
                          read guess
                           guesses=$(( $guesses + 1 ))
                           echo " you have chosen $guess"
                       if echo $guessed | grep -i $guess > .temp1 ; then
                           echo "You've already guessed that letter. Try 
                  
                  again!"
                   guessed="$guessed$guess"
                          echo $letter
                       elif ! echo $word | grep -i $guess > .temp1 ; then
                          echo "Sorry, the letter \"$guess\" is not in the 
                  
                  word."
                          guessed="$guessed$guess"
                          badguesses=$(( $badguesses + 1))
                           echo "$guessed"
                           life=$(( $life -1 ))
                           echo " you have $life life left"
                       else
                            echo "Good going! The letter $guess is in the 
                  
                  word!"
                            guessed="$guessed$guess"
                            echo $letter
                       fi
                  
                  if [ $guess == $word ]; then
                          echo "you have guessed the word $word"
                          choice= replay
                          exit 0
                  fi
                  
                  if [ $life -eq 0 ]; then
                           echo "THE WORD YOU WERE TRYING TO 
                  
                  GUESS WAS \"$word\""
                          #choice= replay
                          #startup
                          #replay
                         exit 0
                  fi
                  
                        lettercount=0
                        template2=""
                        while [ $lettercount != $(( $letters )) ] ; do
                          val1=${word:lettercount:1}
                          val2=${template:lettercount:1}
                            if [ "$val1" == "$guess" ] ; then
                            template2="$template2$guess"
                            elif [ "$val2" != '.' ] ; then
                            template2="$template2$val2"
                            else
                            template2="$template2."
                            fi
                            lettercount=$(( $lettercount + 1 ))
                   done
                        template=$template2
                  done
                  
                  echo -n "Congratulations! You guessed \"$word\" in 
                  
                  \"$guesses\" guesses"
                  echo " with $badguesses incorrect guesses"
                  echo ""
                  echo ""
                  guesses=0
                  guess=""
                  badguesses=
                  choice= replay
                  done
                  exit 0

                  Comment

                  • arne
                    Recognized Expert Contributor
                    • Oct 2006
                    • 315

                    #10
                    Originally posted by keyvanrahmadi
                    code now is a bit tider i think, but i am having a sever trouble with the line:

                    Code:
                    if [ $life -eq 0 ]; then
                             echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
                            choice= replay
                            #startup
                            #replay
                           exit 0
                    fi
                    I'm not sure I understand: if you call "exit 0;" somehwere in the program it will exit immediately. Instead of calling exit, you should jump back to the beginning of the loop if choce is 'y'. Use the "continue" command, for instance.

                    HTH,
                    arne

                    Comment

                    • keyvanrahmadi
                      New Member
                      • Feb 2007
                      • 57

                      #11
                      Originally posted by arne
                      I'm not sure I understand: if you call "exit 0;" somehwere in the program it will exit immediately. Instead of calling exit, you should jump back to the beginning of the loop if choce is 'y'. Use the "continue" command, for instance.

                      HTH,
                      arne
                      I used continue, what it will do is contiune the script even when life is = 0

                      I am not even sure what i need to do next, as i have tried everything i know now.

                      K

                      Comment

                      • arne
                        Recognized Expert Contributor
                        • Oct 2006
                        • 315

                        #12
                        Originally posted by keyvanrahmadi
                        I used continue, what it will do is contiune the script even when life is = 0

                        I am not even sure what i need to do next, as i have tried everything i know now.

                        K
                        Code:
                        if [ $life -eq 0 ]; then	 
                          echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""	 
                          replay
                          continue
                        fi
                        will continue the loop. Make sure to reset 'life' to 10, to clear the template and to choose another word.

                        HTH,
                        arne

                        Comment

                        • keyvanrahmadi
                          New Member
                          • Feb 2007
                          • 57

                          #13
                          Originally posted by arne
                          Code:
                          if [ $life -eq 0 ]; then	 
                            echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""	 
                            replay
                            continue
                          fi
                          will continue the loop. Make sure to reset 'life' to 10, to clear the template and to choose another word.

                          HTH,
                          arne
                          I have managed to set the life counter to 10, by creating a function and calling it when i need to. The only problem now is that not sure why it keeps on calling the same word over and over again when 1 player option is chosen. is there a way to clear the template?

                          K

                          Comment

                          • arne
                            Recognized Expert Contributor
                            • Oct 2006
                            • 315

                            #14
                            Originally posted by keyvanrahmadi
                            I have managed to set the life counter to 10, by creating a function and calling it when i need to. The only problem now is that not sure why it keeps on calling the same word over and over again when 1 player option is chosen. is there a way to clear the template?

                            K
                            What about defining a function that will select a new word randomly and set the template accordingly?

                            arne

                            Comment

                            • keyvanrahmadi
                              New Member
                              • Feb 2007
                              • 57

                              #15
                              Originally posted by arne
                              What about defining a function that will select a new word randomly and set the template accordingly?

                              arne
                              Code:
                              stored_word()
                              {
                                case $(( $$ % 10 )) in
                                  0 ) echo "energy"    ;;  1 ) echo "touch" ;;
                                  2 ) echo "climbing" ;;   3 ) echo "declare" ;;
                                  4 ) echo "marry"  ;;     5 ) echo "relax"   ;;
                                  6 ) echo "bugs"     ;;   7 ) echo "inaccessible" ;;
                                  8 ) echo "country" ;;    9 ) echo "folder"
                                esac
                              }
                              thats what the first funtion dose. I think the main problem is that once the $word is set, it remains the same through out the cycle of the program and only once you exit the game it resets to zero.

                              have i misunderstood you?

                              K

                              Comment

                              Working...