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
  •  


2-Bay/4-Bay Hard Drive Enclosure for 3.5

2-Bay/4-Bay Hard Drive Enclosure for 3.5"/2.5'' SATA HDD/SSD 10Gbps USB-C 3.2

$75.99



OWC Mercury Elite Pro 2 Bay RAID Enclosure with USB 3.2  for Dual 3.5 inch HDDs picture

OWC Mercury Elite Pro 2 Bay RAID Enclosure with USB 3.2 for Dual 3.5 inch HDDs

$87.99



Intel VROC Premium RAID Controller Card PCIe Add-On Module VROCPREMMOD picture

Intel VROC Premium RAID Controller Card PCIe Add-On Module VROCPREMMOD

$265.00



CalDigit VR minin 2 Portable Raid Storage On The Go picture

CalDigit VR minin 2 Portable Raid Storage On The Go

$75.00



WAVLINK RAID 2 Bay Hard Drive Docking Station SATA 2.5/3.5'' SSD HDD Enclosure picture

WAVLINK RAID 2 Bay Hard Drive Docking Station SATA 2.5/3.5'' SSD HDD Enclosure

$119.99



Acasis USB 3.2 2 Bay HDD RAID Enclosure for 2.5/3.5

Acasis USB 3.2 2 Bay HDD RAID Enclosure for 2.5/3.5" SATA Drives External Array

$55.00



Dell PowerEdge HBA330 LP PCI-E 12GBs RAID SAS Controller Adapter J7TNV picture

Dell PowerEdge HBA330 LP PCI-E 12GBs RAID SAS Controller Adapter J7TNV

$18.95



NVME M.2 Adapter Card PCIE X16 Splitter Expansion Controller 4-Disk Soft RAID picture

NVME M.2 Adapter Card PCIE X16 Splitter Expansion Controller 4-Disk Soft RAID

$24.43



Broadcom 9600-24i RAID Controller PCIe Gen 4.1 Tri-Mode NEW 05-50111-01004 picture

Broadcom 9600-24i RAID Controller PCIe Gen 4.1 Tri-Mode NEW 05-50111-01004

$649.99



Areca ARC-1882IX-24 (16i4e)24-Port RAID Controller Card w/BBU /HDD Monitor Board picture

Areca ARC-1882IX-24 (16i4e)24-Port RAID Controller Card w/BBU /HDD Monitor Board

$300.00