Results 1 to 7 of 7

Thread: Bash help

  1. #1
    Junior Member
    Join Date
    Oct 2005
    Posts
    9

    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. #2
    Senior Member registered user
    Join Date
    Dec 2003
    Location
    Salt Lake City, UT, U.S.A.
    Posts
    1,338
    anticuchos,

    try this, replace your "if" statements with one big "case", havent worked out the "looping" part, conditional looping is a "structured" way, not quite sure on a way to "unconditionaly" loop, yet...

    This way, you can add new entries, by adding a "something)" line, and what to do when that text is entered. The "special" "*)" condition is when "nothing else matches" within the case block...

    Code:
    #!/bin/bash
    echo -n "
        How are you feeling?
            Tired
            Bored
            Hungry
    
                Enter: " #Type how you feel
      read OPTION
      case $OPTION in
         tired)
           #if $option 'Tire'; then
           echo "You should get some rest."
         ;;
    
         bored)
           #if $option 'Bored'; then
           echo "Play a game of Frozen Bubble."
         ;;
    
         hungry)
           #If $option 'Hungry'; then
           echo "Order pizza!" 
         ;;
         *)
           echo "Invalid entry: $OPTION!"
         ;;
      esac
    Hope this helps,
    Ms. Cuddles

  3. #3
    Senior Member registered user
    Join Date
    Dec 2003
    Location
    Salt Lake City, UT, U.S.A.
    Posts
    1,338
    anticuchos,

    Just figured out the unconditional, or, "infinite" loop, for your script:

    Code:
    #!/bin/bash
    while true
    do
    echo -n "
        How are you feeling?
            Tired
            Bored
            Hungry
    
                Enter: " #Type how you feel
      read OPTION
      case $OPTION in
         tired)
           #if $option 'Tire'; then
           echo "You should get some rest."
         ;;
    
         bored)
           #if $option 'Bored'; then
           echo "Play a game of Frozen Bubble."
         ;;
    
         hungry)
           #If $option 'Hungry'; then
           echo "Order pizza!"
         ;;
         *)
           echo "Invalid entry: $OPTION!"
         ;;
      esac 
    done
    Note, that, this script HAS TO BE control-C 'ed out of, or it will NEVER end...

    You can always add a condition into the "case" block, like, "exit" - to allow it to exit easier...

    Case closed, so to speak
    Ms. Cuddles

  4. #4
    Senior Member registered user
    Join Date
    Nov 2002
    Location
    Long Island, NY USA
    Posts
    1,510
    Note, that, this script HAS TO BE control-C 'ed out of, or it will NEVER end...
    wrap the script in a while -> do statement, then make the a section that changes

    Code:
    ExitCondition="no"
    while [ $ExitCondition == "no" ] ;
    do
        #Place The rest of the script here
    
        #under the case statement, make
        # exit do a ExitCondition="yes"
    done

  5. #5
    Senior Member registered user
    Join Date
    Dec 2003
    Location
    Salt Lake City, UT, U.S.A.
    Posts
    1,338
    As UnderSxore explains, changing my script to a "conditional" while do loop, will allow easier exiting, other than the CTL-C issue... From what you stated, my assumption was you didnt want to change the script, just 1 ) make it work, 2 ) make it repeat "indeffinately"...

    If you take UnderScore's idea, and my changes to your script, here is what it would look like:

    Code:
    #!/bin/bash
    # Seed the exit condition with false...
    ExitCondition="no"
    # Loop your script while the exit condition is still false...
    while [ $ExitCondition == "no" ] ;
    do
    # Output our menu selections...
    echo -n "
        How are you feeling?
            Tired
            Bored
            Hungry
            -=-=-=-=-=-
            Leave to exit script
    
                Enter: " #Type how you feel
    
    # Get the users input...
      read OPTION
    
    # Begin our interogation of the users input...
      case $OPTION in
         tired)
           echo "You should get some rest."
         ;;
    
         bored)
           echo "Play a game of Frozen Bubble."
         ;;
    
         hungry)
           echo "Order pizza!"
         ;;
    
         leave)
           echo "Hope you are feeling better now."
           # User wants to exit, change our exit condition to true...
           ExitCondition="yes"
         ;;
    
         *)
           # User has made an entry that doesnt contain any of the tests we are looking for...
           # ... Output a message that they entered something that is invalid, and what they
           # entered...
           echo "Invalid entry: $OPTION!"
         ;;
      esac
    done
    I changed your script a little, took out the "if" statements completely, and added comments to explain more...

    Hope this helps,
    Ms. Cuddles

  6. #6
    Senior Member registered user
    Join Date
    Aug 2004
    Location
    Sydney/Australia
    Posts
    311
    One of the "if" statements in the original script had an upper-case "i" ...

    "If" rather than "if"


    jm

  7. #7
    Senior Member registered user
    Join Date
    Aug 2004
    Location
    Sydney/Australia
    Posts
    311
    plus it needs to be closed with matching "fi" statements ...

