我正在 shell 中执行程序并重定向输出:
nice -10 appname > /tmp/output.log 2>&1
我还需要计算执行时间。 time
命令有效,但它的输出也会与主应用程序的输出一起写入 output.log:
time -p nice -10 appname > /tmp/output.log 2>&1
我尝试将应用程序括在括号中,但这会引发错误:
time -p (nice -10 appname > /tmp/output.log 2>&1)
sh: 1: Syntax error: "(" unexpected
如何将 time
输出写入 stdout,同时仍将应用程序输出重定向到文件?
最佳答案
您可以使用虚拟printf
/echo
:
time -p printf "$(nice -10 appname > /tmp/output.log 2>&1)"
或者这里的文档:
time -p sh <<EOF
nice -10 appname > /tmp/output.log 2>&1
EOF
或者使用 Shell 的
-c
选项:
time -p sh -c "nice -10 appname > /tmp/output.log 2>&1"
或者一个函数 (Bash):
nicefun(){
nice -10 appname > /tmp/output.log 2>&1
}
time -p nicefun
或专用脚本:
nice.sh:
#!/bin/sh
nice -10 appname > /tmp/output.log 2>&1
用法:
time -p path/to/nice.sh
关于shell - 具有输出重定向的计时应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245547/