tags:

views:

165

answers:

6

My code:

    #!/bin/sh
    #filename:choose.sh
    read choose
    [ "$choose" == "y" -o "$choose" == "Y" ] && echo "Yes" && exit 0
    [ "$choose" == "n" -o "$choose" == "N" ] && echo "No"  && exit 0
    echo "Wrong Input" && exit 0

But when I execute

    sh ./choose.sh

terminal prompt me that

   [: 4: n: :Unexpected operator
   [: 5: n: :Unexpected operator

Is there any mistake in my bash script? Thanks!

+2  A: 

you have to use bash instead or rewrite your script using standard sh

sh -c 'test "$choose" = "y" -o "$choose" = "Y"'
aaa
I think it is more convenient using "[ ]" instead of "test".It seemed that "bash ./choose.sh" can solve the problem.
kit.yang
@kit either way, sh is more little portable
aaa
@kit.yang How are brackets more convenient? The historical confusion caused by people failing to realize that [ is a synonym for test and the continuing errors made through omitted whitespace around the brackets hardly compensate for the single character saved. ("if [ $x = 5 ]" vs "if test $x = 5"; 13 chars vs 14).
William Pursell
@ William Pursell Great explanation,+1!
kit.yang
+4  A: 

There is no mistake in your bash script. But you are executing it with sh which has a less extensive syntax ;)

So, run bash ./choose.sh instead :)

WoLpH
Solve my problem.Thanks!Then is there any difference between the command "sh" and "bash"?
kit.yang
`bash` syntax is a superset of `sh` syntax - the `/bin/sh` executable on your system may provide only standard `sh` functionality, in which `[]`-style tests are not included.
Tim
Yes. They are completely different shells. Although, bash was based on and is largely backwards-compatable with sh, and they might actually be the same program on your system, but will still behave differently depending on which name you use. You can have the script run with bash automatically by changing the first line to `#!/bin/bash` and making the file executable, and just running `./choose.sh`.
Tyler McHenry
Great explanation Tyler McHenry, +1!
WoLpH
@ Tyler McHenry It is mainly the difference between bash and sh in testing variable when using brackets.Thanks.
kit.yang
+1  A: 

To execute it with Bash, use #!/bin/bash and chmod it to be executable, then use

./choose.sh
Jack Scott
+12  A: 

POSIX sh doesn't understand == for string equality, as that is a bash-ism. Use = instead.

The other people saying that brackets aren't supported by sh are wrong, btw.

Cirno de Bergerac
Yes,I try to just replace the "==" for "=",the script can also be executive by "sh ./choose.sh".Both bash and sh support the brackets.
kit.yang
+1 This is a better answer than unnecessarily using bash, IMO.
John Kugelman
A: 

In fact the "[" square opening bracket is just an internal shell alias for the test command.

So you can say:

test -f "/bin/bash" && echo "This system has a bash shell"

or

[ -f "/bin/bash" ] && echo "This system has a bash shell"

... they are equivalent in either sh or bash. Note the requirement to have a closing "]" bracket on the "[" command but other than that "[" is the same as "test". "man test" is a good thing to read.

delatbabel
+3  A: 

you can use case/esac instead of if/else

case "$choose" in
  [yY]) echo "Yes" && exit;;
  [nN]) echo "No" && exit;;
  * ) echo "wrong input" && exit;;
esac
ghostdog74