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

Jie*_*eng 24 bash

我想测试是否有任何输出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;
Run Code Online (Sandbox Code Playgroud)

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

Rin*_*ind 19

这有效:

如果差异 "./helloworld$x.out" "./output/helloworld$x.out" >/dev/null; 然后
    echo "通过 $x";
别的
    echo "失败 $x";
菲

如果您使用变量而不是echo您可以删除else分支:在之前将变量设置为 falseif并保存 2 行代码。

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

some_var="$(diff "./helloworld$x.out" "./output/helloworld$x.out")"
Run Code Online (Sandbox Code Playgroud)

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

rinzwind@discworld:~$ touch 1
rinzwind@discworld:~$ touch 2
rinzwind@discworld:~$ 更多测试
如果差异 1 2 >/dev/null; 然后
    echo "通过 $x";
别的
    echo "失败 $x";
菲
rinzwind@discworld:~$ ./test
通过 

rinzwind@discworld:~$ vi 2
rinzwind@discworld:~$ 更多 2
2
rinzwind@discworld:~$ ./test
失败的 

>/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


Lek*_*eyn 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
Run Code Online (Sandbox Code Playgroud)

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

if diff -q "$file1" "$file2" >/dev/null; then
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

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

[ "$output" = "" ]
[ "$output" == "" ]
[[ "$output" == "" ]]
[[ $output == "" ]]
Run Code Online (Sandbox Code Playgroud)

如果您使用 Bash,建议使用最后两个命令进行字符串比较。否则,第一个和[ -n "$output" ]被推荐。


use*_*own 6

a) command1 的输出可以被捕获

output=$(diff "helloworld$x.out" "output/helloworld$x.out") 
Run Code Online (Sandbox Code Playgroud)

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

 output=`cmd1`
Run Code Online (Sandbox Code Playgroud)

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

 cmd1 > file
 cat file | cmd2 

 output=$(cmd1)
 echo "${output}" | cmd2 
Run Code Online (Sandbox Code Playgroud)

=>

 cmd1 | cmd2 
Run Code Online (Sandbox Code Playgroud)

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

 diff "helloworld$x.out" "output/helloworld$x.out" && echo "success" || echo "failure" 
Run Code Online (Sandbox Code Playgroud)

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

为了保持输出干净,您可以将 'diff' 的输出重定向到任何地方:

 diff "helloworld$x.out" "output/helloworld$x.out" >/dev/null && echo "success" || echo "failure" 
Run Code Online (Sandbox Code Playgroud)

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

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