对 exec 重定向到 /dev/null 感到困惑,我做得对吗?
Confused about exec redirection to /dev/null, am I doing it correctly?
背景
我在回答的时候POSIX shell scripting, please avoid any Bashism在训练自己。谢谢。
我现在知道了,多亏了 Kusalananda's answer,如何确定我的脚本是否 运行 交互,即当它连接到 stdin
。
问题
因为我很少使用exec
(man page),所以我不确定我是否正确地做了下面的想法?请详细说明。
如果脚本是运行ning:
interactively:将错误信息输出到stderr
,否则在默认环境设置中运行
非交互:将所有输出重定向到/dev/null
在这个例子中(最后我可能想将 stdout
和 stderr
重定向到实际文件)。
代码
# test if file descriptor 0 = standard input is connected to the terminal
running_interactively () { [ -t 0 ]; }
# if not running interactively, then redirect all output from this script to the black hole
! running_interactively && exec > /dev/null 2>&1
print_error_and_exit ()
{
# if running interactively, then redirect all output from this function to standard error stream
running_interactively && exec >&2
...
}
看来我已经很接近了。由于 running_interactively
函数按预期工作,我能够继续重定向到文件,如下所示。
我已编辑此答案以提供更多代码以供重复使用。
#!/bin/sh
is_running_interactively ()
# test if file descriptor 0 = standard input is connected to the terminal
{
# test if stdin is connected
[ -t 0 ]
}
redirect_output_to_files ()
# redirect stdout and stderr
# - from this script as a whole
# - to the specified files;
# these are located in the script's directory
{
# get the path to the script
script_dir=$( dirname "[=10=]" )
# redirect stdout and stderr to separate files
exec >> "${script_dir}"/stdout \
2>> "${script_dir}"/stderr
}
is_running_interactively ||
# if not running interactively, add a new line along with a date timestamp
# before any other output as we are adding the output to both logs
{
redirect_output_to_files
printf '\n'; printf '\n' >&2
date; date >&2
}
print_error_and_exit ()
{
# if running interactively redirect all output from this function to stderr
is_running_interactively && exec >&2
...
}
背景
我在回答的时候POSIX shell scripting, please avoid any Bashism在训练自己。谢谢。
我现在知道了,多亏了 Kusalananda's answer,如何确定我的脚本是否 运行 交互,即当它连接到 stdin
。
问题
因为我很少使用exec
(man page),所以我不确定我是否正确地做了下面的想法?请详细说明。
如果脚本是运行ning:
interactively:将错误信息输出到
stderr
,否则在默认环境设置中运行非交互:将所有输出重定向到
/dev/null
在这个例子中(最后我可能想将stdout
和stderr
重定向到实际文件)。
代码
# test if file descriptor 0 = standard input is connected to the terminal
running_interactively () { [ -t 0 ]; }
# if not running interactively, then redirect all output from this script to the black hole
! running_interactively && exec > /dev/null 2>&1
print_error_and_exit ()
{
# if running interactively, then redirect all output from this function to standard error stream
running_interactively && exec >&2
...
}
看来我已经很接近了。由于 running_interactively
函数按预期工作,我能够继续重定向到文件,如下所示。
我已编辑此答案以提供更多代码以供重复使用。
#!/bin/sh
is_running_interactively ()
# test if file descriptor 0 = standard input is connected to the terminal
{
# test if stdin is connected
[ -t 0 ]
}
redirect_output_to_files ()
# redirect stdout and stderr
# - from this script as a whole
# - to the specified files;
# these are located in the script's directory
{
# get the path to the script
script_dir=$( dirname "[=10=]" )
# redirect stdout and stderr to separate files
exec >> "${script_dir}"/stdout \
2>> "${script_dir}"/stderr
}
is_running_interactively ||
# if not running interactively, add a new line along with a date timestamp
# before any other output as we are adding the output to both logs
{
redirect_output_to_files
printf '\n'; printf '\n' >&2
date; date >&2
}
print_error_and_exit ()
{
# if running interactively redirect all output from this function to stderr
is_running_interactively && exec >&2
...
}