如何将命令(例如diff)的输出保存到变量中


21

我想测试diff(如果文件相同)是否有任何输出(如果没有)echo "Passed $x" else echo "Failed $x"。我想出了一些中间步骤(将diff的输出保存到文件中,然后从文件中读取)

diff "./helloworld$x.out" "./output/helloworld$x.out" > tmp.txt;
output="`cat tmp.txt`";

if [ "$output" = "" ];
  then
    echo "Passed $x";
  else
    echo "Failed $x";
  fi;

我确定代码可以改进吗?主要问题是:是否可以将输出diff直接保存到变量中?

Answers:


18

这有效:

如果diff“ ./helloworld$x.out”“ ./output/helloworld$x.out”> / dev / null; 然后
    回显“ Passed $ x”;
其他
    回显“失败的$ x”;
科幻

如果您使用变量而不是变量,则echo可以删除else分支:将变量设置为false,if然后保存2行代码。

如果要实际将结果放入变量中,请使用:

some_var="$(diff "./helloworld$x.out" "./output/helloworld$x.out")"

包括我的测试以查看它是否确实有效:

rinzwind @ discworld:〜$ touch 1
rinzwind @ discworld:〜$ touch 2
rinzwind @ discworld:〜$更多测试
如果diff 1 2> / dev / null; 然后
    回显“ Passed $ x”;
其他
    回显“失败的$ x”;
科幻
rinzwind @ discworld:〜$ ./测试
已通过 

rinzwind @ discworld:〜$ vi 2
rinzwind @ discworld:〜$更多2
2
rinzwind @ discworld:〜$ ./测试
失败的 

在这方面>/dev/null>/dev/null 2>&1将输出发送到 标准命令>/dev/null2>&1并将标准错误发送到&1该命令前面的同一文件(表示“使用第一个参数”)(因此它也使用/dev/null)。

旁注:sdiff将显示并排diff清单:

差异1 2 
1 1
2 2
3 3
4 4
5 5
7 7
                                  > 8
9 9
10 10

谢谢,它的工作原理,但我不明白... > /dev/null在这种情况下做什么。diff的输出将转到/ dev / null,但这不是什么吗?
孟梦

1
diff将输出屏幕上文件之间的所有差异,并且> / dev / null确保命令的输出重定向到遗忘。if仍基于输出执行。
Rinzwind

1
if语句检查diff命令的退出代码。0表示未发现差异,而1表示存在一个或多个差异。
Knut Eldhuset 2012年

17

diff 甚至可以使用以下代码完全禁止输出,除了“文件/ bin / bash和/ bin / sh不同”消息。

file1="./helloworld$x.out"
file2="./output/helloworld$x.out"

if diff -q "$file1" "$file2"; then
    echo "Passed $x"
else
    echo "Failed $x"
fi

如果您甚至想隐藏该消息,则必须> /dev/null在diff命令之后追加以隐藏的输出diff

if diff -q "$file1" "$file2" >/dev/null; then

/dev/null 是一个充当黑洞的特殊文件,如果您对其进行写操作,它将消失,如果您从中读取信息,您将一无所获。

请注意,bash不需要;结尾行。

对于原始问题,要将程序的输出保存在变量中:

file1="./helloworld$x.out"
file2="./output/helloworld$x.out"
output="$(diff -q "$file1" "$file2")"

# the quotes are mandatory, this checks whether $output is empty or not
if [ -n "$output" ]; then
    echo "Passed $x"
else
    echo "Failed $x"
fi

检查变量是否为空的替代方法:

[ "$output" = "" ]
[ "$output" == "" ]
[[ "$output" == "" ]]
[[ $output == "" ]]

如果使用的是Bash,建议使用最后两个命令进行字符串比较。否则,[ -n "$output" ]建议先使用。


6

a)可以捕获command1的输出

output=$(diff "helloworld$x.out" "output/helloworld$x.out") 

或带有反引号的字符,但不鼓励使用,因为您无法嵌套它们,并且它们可能很难与撇号区分开,具体取决于字体:

 output=`cmd1`

b)您可以直接使用管道来代替写入文件,然后读取该文件(或获取输出,然后回显该文件):

 cmd1 > file
 cat file | cmd2 

 output=$(cmd1)
 echo "${output}" | cmd2 

=>

 cmd1 | cmd2 

但是在您的示例中,您对程序的输出不感兴趣,但是对程序的结果感兴趣-它起作用了吗?

 diff "helloworld$x.out" "output/helloworld$x.out" && echo "success" || echo "failure" 

阅读有关&&和||的用法 搜索“快捷方式AND和快捷方式OR”。

为了保持输出整洁,您可以将'diff'的输出重定向到无处:

 diff "helloworld$x.out" "output/helloworld$x.out" >/dev/null && echo "success" || echo "failure" 

为了获得成功并在以后进行评估,请将最后一个命令的结果存储在带有$?的变量中:

diff "helloworld$x.out" "output/helloworld$x.out" >/dev/null
result=$?
# do something else
case $result in 
   0) echo success ;;
   *) echo failure ;;
esac

3

如果您想知道两个文件相同还是不同(但不在乎实际的区别是什么),cmp则更合适。

if cmp -s file1 file2; then echo "Equal"; else echo "Not equal"; fi
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.