2

Warninng: This may be a specific machine issue or a problem with GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu) will update as I check further.

Update 1: Works with GNU bash, version 4.2.39. and GNU bash, version 4.2.24

I am working on bash script that asks questions. I was hoping to have a wild card if statement but checks if the user enters in no or just n but for some reason it just does not seem to work.

echo 'Is this a Personal Project? [y/n]'
    read ORG_SWITCH
echo 'Do you want to Open Source this project? [y/n]'
    read PRIVATE

and here is the if statement

if [[ $ORG_SWITCH == *"n"* ]] ; then
    if [[ $PRIVATE == *"n"* ]] ; then

the errors it outputs are [[: not found [[: not found

and I am not sure why. I did see this but and tried a few of the solutions there but maybe I am just not understanding what is going on.

Any help would be appreciated and please feel free to talk down.

Community
  • 1
  • 1
  • 2
    do you start your script with `#!/bin/bash`? (if you don't, maybe another kind of `sh` is used to run it, having no built-in `[[`?) – Anton Kovalenko Jan 24 '13 at 18:01
  • The script has that at the top. It is actually just to long to post the entire thing. But the simplified script below also never validates so I am tracking down the problem. – Ley Missailidis Jan 24 '13 at 18:43

2 Answers2

0

You can use read -n1 -p "Is this a Personal Project? [y/n] " ORG_SWITCH to avoid using the echo and to limit the user input to one char (y or n). Also you can test your condition without the [[ ]] pair:

read -n1 -p "Is this a Personal Project? [y/n] " ORG_SWITCH 
read -n1 -p "Do you want to Open Source this project? [y/n] " PRIVATE

if [ "$ORG_SWITCH" = "n" ]; then 
    if [ "$PRIVATE" = "n" ]; then 
        echo "double no"
    fi
fi

In one bracket conditions ([ ]), always double quote your variables, so if they evaluate to empty, your code won't incur in a syntax error

higuaro
  • 15,730
  • 4
  • 36
  • 43
0

This code works on GNU bash, version 4.2.24

#!/bin/bash
echo 'Is this a Personal Project? [y/n]'
read ORG_SWITCH

if [[ $ORG_SWITCH == *"n"* ]] ; then
    echo "True $ORG_SWITCH"
else
    echo "False $ORG_SWITCH"
fi

echo 'Do you want to Open Source this project? [y/n]'
read PRIVATE
if [[ $PRIVATE == *"n"* ]] ; then
    echo "True $PRIVATE"
else
    echo "False $PRIVATE"
fi

This is the output

Is this a Personal Project? [y/n]
no
True no
Do you want to Open Source this project? [y/n]
no
True no

Am I missing something?

user000001
  • 32,226
  • 12
  • 81
  • 108
  • Tested this and get the same response, as in my exception in GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu). I just assumed it was my code because normally I am the weak chain in the link. – Ley Missailidis Jan 24 '13 at 18:38
  • Also it could be that you are not [*actually* running bash](http://stackoverflow.com/a/12230756/828193). You are executing the script with `./scriptname.sh` right? – user000001 Jan 24 '13 at 19:05
  • Yep just tested on a fresh VM.GNU bash, version 4.2.39. Will keep posting updates till I get to the bottom of it. – Ley Missailidis Jan 24 '13 at 19:09