bash - if else 语句 bash 脚本
问题描述
我不确定为什么我的脚本总是执行最后一条语句,即使文件 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
要求是在一个或两个文件为空时发送通知
解决方案
这是一个供参考的工作脚本。
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
推荐阅读
- android - 是否可以更改我的 Android 源代码以允许用户调用他们不允许调用的函数?
- google-bigquery - Bigquery 日期时间时间戳函数
- python - 即使列存在,也会发生 KeyError
- python - 如何将花哨的字符串日期索引与 MultiIndex 一起使用
- javascript - React Native 和 Firebase 实时数据库查询给出 null
- c# - 没有这样的元素:无法在 C# 中找到元素 OpenQA.Selenium
- jenkins - 浏览器未在 jenkins 中初始化
- excel - VBA-使用命令按钮根据下拉选择进行特殊复制和粘贴
- r - 在 RStudio 中使用 glmulti 包预测最佳模型的问题
- java - 保存时传递的分离实体持续存在