スクリプト実行時に予期しないファイル終了エラーが発生しました

スクリプト実行時に予期しないファイル終了エラーが発生しました

誰かこの簡単なスクリプトのデバッグを手伝ってくれませんか? 2時間ほど試していますが、うまく動作しないようです。

#!/bin/bash

echo "Search for MMSC or WAP connectivity errors"

sftpErrorCount=$(tail -100 3_ERRORs_log.txt | grep "MMSC_Upload2" | grep "Fail to copy"| awk '{print $1 " " $2" " $3" " $4}'| wc -l)

if [ "$sftpErrorCount" -gt 0 ]
then
sftpErrorDate=$(tail -100 3_ERRORs_log.txt | grep "MMSC_Upload2" | grep "Fail to copy"| awk '{print $1 " " $2" " $3" " $4}'| tail -1)
echo "Error found at around $sftpErrorDate please check FTP logs"

else
echo "No errors found"

スクリプトを実行するとエラーが発生します:

$ sh test_script.sh
Search for MMSC or WAP connectivity errors
test_script.sh: line 14: syntax error: unexpected end of file

答え1

if ステートメントを という単語で閉じる必要がありますfi

#!/bin/bash

echo "Search for MMSC or WAP connectivity errors"

sftpErrorCount=$(tail -100 3_ERRORs_log.txt | grep "MMSC_Upload2" |
   grep "Fail to copy"| awk '{print $1 " " $2" " $3" " $4}'| wc -l)

if [ "$sftpErrorCount" -gt 0 ] ; then
    sftpErrorDate=$(tail -100 3_ERRORs_log.txt | grep "MMSC_Upload2" |
      grep "Fail to copy"| awk '{print $1 " " $2" " $3" " $4}'| tail -1)
    echo "Error found at around $sftpErrorDate please check FTP logs"
else
    echo "No errors found"
fi
# ^ This closes the block.

また、スクリプトにいくつかのスタイル変更を加えたことにも注意してください。インデントを使用すると、このようなバグを見つけやすくなります。

関連情報