Bash 脚本如果 elif elif 不起作用

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

Bash Script if elif elif not working

linuxbash

提问by NDEthos

So I am trying to do an if else if else statement in a bash script. As of now when I run this script I get the following error message "./groupJobs.sh: line 76: syntax error near unexpected token elif' ./groupJobs.sh: line 76:elif [ $jobsize -lt $2 ]; then'"

所以我试图在 bash 脚本中执行 if else if else 语句。截至目前,当我运行此脚本时,我收到以下错误消息“./groupJobs.sh:第 76 行:意外令牌附近的语法错误elif' ./groupJobs.sh: line 76:elif [ $jobsize -lt $2 ]; then'”

I have looked online at multiple examples and cannot see any difference in what I have done and what others say works.

我在网上查看了多个示例,但看不出我所做的和其他人所说的有什么不同。

Any help would be appreciated. (line 76 is the last elif statement)

任何帮助,将不胜感激。(第 76 行是最后一个 elif 语句)

if [ $filesize -ge  ]; then
#goes this way if file is to big to put in a job with anyother files
$filename >> $jobname$jobnumber;

elif [ $jobsize -ge  ]; then
#job is done being created

elif [ $jobsize -lt  ]; then
#add file to job and move on to next file check

fi

采纳答案by photoionized

The elifstatements need an actual statement. Put something like echo "job creation done"and echo "add file to job"in those statements (or whatever else you want) for the time being...

这些elif语句需要一个实际的语句。暂时在这些陈述(或其他任何你想要的)中加入类似echo "job creation done"echo "add file to job"...

if [ $filesize -ge  ]; then
#goes this way if file is to big to put in a job with anyother files
$filename >> $jobname$jobnumber;

elif [ $jobsize -ge  ]; then
#job is done being created
echo "whatever you want"
#the above line just has to be some sort of actual statement

elif [ $jobsize -lt  ]; then
#add file to job and move on to next file check
echo "whatever you want"
#the above line just has to be some sort of actual statement
fi

回答by tkocmathla

The body of an elifblock cannot be empty (a comment doesn't count). If you just need to stub out the body, use the :function.

elif块的主体不能为空(评论不算数)。如果您只需要剔除身体,请使用该:功能。

if [ $filesize -ge  ]; then
#goes this way if file is to big to put in a job with anyother files
$filename >> $jobname$jobnumber;

elif [ $jobsize -ge  ]; then
#job is done being created
:

elif [ $jobsize -lt  ]; then
#add file to job and move on to next file check
:

fi

回答by reece

Bash requires a command (e.g. echo) in the if/elif blocks. Comments do not count.

Bash 需要echo在 if/elif 块中使用命令(例如)。评论不算数。