【发布时间】:2011-07-10 21:33:28
【问题描述】:
我想计算 bash 脚本中命令输出的行数。即
COUNT=ls | wc -l
但我还希望脚本从ls 输出原始输出。如何完成这项工作? (我的实际命令不是ls,它有副作用,所以不能运行两次。)
【问题讨论】:
标签: bash output-redirect
我想计算 bash 脚本中命令输出的行数。即
COUNT=ls | wc -l
但我还希望脚本从ls 输出原始输出。如何完成这项工作? (我的实际命令不是ls,它有副作用,所以不能运行两次。)
【问题讨论】:
标签: bash output-redirect
tee(1) 实用程序可能会有所帮助:
$ ls | tee /dev/tty | wc -l
CHANGES
qpi.doc
qpi.lib
qpi.s
4
info coreutils "tee invocation" 包含以下示例,这可能更能说明tee(1) 的力量:
wget -O - http://example.com/dvd.iso \
| tee >(sha1sum > dvd.sha1) \
>(md5sum > dvd.md5) \
> dvd.iso
下载文件一次,通过两个子进程发送输出(通过bash(1) 进程替换启动)以及tee(1) 的标准输出,它被重定向到一个文件。
【讨论】:
COUNT=$(ls -l | tee /dev/tty | wc -l)
ls | tee tmpfile | first command
cat tmpfile | second command
【讨论】:
Tee 是一个很好的方法,但你可以做一些更简单的事情:
ls > __tmpfile
cat __tmpfile | wc -l
cat __tmpfile
rm __tmpfile
【讨论】: