if else语句bash脚本

时间:2018-08-15 10:28:32

标签: bash if-statement

即使文件file1和file2不为空,我也不知道为什么我的脚本始终执行最后一个语句:

SERVER_NAME=$HOSTNAME
SENDER="Admin"
SUBJECT="Notification from "$SERVER_NAME""
RECEIVER="admin@mycompany.com"
file1=/home/first_file.txt 
file2=/home/second_file.txt
TEXT1="The file $file1 is empty"
TEXT2="The file $file2 is empty"
TEXT3="Both files are empty"


if [ -s $file1 && -s $file2 ]
     then
        echo "files are good"  

    elif [[ -s $file1 ]]
     then
        echo -e "$TEXT2" | mail -s "$SUBJECT" "$RECEIVER" &>/dev/null

    elif [[ -s $file2 ]]
    then
        echo -e "$TEXT1" | mail -s "$SUBJECT" "$RECEIVER"  &>/dev/null

    else
        echo -e "$TEXT3" | mail -s "$SUBJECT" "$RECEIVER"  &>/dev/null

    fi

要求是在一个或两个文件为空的情况下发送通知

1 个答案:

答案 0 :(得分:-1)

这是一个可供参考的工作脚本。

SERVER_NAME=$HOSTNAME
SENDER="Admin"
SUBJECT="Notification from "$SERVER_NAME""
RECEIVER="admin@mycompany.com"
file1=/home/first_file.txt 
file2=/home/second_file.txt
TEXT1="The file $file1 is empty"
TEXT2="The file $file2 is empty"
TEXT3="Both files are empty"


if [[ -s $file1 && -s $file2 ]]
     then
        echo "files are good"  

    elif [[ -s $file1 ]]
     then
        echo -e "$TEXT2" | mail -s "$SUBJECT" "$RECEIVER" &>/dev/null

    elif [[ -s $file2 ]]
    then
        echo -e "$TEXT1" | mail -s "$SUBJECT" "$RECEIVER"  &>/dev/null

    else
        echo -e "$TEXT3" | mail -s "$SUBJECT" "$RECEIVER"  &>/dev/null

    fi