如何在脚本本身内重定向整个Shell脚本的输出? [英] How to redirect output of an entire shell script within the script itself?

查看:296
本文介绍了如何在脚本本身内重定向整个Shell脚本的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Bourne shell脚本的所有输出重定向到某个位置,但脚本本身内部包含shell命令?

Is it possible to redirect all of the output of a Bourne shell script to somewhere, but with shell commands inside the script itself?

重定向单个命令的输出很容易,但是我想要更多类似的东西:

Redirecting the output of a single command is easy, but I want something more like this:

#!/bin/sh
if [ ! -t 0 ]; then
    # redirect all of my output to a file here
fi

# rest of script...

含义:如果脚本是非交互式运行的(例如cron),请将所有输出保存到文件中.如果要从Shell交互运行,则将输出照常转到stdout.

Meaning: if the script is run non-interactively (for example, cron), save off the output of everything to a file. If run interactively from a shell, let the output go to stdout as usual.

我想对通常由FreeBSD定期实用程序运行的脚本执行此操作.这是日常运行的一部分,通常我并不希望每天都在电子邮件中看到它,因此我没有发送它.但是,如果这个特定脚本中的某些内容失败了,那对我来说很重要,我希望能够捕获并通过电子邮件发送这部分日常工作的输出.

I want to do this for a script normally run by the FreeBSD periodic utility. It's part of the daily run, which I don't normally care to see every day in email, so I don't have it sent. However, if something inside this one particular script fails, that's important to me and I'd like to be able to capture and email the output of this one part of the daily jobs.

更新:Joshua的回答是即时的,但我也想在整个脚本中保存和还原stdout和stderr,这是这样完成的:

Update: Joshua's answer is spot-on, but I also wanted to save and restore stdout and stderr around the entire script, which is done like this:

# save stdout and stderr to file 
# descriptors 3 and 4, 
# then redirect them to "foo"
exec 3>&1 4>&2 >foo 2>&1

# ...

# restore stdout and stderr
exec 1>&3 2>&4

推荐答案

正在解决该问题.

#...part of script without redirection...

{
    #...part of script with redirection...
} > file1 2>file2 # ...and others as appropriate...

#...residue of script without redirection...

括号"{...}"提供I/O重定向的单位.括号必须出现在可能出现命令的位置-简单地说,在行的开头或分号之后. (是的,可以更加精确;如果您想打怪,请告诉我.)

The braces '{ ... }' provide a unit of I/O redirection. The braces must appear where a command could appear - simplistically, at the start of a line or after a semi-colon. (Yes, that can be made more precise; if you want to quibble, let me know.)

您正确的是,您可以使用显示的重定向来保留原始的stdout和stderr,但是如果稍后对重定向的代码进行范围划分,那么以后不得不维护脚本的人通常会更容易理解发生了什么,这通常会更简单.

You are right that you can preserve the original stdout and stderr with the redirections you showed, but it is usually simpler for the people who have to maintain the script later to understand what's going on if you scope the redirected code as shown above.

Bash手册的相关部分为分组命令 I/O重定向. POSIX Shell规范的相关部分为复合命令 I/O重定向. Bash有一些额外的表示法,但与POSIX Shell规范类似.

The relevant sections of the Bash manual are Grouping Commands and I/O Redirection. The relevant sections of the POSIX shell specification are Compound Commands and I/O Redirection. Bash has some extra notations, but is otherwise similar to the POSIX shell specification.

这篇关于如何在脚本本身内重定向整个Shell脚本的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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