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

查看:32
本文介绍了如何在脚本本身内重定向整个 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天全站免登陆