执行脚本时出现意外的文件结束错误

执行脚本时出现意外的文件结束错误

有人可以帮我调试这个简单的脚本吗?我已经尝试了 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.

另请注意,我对您的脚本进行了一些样式更改。缩进可以使此类错误更容易被发现。

相关内容