[英]Pipe Bash command output to stdout and to a variable
我必须找到具有选定权限的文件,并列出它们及其编号。 因此,我想将find
命令的结果传递给shell和下一个命令,该输出我想存储在变量中,以便以后可以很好地显示。 我想吃点东西
for i in "$@"
do
find $filename -perm $i | tee /dev/tty | var=${wc -l}
echo "number of files with $i permission: $var"
done
但var=${wc -l}
部分无效。 请帮忙。
编辑我知道我可以将命令的整个输出放到像这样的变量中
var=$(find $filename -perm $i | tee /dev/tty | wc -l)
但是然后我只需要wc -l
的结果。 我如何从该变量中获得该数字? 是否可以按相反的顺序显示它,先显示编号,然后显示列表?
管道组件在子外壳程序中运行,因此,即使它们确实分配了外壳程序变量(并且语法不正确),这些外壳程序变量也会在管道退出时立即取消设置(因为子外壳程序的寿命与管道程序一样长)。
因此,您需要将整个管道的输出捕获到变量中:
var=$(find "$filename" -perm "$i" | tee /dev/tty | wc -l)
就个人而言,顺便说一句,我会tee
ING至/dev/stderr
或/dev/fd/2
,以避免使行为依赖于TTY是否可用。
使用bash 4.1,自动文件描述符分配使您可以执行以下操作:
exec {stdout_copy}>&1 # make the FD named in "$stdout_copy" a copy of FD 1
# tee over to "/dev/fd/$stdout_copy"
var=$(find "$filename" -perm "$i" | tee /dev/fd/"$stdout_copy" | wc -l)
exec {stdout_copy}>&- # close that copy previously created
echo "Captured value of var: $var"
对于较旧版本的bash,您需要自己分配FD-在以下示例中,我选择文件描述符编号3(因为0、1和2分别保留给stdin,stdout和stderr):
exec 3>&1 # make copy of stdout
# tee to that copy with FD 1 going to wc in the pipe
var=$(find "$filename" -perm "$i" | tee /dev/fd/3 | wc -l)
exec 3>&- # close copy of stdout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.