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
  •  


SFP TO RJ45 TRANSCEIVER SFP MODULE COPPER SFP OPTICAL PORT TO RJ45 ETHERNET PORT picture

SFP TO RJ45 TRANSCEIVER SFP MODULE COPPER SFP OPTICAL PORT TO RJ45 ETHERNET PORT

$7.99



Dell Intel X710-DA2 5N7Y5 Dual-Port 10GbE SFP+ PCIe 3.0 x8 Network Adapter picture

Dell Intel X710-DA2 5N7Y5 Dual-Port 10GbE SFP+ PCIe 3.0 x8 Network Adapter

$27.50



Cisco Acacia DP04QSDD-E25-840 400G QSFP-DD Coherent DCO Transceiver Module picture

Cisco Acacia DP04QSDD-E25-840 400G QSFP-DD Coherent DCO Transceiver Module

$699.99



NEW Sealed Cisco SFP-10G-LR 10GBASE-LR SFP+ 1310nm 10km *US Shipping* picture

NEW Sealed Cisco SFP-10G-LR 10GBASE-LR SFP+ 1310nm 10km *US Shipping*

$24.00



Genuine Cisco SFP-10G-SR 0-2415-03 10 Gigabit Transceiver picture

Genuine Cisco SFP-10G-SR 0-2415-03 10 Gigabit Transceiver

$5.40



Intel X520-DA2 10Gb 10Gbe 10 Gigabit Network Adapter NIC Dual Port E10G42BTDA picture

Intel X520-DA2 10Gb 10Gbe 10 Gigabit Network Adapter NIC Dual Port E10G42BTDA

$28.94



HP 560SFP+ 10GB 2-port  PCIe NIC Ethernet ADAPTER 669279-001 w/ (2) 10GB SFP+ picture

HP 560SFP+ 10GB 2-port PCIe NIC Ethernet ADAPTER 669279-001 w/ (2) 10GB SFP+

$16.99



Lot of 5 Cisco 10G SFP+ 850nm 300m Transceiver Module P/N: SFP-10G-SR-10-2415-03 picture

Lot of 5 Cisco 10G SFP+ 850nm 300m Transceiver Module P/N: SFP-10G-SR-10-2415-03

$15.00



Dell 10 Gigabit SFP+ SR Transceiver 0XYD50 NEW SEALED picture

Dell 10 Gigabit SFP+ SR Transceiver 0XYD50 NEW SEALED

$24.99



Arista SFP-10G-SR 10GBASE-SR XVR-10001-20 850nm SFP+ Optical Transceiver picture

Arista SFP-10G-SR 10GBASE-SR XVR-10001-20 850nm SFP+ Optical Transceiver

$30.00