Bash:意外标记“then”附近的语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21671343/
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
Bash: Syntax error near unexpected token `then'
提问by user3291870
the error is at line 21...
错误在第 21 行...
if [ $weight1 -gt $weight2 ];
then echo "Weight 2 should be greater than Weight 1."
else
if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ];
then echo "Weight 1 is bike."
else
echo "Weight 1 is either bicycle, car, van or lorry."
fi
fi
回答by Малъ Скрылевъ
I done\'t see the issue, on my tests then
keyword is worked for your case, anyway some noted on your code:
我没有看到这个问题,我的测试then
关键字适用于您的情况,无论如何,您的代码中有一些注释:
Put
then
on the line ofif
operator:if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ]; then
Enclose your variables into quotes:
if [ "$weight1" -ge 20 ] && [ "$weight1" -le 39 ]; then
I recommend to use
-a
key, instead to two calls to[
app:if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
放在运算符
then
的行上if
:if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ]; then
将变量括在引号中:
if [ "$weight1" -ge 20 ] && [ "$weight1" -le 39 ]; then
我建议使用
-a
密钥,而不是两次调用[
应用程序:if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
NOTE:The error Syntax error near unexpected token 'then'
can rise up mostly in a few cases:
注意:Syntax error near unexpected token 'then'
在少数情况下,错误可能会增加:
When you specify
then
keyword outside ofif
case:weight1=21 then echo "Weight 1 is bike." else echo "Weight 1 is either bicycle, car, van or lorry." fi
when youve specified
then
twice:if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then then echo "Weight 1 is bike."
when you've put
then
to improper place, for example, after theelse
operator:else then echo "Weight 1 is either bicycle, car, van or lorry." fi
当您
then
在if
大小写之外指定关键字时:weight1=21 then echo "Weight 1 is bike." else echo "Weight 1 is either bicycle, car, van or lorry." fi
当您指定
then
两次时:if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then then echo "Weight 1 is bike."
当你放在
then
不合适的地方时,例如,在else
操作员之后:else then echo "Weight 1 is either bicycle, car, van or lorry." fi
回答by MLSC
just
只是
chmod +x test.sh
first line:
add #!/bin/bash
第一行:添加 #!/bin/bash
then do:
然后做:
./test.sh
It is working whithout problem
它可以正常工作