Announcement

Collapse
No announcement yet.

Bash help

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Bash help

    Hi,

    Would someone please help me on Bash? Here is a broken script. I need someone to make it work.

    Code:
    #!/bin/bash
    echo -n "
      How are you feeling?
        Tire
        Bored
        Hungry
    
          Enter: " #Type how you feel
    
    read option
      if $option 'Tire'; then
      echo "You should get some rest."
    
      if $option 'Bored'; then
      echo "Play a game of Frozen Bubble."
    
      If $option 'Hungry'; then
      echo "Order pizza!"
    Would someone please fix it for me? If possible add an error prompt that will say something like "You must have made a typo. Select option again." if I choose something that is not in the options.

    Also, how do I make the script stay active until I exit it? Like after the script replies me, it goes back to asking me how I feel?

    Thanks in advance for the help.

    #2
    Re: Bash help

    Try http://www.tldp.org/LDP/abs/html/ and look for loops.
    Blog: Tasty Tidbits

    Comment


      #3
      Re: Bash help

      #!/bin/bash
      echo -n "
          How are you feeling?
              Tire
              Bored
              Hungry

                  Enter: " #Type how you feel

      read option
         if [ $option = "Tire" ]; then
         echo "You should get some rest."; fi

         if [ $option = "Bored" ]; then
         echo "Play a game of Frozen Bubble."; fi

         if [ $option = "Hungry" ]; then
         echo "Order pizza!"; fi
      as you can see, adding [] to the area that has to be tested does the trick. You also have to end the 'if' with 'fi'

      This script is not a good one, it'll return an error if nothing is typed. It might also be more wrong as I don't know anything about bash programming... yet...

      An easier to understand tutorial can be found at http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html

      Edit:
      If you add: if [ $option != "" ]; then
      after 'read option' and 'fi' at the end, then you wont get the error message if nothing is entered

      Here is the codes with typo and loop: (as goos as I could get it after working with bash for only 4hours (got some 'basic' programming skills from the earlier days))

      ----------------------------------------------------------

      #!/bin/bash
      while [ "$option" != "exit" ]; do   #makes a loop until 'option' = "exit"
      echo -n
          How are you feeling?
              Tire
              Bored
              Hungry

                  Enter: "
      #Prits the text on the screen

      read option   #Asks for what to put in option

      if [ "$option" != "exit" ]; then  #This jumps over the last 'read' if 'exit' is typed
      if [ "$option" != "" ]; then   #Checks if you have written anything (no_text)

         if [ "$option" = "Tire" ]; then   #Tests if you typed Tired
         echo "You should get some rest."; else   #If you didn't type Tired, It'll test next

         if [ "$option" = "Bored" ]; then
         echo "Play a game of Frozen Bubble."; else

         if [ "$option" = "Hungry" ]; then
         echo "Order pizza!"; else

      echo "typo"   #As it didn't stop at any ifs, this line is executed
      fi; fi; fi   #All ifs ends here. While the exit and no_text ends later
         else
         echo "You have to type something!"   #if there was no_text typed, the echo is executed
      fi   #The no_text if ends here
         read; clear   #this makes a pause until you hit enter, then clears the screen
      fi   #ends the exit if. If exit is typed, it jumps here
      done   #ends the loop or restarts it
         clear   #Clears the screen before exiting
         echo "Good Bye!"

      Comment


        #4
        Re: Bash help

        you can also use a case statement:

        Code:
        if [ "$option" != "exit" -o "$option" != "EXIT" ]; then
           case $option in
               "tired" | "TIRED") echo "something" ;;  # the ;;'s end each condition
               "bored" | "BORED") echo "something" ;;
               "another" | "ANOTHER") echo "antoher" ;;
                *) exit 1 ;; #everything else
            esac
        fi

        Comment


          #5
          Re: Bash help

          I knew that! But mine just looks so much nicer with all the lines and the ifs inside the ifs and all that other nice things that keeps peoples away from steeling my codes.

          Or I could just admit that my biggest program ever just makes ANSII pictures from 4 collored bitmaps (and that is infact verry easy, but with my logical programming it almost took an whole page in VB and a whole day of work)

          Comment


            #6
            Re: Bash help

            heh. I used to do the same thing. You'll get better at it, takes time.

            Comment

            Working...
            X