shell - 具有输出重定向的计时应用程序

标签 shell output

我正在 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/

相关文章:

java - 如何使用 Bourne Shell 将输入从 Java 程序传输到另一个程序

sql-server - 从 Powershell 调用存储过程后将其输出存储在文件中

linux - 系统(命令)在生产服务器上返回 false : Ruby on Rails

android - 在 Windows 上,如何在 shell 后台运行程序,并在程序运行时关闭 shell?

c - 用 C 编写 shell,在某处关闭 stdin

bash - awk gensub 函数使用问题

java - 使用 System.out.println 的简单程序没有输出

actionscript-3 - 将声音输出到耳机和扬声器

c++ - 在 C++ 中读/写二进制文件

python - 简单的 python 代码不起作用