Linux Bash脚本条件
我正在构建一个bash脚本,以根据上一个命令发送电子邮件。我似乎有困难。在脚本外部,该命令可以正常工作,但当将其放入脚本时,它不会给出所需的结果 以下是脚本片段:Linux Bash脚本条件,linux,bash,shell,if-statement,multiple-conditions,Linux,Bash,Shell,If Statement,Multiple Conditions,我正在构建一个bash脚本,以根据上一个命令发送电子邮件。我似乎有困难。在脚本外部,该命令可以正常工作,但当将其放入脚本时,它不会给出所需的结果 以下是脚本片段: grep -vFxf /path/to/first/file /path/to/second/file > /path/to/output/file.txt if [ -s file.txt ] || echo "file is empty"; then swaks -t "1@email.com" -f
grep -vFxf /path/to/first/file /path/to/second/file > /path/to/output/file.txt
if [ -s file.txt ] || echo "file is empty";
then
swaks -t "1@email.com" -f "norply@email.com" --header "Subject: sample" --body "Empty"
else
swaks -t "1@email.com" -f "norply@email.com" --header "subject: sample" --body "Not Empty"
fi
我在脚本外部运行命令,可以看到有数据,但当我在脚本中添加命令时,我得到的是空输出。请告知。提前谢谢。您的条件将始终为真,因为如果
[-s file.txt]
失败,|
-列表的退出状态为echo
的退出状态,几乎可以保证为0。您希望将echo
移出条件并放入if
语句的主体中。(为了进一步简化,只需将body设置为变量,并在if
完成后调用swaks
if [ -s file.txt ];
then
body="Not Empty"
else
echo "file is empty"
body="Empty"
fi
swaks -t "1@email.com" -f "norply@email.com" --header "subject: sample" --body "$body"
如果创建
file.txt
的唯一原因是检查它是否为空,则可以直接将grep
命令置于If
条件下:
if grep -vFxfq /atph/to/first/file /path/to/second/file; then
body="Not Empty"
else
echo "No output"
body="Empty"
fi
swaks -t "1@email.com" -f "norply@email.com" --header "subject: sample" --body "$body"
您的条件始终为true,因为如果
[-s file.txt]
失败,|
-列表的退出状态是echo
的退出状态,这几乎可以保证为0。您希望将echo
移出条件并移到if
语句的主体中。(为了进一步简化,只需将body设置为变量,并在if
完成后调用swaks
if [ -s file.txt ];
then
body="Not Empty"
else
echo "file is empty"
body="Empty"
fi
swaks -t "1@email.com" -f "norply@email.com" --header "subject: sample" --body "$body"
如果创建
file.txt
的唯一原因是检查它是否为空,则可以直接将grep
命令置于If
条件下:
if grep -vFxfq /atph/to/first/file /path/to/second/file; then
body="Not Empty"
else
echo "No output"
body="Empty"
fi
swaks -t "1@email.com" -f "norply@email.com" --header "subject: sample" --body "$body"
[-s file.txt]
和/path/to/output/file.txt
是否相同?(您应该始终在[…]
中使用双引号-授予file.txt
没有区别,但如果它是一个变量-双引号)请提供.@DavidC.Rankin是的,这两个文件是相同的。谢谢,我将用双引号更新。是否[-s file.txt]
和/path/to/output/file.txt
相同?(您应该始终在[…]中使用双引号)
--授予file.txt
没有区别,但如果是变量-双引号)请提供.@DavidC.Rankin是的,这两个文件是相同的。谢谢,我将用双引号更新。