bash 意外标记“else”附近的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23307842/
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 10:18:48  来源:igfitidea点击:

syntax error near unexpected token `else'

linuxbashshell

提问by Matt

I am new to Linux and Shell scripting so please help me with some patience Could you please let me know if the bash is in my path or not. how to find it in future

我是 Linux 和 Shell 脚本的新手,所以请耐心帮助我如果 bash 是否在我的路径中,请告诉我。以后怎么找

Below is the error

下面是错误

./test.sh: line 5: syntax error near unexpected token `else'
./test.sh: line 5: `else'

below is my PATH /u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/oracle/bin

下面是我的路径 /u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr /X11R6/bin:/home/oracle/bin

which bash /bin/bash

哪个 bash /bin/bash

Below is the simple if statment in shell scripting. I use vi editor

下面是 shell 脚本中的简单 if 语句。我使用 vi 编辑器

#!/bin/bash -x
age=10;
if(age -lt 13)

echo "$age"

else

echo "xx"

Any information is highly appreciable.

任何信息都是高度可观的。

Matt

马特

回答by gangadhars

  1. Don't end a statement with ;. This is not c/c++/java program.
  2. Conditions in script are enclosed with []not ()
  3. Please read how to use ifstatement/syntax. Do google and then try.
  1. 不要以;. 这不是 c/c++/java 程序。
  2. 脚本中的条件用[]not括起来()
  3. 请阅读如何使用if语句/语法。做谷歌然后尝试。

Syntax of if:

的语法if

if [ conditional expression ]
then
    #statements
    ...
fi

So your program is:

所以你的程序是:

#!/bin/bash
age=10
if [ $age -lt 13 ]
then
  echo "something"
else
  echo "some other thing"
fi