将 bash 脚本的每个命令的 stdout 和 stderr 的输出附加到文件
Append output from both stdout and stderr of every command of a bash script to file
这里
我学会了如何为单个命令执行此操作 cmd
将输出附加到 file.txt
cmd >>file.txt 2>&1
但是我的脚本包含很多语句和命令。
而且我想避免将 >>file.txt 2>&1
附加到每一行。
是否有指令让我默认为每个后续命令执行此操作?
旁注:我正在寻找也适用于 MacOs X 的解决方案 bash
在您的脚本之上,您可以像这样使用 exec
:
#!/bin/bash
# append stdout/stderr to a file
exec >> file.log 2>&1
# script starts here
这里
我学会了如何为单个命令执行此操作 cmd
将输出附加到 file.txt
cmd >>file.txt 2>&1
但是我的脚本包含很多语句和命令。
而且我想避免将 >>file.txt 2>&1
附加到每一行。
是否有指令让我默认为每个后续命令执行此操作?
旁注:我正在寻找也适用于 MacOs X 的解决方案 bash
在您的脚本之上,您可以像这样使用 exec
:
#!/bin/bash
# append stdout/stderr to a file
exec >> file.log 2>&1
# script starts here