Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

There is another thread for this topic but it is closed. I would like to revisit this. I m trying to write a utility script for myself. However i am not a programmer so I wanted to get some feed back. although I do appreciate others attempt to help, I do not want to go off on a tangent and have to read a bunch of stuff. My need is very specific.

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option3" "Quit")
select opt in "${options[@]}"
do
    case $opt in


 if [ "$RESP" = "y" ]; then
                action here
        else
                echo "Thank you."
        sleep 2
    exit;  
fi

;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

How do I get from the exit; or fi back to the menu ?

Thanks very much,

share|improve this question
    
What's the other thread? This is getting close votes, even though bash scripting is not off-topic for Ask Ubuntu. That might be because you've said it's a re-ask of a closed question. Can you tell us what the closed question is, so this can get sorted out? If this is closed as a duplicate of it, then it can still be reopened. –  Eliah Kagan Mar 17 '13 at 5:11

1 Answer 1

First, you're missing an "Option 1") case before your if. And "Option3" in your options list is missing a space: it should be "Option 3".

Other than that, I can't really understand your question. What's the problem? Once those are fixed, it seems to work for me. Below is a test run. To clarify, exit exits the script and returns the user to their shell prompt. If you want to stay in the script, don't use exit.

ace@ace2:~$ ./test.sh
1) Option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice: 1
Thank you.
ace@ace2:~$ RESP="y" ./test.sh
1) Option 1
2) Option 2
3) Option3
4) Quit
Please enter your choice: 1
you chose choice 1
Please enter your choice: 2
you chose choice 2
Please enter your choice: 3
you chose choice 3
Please enter your choice: 4
ace@ace2:~$

Here is the code:

#!/bin/bash
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in

    "Option 1")
        if [ "$RESP" = "y" ]; then
            echo "you chose choice 1"
        else
            echo "Thank you."
            sleep 2
            exit
        fi
        ;;
    "Option 2")
        echo "you chose choice 2"
        ;;
    "Option 3")
        echo "you chose choice 3"
        ;;
    "Quit")
        break
        ;;
    *) 
        echo invalid option
        ;;
    esac
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.