3

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'"

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)

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
NDEthos
  • 507
  • 6
  • 17
  • 1
    the error msg looks like there might not be a space between `elif` and `[`. Looks ok in your code abve. I would move the `;` chars so there is a space separating them from `]`, but it may not be necessary. AND it never hurts to wrapp all variables in dbl-quotes, ie. `"$filename" >> "$jobname$jobnumber"`. Good luck. – shellter Apr 10 '14 at 18:36
  • 1
    Welcome to the bash tag! Check out the [tag wiki](http://stackoverflow.com/tags/bash/info) for good tips on what to do before asking. In this case, [shellcheck](http://www.shellcheck.net) will helpfully pinpoint the problem. – that other guy Apr 10 '14 at 18:36

3 Answers3

5

The elif statements 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...

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
photoionized
  • 5,092
  • 20
  • 23
  • 1
    You can use the null command `:` as a statement. This way, you don't have to even echo anything out. – David W. Apr 10 '14 at 18:36
  • @NicolasEdwards yup, doesn't matter what you put in there, just need something other than a comment – photoionized Apr 10 '14 at 18:36
  • 1
    You should use `[[` instead of `[` or `test` in bash. Otherwise, you should protect your variable with doubles quotes. See : http://stackoverflow.com/questions/19597962/bash-illegal-number/19598570#19598570 – Idriss Neumann Apr 10 '14 at 21:06
4

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

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
tkocmathla
  • 901
  • 11
  • 24
  • You should use `[[` rather than `[` or `test` in bash. Otherwise, you should protect your variable with doubles quotes. See : http://stackoverflow.com/questions/19597962/bash-illegal-number/19598570#19598570 – Idriss Neumann Apr 10 '14 at 21:07
1

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

reece
  • 7,945
  • 1
  • 26
  • 28