Mac 上的 bash If-else

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16756132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:30:54  来源:igfitidea点击:

bash If-else on Mac

macosbash

提问by HymanWM

Why such a simple script does not work?

为什么这么简单的脚本不起作用?

  1 for i in {1..6}
  2 do
  3     if[ "$i" -lt "3" ]
  4     then
  5         echo "a"
  6     else
  7         echo "b"
  8     fi
  9 done

or

或者

  1 for i in {1..6}
  2 do
  3     if[ "$i" < 3 ]
  4     then
  5         echo "a"
  6     else
  7         echo "b"
  8     fi
  9 done

Err:

呃:

./test.sh: line 4: syntax error near unexpected token `then'
./test.sh: line 4: `    then'

回答by Marcelo Cantos

You need a space between ifand [.

if和之间需要一个空格[