I'm using bash shell on Amazon Linux. I can't figure out why my script is dying with a syntax error. My script ends like so

chmod 775 $TFILE2
output_file=$( create_test_results_file "$TFILE2" )
(cat $TFILE2; uuencode $output_file $output_file) | mailx -s "$subject" "$to_email"
rm $output_file
echo "sent second email"

rm $TFILE1
rm $TFILE2
echo "removed files"

# If node exited unsuccessfully, verify we alert the process above.
if [ $rc1 != 0 ]; then exit $rc1 fi
if [ $rc2 != 0 ]; then exit $rc2 fi

and when I run it, it prints out the last two echo statements, but then seems to die after that

sent second email
removed files
/home/jboss/.jenkins/jobs/springboard/workspace/automated-tests/nodejs/run_tests.sh: line 86: syntax error: unexpected end of file

Can anyone tell me why its dying with the unexpected end of file error?

  • If there is no code after the block shown here, you could write the last line simply as exit $rc2. – VPfB Apr 21 '17 at 15:33

The short answer is that fi needs to be a separate command, so you need semi-colons:

if [ $rc1 != 0 ]; then exit $rc1; fi
if [ $rc2 != 0 ]; then exit $rc2; fi

You should quote your variables, and since you’re comparing integers, use the appropriate operator:

if [ "$rc1" -ne 0 ]; then exit "$rc1"; fi
if [ "$rc2" -ne 0 ]; then exit "$rc2"; fi

although the behaviour here is slightly different: empty values will be considered equal to 0 (where != would consider them different).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.