将命令输出保存到变量而不打印到标准输出

时间:2021-02-11 23:53:33

I'm doing a bash script and I'm grabbing the output from this command:

我正在做一个bash脚本,我正在抓取这个命令的输出:

fpings=$(fping -c 1 -t 1 $ips | sort) 

The fpings variable does contain the output from the command, and the actual output of the command is not printed to the shell, but it still writes a line to the shell for each ip pinged.

fpings变量确实包含命令的输出,并且命令的实际输出不会打印到shell,但它仍会为每个ip ping的shell写一行。

There is a switch (-q) to suppress the output (the part that I want) but not any to suppress the part I don't want.

有一个开关(-q)来抑制输出(我想要的部分)但不能抑制我不想要的部分。

How do I get the result from the fpings command without it printing stuff to the shell?

如何在没有将内容打印到shell的情况下从fpings命令获取结果?

2 个解决方案

#1


6  

If you do not want to see the standard error, redirect it to /dev/null:

如果您不想看到标准错误,请将其重定向到/ dev / null:

fpings=$(fping -c 1 -t 1 $ips 2>/dev/null | sort) 

#2


2  

fpings=$( {fping -c 1 -t 1 $ips | sort; } 2>&1 )

should work the {} capture everything and then it redirects both streams (out and err) to just out and saves in the variable

应该工作{}捕获所有内容然后它将两个流(out和err)重定向到刚出来并保存在变量中

#1


6  

If you do not want to see the standard error, redirect it to /dev/null:

如果您不想看到标准错误,请将其重定向到/ dev / null:

fpings=$(fping -c 1 -t 1 $ips 2>/dev/null | sort) 

#2


2  

fpings=$( {fping -c 1 -t 1 $ips | sort; } 2>&1 )

should work the {} capture everything and then it redirects both streams (out and err) to just out and saves in the variable

应该工作{}捕获所有内容然后它将两个流(out和err)重定向到刚出来并保存在变量中