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
  •  


AMD Ryzen 5 5500 Processor ( read Description) picture

AMD Ryzen 5 5500 Processor ( read Description)

$59.00



(LOT OF 20) Intel Core i5-8400 CPU 2.80GHz Desktop Processor (SR3QT) picture

(LOT OF 20) Intel Core i5-8400 CPU 2.80GHz Desktop Processor (SR3QT)

$700.00



INTEL CORE I9-10940X X-SERIES PROCESSOR | 3.30GHZ | SRGSH picture

INTEL CORE I9-10940X X-SERIES PROCESSOR | 3.30GHZ | SRGSH

$179.99



Compo MSI B150I GAMING PRO AC LGA1151 16 Gb DDR4 + Cpu I7 6700+I/O Shields. picture

Compo MSI B150I GAMING PRO AC LGA1151 16 Gb DDR4 + Cpu I7 6700+I/O Shields.

$159.00



Intel Core i7-9700 Octa-Core 3 GHz Desktop Processor LGA 1151 picture

Intel Core i7-9700 Octa-Core 3 GHz Desktop Processor LGA 1151

$79.99



Intel Core i7-3770K 3.5 GHz Processor picture

Intel Core i7-3770K 3.5 GHz Processor

$39.00



Intel Core i5-12500T SRL5W 2GHz 18MB 6 Core Socket LGA1700 Desktop CPU Processor picture

Intel Core i5-12500T SRL5W 2GHz 18MB 6 Core Socket LGA1700 Desktop CPU Processor

$114.99



Intel Core i7-9700K Octa-Core 3.6 GHz Desktop CPU picture

Intel Core i7-9700K Octa-Core 3.6 GHz Desktop CPU

$99.99



Intel Core i7-4770 3.40GHz Quad Core SR149 LGA 1150 CPU Processor tested picture

Intel Core i7-4770 3.40GHz Quad Core SR149 LGA 1150 CPU Processor tested

$35.00



INTEL CORE I9-10980XE PROCESSOR | 3.00GHZ | SRGSG picture

INTEL CORE I9-10980XE PROCESSOR | 3.00GHZ | SRGSG

$299.99