Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I'm working on a bash script and have used a case command. The case command performs specific tasks depending on the parameters entered. My case command would look something ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    75

    Help with case command in bash script

    Hi,

    I'm working on a bash script and have used a case command. The case command performs specific tasks depending on the parameters entered. My case command would look something like

    function option {
    echo " your choice: "
    read choice
    }

    case $choice in
    R) r_script;; #### executes the r_script function
    L) l_script;; #### executes the l_script function
    X) exit;; ### exits the script
    *) option;; ### rerun the option function again
    esac

    function i've defined are r_script, l_script & option

    option is the function which prompts me for choice and case command carries out the requisite task based on user input. My understanding of the above case command is that if any letter except for R , L & X are entered it should again run the option function.

    When executing this script, I've observed that when I enter a non-valid alphabet, it runs the option function once and then irrespective of the choice made it exits. The case command / option function seems to work fine the first time, but when it runs the option function the second time it exits no matter what the choice I enter. I assume that irrespective of the number of times I enter an invalid choice (ALPHABETS other than R, L , X) it should keep running the option command and prompting me for choice

    Could someone assist me with where I've gone wrong... Would appreciate your assistance

    --- Syd

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    It's due to the flow of your program. When you call a function the function is run. Once the function completes, the program jumps back to where the function was called.

    It's quite hard to explain so maybe an example would be better:

    Code:
    #function not run until called
    function my_func {
        echo "hello world"
    }
    
    #first thing to be printed
    echo "script begins"
    
    #now call the function
    my_func
    
    #this line is executed after the my_func function has complete
    echo "script ends"
    So, for your code the first thing that is done is case checks $choice. Since it hasn't been set yet it runs the option function as that's the default option. The option function completes and so there is nothing left for the script to do.

    What you probably want to do is use an infinate while loop to keep running your case:

    Code:
    function option {
        echo " your choice: "
        read choice
    }
    
    while [[ true ]]
    do
        case $choice in
            R) r_script;; #### executes the r_script function
            L) l_script;; #### executes the l_script function
            X) exit;; ### exits the script
            *) option;; ### rerun the option function again
        esac
    done
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    75

    Thumbs up

    Thanx Kieren for your prompt reply. Because of whatever little I know of scripting I did not realize the concept of flow hence I assumed that when the function is called it would go back to that line in the script (so to say) and begin execution.

    Your explanation cleared up the confusion I had. In the mean time, I did break my script into smaller functions and managed to clear the issue i faced.

    I also tried out the while infinite loop you mentioned about in your reply and that's also resolved the problem.

    Thanx again for your valuable feedback, assistance and explaination...


    -- Syd

Posting Permissions

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