Linux终端输出重定向 [英] Linux terminal output redirection

查看:46
本文介绍了Linux终端输出重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将bash脚本的输出重定向到文件.

I want to redirect the output of a bash script to a file.

脚本为:

#!/bin/bash
echo "recursive c"
for ((i=0;i<=20;i+=1)); do
time ./recursive
done

但是如果我这样运行:

script.sh >> temp.txt

仅./recursive的输出将被捕获到文件中.

only the output of ./recursive will be captured in the file.

我想在文件中捕获时间命令的输出.

I want to capture the output of time command in the file.

推荐答案

STDERR 重定向到 STDOUT :

script.sh >> temp.txt  2>&1

或者如果使用 bash 4.0:

Or if using bash 4.0:

$ script.sh &>> temp.txt

(感谢第二种形式,用于注释评论者.我无法验证,因为我有一个较早的 bash .)

(Thanks for the second form go to commenter ephemient. I can't verify as I have an earlier bash.)

我的测试令人惊讶:

$ time sleep 1 > /dev/null 2>&1

real    0m1.036s
user    0m0.002s
sys     0m0.032s

问题在于重定向已包含在要定时的命令中.这是此测试的解决方案:

The problem is the redirection was included as part of the command to be timed. Here was the solution for this test:

$ (time sleep 1) > /dev/null 2>&1

我不认为这是您的问题的一部分,但这似乎值得一提.

I don't think this is part of your problem, but it seemed worth a mention.

这篇关于Linux终端输出重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