bash 检查没有传递的参数时出现意外标记“fi”附近的语法错误

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

syntax error near unexpected token `fi' while checking no of arguments passed

bashshell

提问by Bharat

I am new to shell scripting and created a script to check arguments are passed or not using if else condition . But it is always giving a error 'syntax error near unexpected token `fi' '

我是 shell 脚本的新手,并创建了一个脚本来检查参数是否被传递或不使用 if else 条件。但它总是给出一个错误“靠近意外标记‘fi’语法错误

is it always required to use ';'after if condition brackets.
I runned it online on http://www.compileonline.com/execute_bash_online.phpit is working well but not on my system(Centos 6.2). Am i missing something in env settings or is it something else.

是否总是需要使用';' 在 if 条件括号之后。
我在http://www.compileonline.com/execute_bash_online.php 上在线运行它,它运行良好,但在我的系统上运行(Centos 6.2)。我是在环境设置中遗漏了什么还是别的什么。

Code

代码

#!/bin/bash
echo "print a message"
if [ $# -eq 1 ]; then
    echo "Argument are passed to shell script"
else
    echo "No arguments are passed to shell script"
fi

Error message

错误信息

[root@local shell_scripts]# sh test.sh 12 13
test.sh: line 7: syntax error near unexpected token `fi'
test.sh: line 7: `fi'
[root@local shell_scripts]#

my env details

我的环境细节

[root@local shell_scripts]# env
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.1.17 49656 22
PERL5LIB=/home/bharat/perl5/lib/perl5/x86_64-linux-thread-multi:/home/bharat/perl5/lib/perl5
PERL_MB_OPT=--install_base /home/bharat/perl5
SSH_TTY=/dev/pts/6
USER=bharat
PATH=/home/bharat/perl5/bin:/usr/kerberos/sbin:/home/bharat/perl5/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/bharat/bin
MAIL=/var/spool/mail/bharat
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SHLVL=4
PERL_LOCAL_LIB_ROOT=/home/bharat/perl5
LOGNAME=bharat
CVS_RSH=ssh
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
PERL_MM_OPT=INSTALL_BASE=/home/bharat/perl5
_=/bin/env
[root@local shell_scripts]#

回答by sshilovsky

Answering the first of your questions: ;is not required after ifcondition. Syntactically, thenshould go to a separate line. If thenis in the 1st line, you use ;. The following code is correct:

回答您的第一个问题:;if条件之后不需要。从语法上讲,then应该转到单独的行。如果then在第一行,则使用;. 以下代码是正确的:

if [ $# -eq 1 ]
then
    echo ok
fi

Also, these brackets are not actually ifcondition brackets. If condition is any valid bash command, and brackets are equivalent to test, see man testfor ref.

此外,这些括号实际上不是if条件括号。如果条件是任何有效的 bash 命令,并且括号等效于test,请参阅man test参考资料。

The following are valid ifstatement beginnings:

以下是有效的if语句开头:

if cp a.txt b.txt ; then ...
if test $# -eq 1 ; then ...

The second is equivalent to your ifstatement.

第二个相当于你的if陈述。

回答by sshilovsky

Works for me:

对我有用:

$ cat > f.sh
#!/bin/bash
echo "print a message"
if [ $# -eq 1 ]; then
    echo "Argument are passed to shell script"
else
    echo "No arguments are passed to shell script"
fi
$ bash f.sh 
print a message
No arguments are passed to shell script