Similar Threads

  1. Only need the kernel + bash
    By sibtay in forum Customising & Remastering
    Replies: 4
    Last Post: 10-25-2004, 11:08 AM
  2. C++ Compiler in Bash
    By dehahn in forum General Support
    Replies: 4
    Last Post: 10-16-2004, 01:31 AM
  3. Logging off from bash
    By Salzi in forum General Support
    Replies: 1
    Last Post: 09-09-2004, 02:56 AM
  4. Using Bash
    By Guitarplaya512 in forum General Support
    Replies: 5
    Last Post: 03-14-2004, 11:03 AM
  5. Bash Bad Interpreter
    By michaelmontagne in forum General Support
    Replies: 1
    Last Post: 09-04-2003, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


RAM-HOL-SAM60CPU RAM USB-C Powered Dock for Samsung Tab Active5 - 3 - 2 picture

RAM-HOL-SAM60CPU RAM USB-C Powered Dock for Samsung Tab Active5 - 3 - 2

$81.99



New Samsung 512GB FIT Plus USB 3.2 Gen 1 Type-A Flash Drive MUF-512AB/AM NEW picture

New Samsung 512GB FIT Plus USB 3.2 Gen 1 Type-A Flash Drive MUF-512AB/AM NEW

$120.00



SAMSUNG M393A2K43CB2-CTD8Y 16GB 2Rx8 PC4-2666V ECC REG Server Memory RAM picture

SAMSUNG M393A2K43CB2-CTD8Y 16GB 2Rx8 PC4-2666V ECC REG Server Memory RAM

$160.00



NEW Samsung NoteBook NT930QCG touch LCD Full Screen Assembly Blue picture

NEW Samsung NoteBook NT930QCG touch LCD Full Screen Assembly Blue

$225.77



RAM-HOL-SAM52CPU RAM USB-C Powered Dock Samsung Tab Active4 Pro & Tab Active Pro picture

RAM-HOL-SAM52CPU RAM USB-C Powered Dock Samsung Tab Active4 Pro & Tab Active Pro

$81.99



RAM-GDS-SKIN-SAM54-NG-1 RAM Mounts IntelliSkin® Next Gen for Samsung Tab Active4 picture

RAM-GDS-SKIN-SAM54-NG-1 RAM Mounts IntelliSkin® Next Gen for Samsung Tab Active4

$93.99



RAM-GDS-DOCK-SAM88CPU RAM GDS® Tough-Dock™ for Samsung Tab A9+ picture

RAM-GDS-DOCK-SAM88CPU RAM GDS® Tough-Dock™ for Samsung Tab A9+

$174.99



Samsung SCXSTP000, SCX-STP000 (Swingline Staple Cartridge - 3 Refills) picture

Samsung SCXSTP000, SCX-STP000 (Swingline Staple Cartridge - 3 Refills)

$57.99



Samsung 24

Samsung 24" S3 Essential IPS 100Hz Monitor | Sealed | Fast shipping

$50.00



Samsung Galaxy View Tablet 18.4 SM-T670/677A Charging Port 100% Original Oem picture

Samsung Galaxy View Tablet 18.4 SM-T670/677A Charging Port 100% Original Oem

$98.99