2014-04-10 71 views
3

所以我想在bash脚本中做一个if else else if语句。截至目前,当我运行此脚本时,出现以下错误消息:“./groupJobs.sh:line 76:语法错误附近出现了令牌elif' ./groupJobs.sh: line 76: elif [$ jobsize -lt $ 2];然后''Bash脚本如果elif elif不工作

我已经在线在多个例子中,在我所做的和别人说的作品中看不到任何区别。

任何帮助,将不胜感激。 (线76是最后的elif语句)

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

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

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

fi 
+1

错误消息看起来像'elif'和'['之间可能没有空格。在你的代码abve中看起来没问题。我会移动';'字符,所以有一个空格将它们从'''分开,但可能没有必要。并且它从不会伤害到将所有变量包装在dbl引号中,即。 '“$ filename”>>“$ jobname $ jobnumber”'。祝你好运。 – shellter

+1

欢迎来到bash标签!请查看[tag wiki](http://stackoverflow.com/tags/bash/info),以获取提示之前要做什么的好提示。在这种情况下,[shellcheck](http://www.shellcheck.net)将有助于查明问题。 –

回答

3

elif语句需要一个实际的声明。把下面的这些语句(或者其他任何你想要的)暂时echo "job creation done"echo "add file to job" ...

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

elif [ $jobsize -ge $2 ]; 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 $2 ]; 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 
+0

'true'也会做... – mata

+1

您可以使用空命令':'作为语句。这样,你甚至不需要回应任何东西。 –

+0

@NicolasEdwards yup,无所谓你放在那里,只需要一些东西,而不是评论 – photoionized

1

猛砸在if/elif的块需要一个命令(例如echo)。评论不算。

4

elif块的主体不能为空(注释不计算在内)。如果您只需要剔除身体,请使用:功能。

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

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

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

fi 
+0

你应该在[bash中使用[[''而不是'['或'test']。否则,你应该用双打引号来保护你的变量。参见:http://stackoverflow.com/questions/19597962/bash-illegal-number/19598570#19598570 –