Results 1 to 9 of 9

Thread: Yes/no Shell Script Prompt Issue

  1. #1
    Join Date
    Jun 2011
    Beans
    64

    Question Yes/no Shell Script Prompt Issue

    I'm trying to create a yes/no prompt within a shell script that I'm making for someone else and it's starting to get irritating right now... I've been up and down the forums, checked my 833 page PDF book on Bash/Shell scripting for reference and I can not get this to work at all...
    Every time I build a script with various aspects to it I test it out piece by piece. I usually use Zenity for prompts and other scripts, but this is for someone who doesn't have it installed on their system. So far everything works until the yes/no prompt here.

    Here's the NOT WORKING test prompt:

    Code:
    #!/bin/bash
    read -p "Yes or no? If you don't know please type 'no': " prompt
    if [ $prompt == [Yy][Ee][Ss] ] ; then
        #Execute desired command if user agrees.
        printf "Thank you, please wait one moment \n"
        sleep 2
        #Execute functions
        function1
        sleep 2
        function2
        sleep2
        printf "Everything worked just fine without any errors, have a nice day!"
    else
        #Exit if user decided against the action
        printf "Thank you for your time, have a nice day \n"
    fi
    My terminal output is

    Code:
    test.sh: 3: [: yes: unexpected operator
    No worked just fine
    The crazy thing is, I tested the function which works, I tested it using my zenity prompt, but as soon as I use my prompt above, it stops working!
    Can anyone offer me some help or suggestions?

  2. #2
    rai4shu2 is offline Extra Foam Sugar Free Ubuntu
    Join Date
    Jun 2006
    Beans
    Hidden!
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Yes/no Shell Script Prompt Issue

    Why not just check for "y" or "n"? Then it's a simple case of:

    Code:
    if "$prompt" = "y"; then

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Yes/no Shell Script Prompt Issue

    I think you might need to use the double bracket [[ ... ]] syntax (aka extended test) to get the regex test to work

    Code:
    if [[ $prompt == [Yy][Ee][Ss] ]] ; then

  4. #4
    Join Date
    Jun 2011
    Beans
    64

    Re: Yes/no Shell Script Prompt Issue

    Thanks Steel diver but that didn't work either
    I tried that before, and just now but:

    Code:
    Yes or no? If you don't know please type 'no': yes
    test.sh: 3: test.sh: [[: not found
    Thank you for your time, have a nice day
    @rai4shu2

    I want to make sure that anyone who types "yes" with caps or no, or whatever that it will work...

  5. #5
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Yes/no Shell Script Prompt Issue

    well it works for me... I copied your exact script and modified it (no spaces between the [[ btw i.e. '[[' and ']]' are keywords not literal brackets)

    Code:
    #!/bin/bash
    
    read -p "Yes or no? If you don't know please type 'no': " prompt
    if [[ $prompt == [Yy][Ee][Ss] ]] ; then
        #Execute desired command if user agrees.
        printf "Thank you, please wait one moment \n"
        sleep 2
        #Execute functions
        function1
        sleep 2
        function2
        sleep2
        printf "Everything worked just fine without any errors, have a nice day!"
    else
        #Exit if user decided against the action
        printf "Thank you for your time, have a nice day \n"
    fi

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Yes/no Shell Script Prompt Issue

    [[ is a bashism. It doesn't work in dash (sh is a symlink to dash in Ubuntu).

    I'd use a case statement
    Code:
    read -r ans
    case $ans in
        [yY]|[yY][eE][sS])
            do something
            ;;
        [nN]|[nN][oO])
            do something else
            ;;
        *)
            whatever
            ;;
    esac

  7. #7
    Join Date
    Jun 2011
    Beans
    64

    Re: Yes/no Shell Script Prompt Issue

    I don't get it... I copied EXACTLY what you have, then:

    Code:
    usr@usr:path/to/script$ sh test.sh
    Yes or no? If you don't know please type 'no': yes
    test.sh: 4: test.sh: [[: not found
    Thank you for your time, have a nice day 
    usr@usr:path/to/script$

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

  9. #9
    Join Date
    Jun 2011
    Beans
    64

    Re: Yes/no Shell Script Prompt Issue

    Oh wow... I typed "bash test.sh" and it worked... Why does it do that if both commands run it!?!?
    Last edited by rhss6-2011; July 2nd, 2012 at 06:06 PM. Reason: Adding more info...

Tags for this Thread

Bookmarks

Posting Permissions

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