添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
讲道义的松树  ·  Read multiline json ...·  8 月前    · 
八块腹肌的松鼠  ·  Consume a REST-based ...·  12 月前    · 
坚强的伤疤  ·  Linux ...·  1 年前    · 
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!

marked as duplicate by tripleee bash Users with the  bash badge can single-handedly close questions as duplicates and reopen them as needed. Dec 16 '15 at 6:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

When i executed the same code in Linux and in cygwin i was not getting any errors – Raghuram Aug 5 '10 at 1:11 Cygwin has most likely aliased sh to bash. Some distributions don't offer a true sh anymore. Although some will argue (and I tend to agree) that if you're writing a script to be portable, write it in sh instead of bash. – Wolph Aug 5 '10 at 1:27 Two mistakes: 1. use "=" not "==" for /bin/sh 2. doesn't handle the empty string. Do ${choose}BLAH == yBLAH to fix that. So also this is technically not a duplicate question. – personal_cloud Sep 21 '17 at 17:38

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 :)

Solve my problem.Thanks! Then is there any difference between the command "sh" and "bash"? – kit.yang Aug 5 '10 at 1:10 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 Aug 5 '10 at 1:17 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 Aug 5 '10 at 1:17 This answer is partially wrong. The fully correct one is the now top-voted by Nietzche-jou. sh supports [. == works in Bash built-ins [[, test and [ while a single = is required by POSIX version of [ and test. (Bash manual says the same, BTW.) Requiring Bash because of this is needless harm to portability. Command name is [ or test, ] is just non-necessary, unused last parameter. Many shells implement these as built-ins, as Wikipedia says. – Palec Feb 9 '14 at 23:28 @Wolph By writing partially wrong I meant two things. First, the problem is not in Bash vs sh syntax, both can call [ command correctly. It is in syntax of the [ command parameters, which is not sh’s business. Second, Bash is overkill for such a job, especially when a much simpler solution exists. This is not really wrong as it solves the problem too, but I think it is generally bad advice. This leads beginners to false conclusion that Bash solves their problems. It has many unportable extensions over POSIX-required features. I believe we should lead beginners to writing portable programs. – Palec Feb 11 '14 at 9:32

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.

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 Aug 5 '10 at 1:28 In my specific case, My Teamcity agent is running sh by default, and I do not want to change that (it generates other issues). SH is required here, and I think that this is the correct answer for people that must use sh – Nicolas Oliver Nov 15 '17 at 19:28 +1 This is the right answer. Anyone looking to solve it for sh should just use single '='. Only reading the top voted reply by Wolph wasted my 3 hours :( – Umer Feb 8 at 13:40 I think it is more convenient using "[ ]" instead of "test". It seemed that "bash ./choose.sh" can solve the problem. – kit.yang Aug 5 '10 at 1:12 @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 Aug 8 '10 at 3:17

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"
[ -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.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.3.8.33007