ステータス コードでスクリプトを終了しようとしましたが、「予期しないファイルの終わり」が発生します

ステータス コードでスクリプトを終了しようとしましたが、「予期しないファイルの終わり」が発生します

Amazon Linuxでbashシェルを使用しています。スクリプトが構文エラーで終了する理由がわかりません。スクリプトは次のように終了します。

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"

#Cleanup
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

実行すると、最後の2つのecho文が出力されますが、その後は停止するようです。

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

予期しないファイル終了エラーで終了する理由を誰か教えてもらえますか?

答え1

簡単に言えば、fi別のコマンドにする必要があるので、セミコロンが必要です。

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

変数を引用符で囲み、整数を比較するので適切な演算子を使用します。

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

ただし、ここでの動作は若干異なります。空の値は 0 と等しいと見なされます (では!=異なると見なされます)。

関連情報