我想做这种形式的事情:
one() {
redirect_stderr_to '/tmp/file_one'
# function commands
}
two() {
redirect_stderr_to '/tmp/file_two'
# function commands
}
one
two
这将连续运行,one
并two
重定向stderr
到相应的文件。工作等效项为:
one() {
# function commands
}
two() {
# function commands
}
one 2> '/tmp/file_one'
two 2> '/tmp/file_two'
但这有点丑陋。我宁愿将所有重定向指令都包含在函数本身中。这样会更容易管理。我觉得这可能无法实现,但请确保。
最简单,最可靠的方法是使用函数级重定向:请注意,在下面的关闭之后,如何将重定向命令应用于整个函数,}
并且将其范围限定于每个函数(无需重置):
# Define functions with redirected stderr streams.
one() {
# Write something to stderr:
echo one >&2
} 2> '/tmp/file_one'
two() {
# Write something to stderr:
echo two >&2
} 2> '/tmp/file_two'
one
two
# Since the function-level redirections are localized to each function,
# this will again print to the terminal.
echo "done" >&2
文档链接(感谢@gniourf_gniourf):
sh
(仅POSIX功能)脚本中使用它。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句