Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash脚本条件_Linux_Bash_Shell_If Statement_Multiple Conditions - Fatal编程技术网

Linux 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

我正在构建一个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 "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是的,这两个文件是相同的。谢谢,我将用双引号更新。