Jos*_*osh 7 bash stdout stderr
我想在 bash 脚本中添加一个命令,将所有 stderr 和 stdout 定向到特定文件。从这个和许多其他来源,我知道我会从命令行使用:
/path/to/script.sh >> log_file 2>> err_file
Run Code Online (Sandbox Code Playgroud)
然而,我想要在我的脚本中添加一些类似于这些 slurm 标志的东西:
#!/bin/bash
#SBATCH -o slurm.stdout.txt # Standard output log
#SBATCH -e slurm.stderr.txt # Standard error log
<code>
Run Code Online (Sandbox Code Playgroud)
有没有办法在脚本内直接输出,或者我是否需要在>> log_file 2>> err_file每次调用脚本时使用?谢谢
你可以使用这个:
exec >> file
exec 2>&1
Run Code Online (Sandbox Code Playgroud)
在 bash 脚本的开头。这会将 stdout 和 stderr 附加到您的文件中。
您可以在 bash 脚本的开头使用它:
# Redirected Output
exec > log_file 2> err_file
Run Code Online (Sandbox Code Playgroud)
如果文件确实存在,它将被截断为零大小。如果您喜欢附加,请使用:
# Appending Redirected Output
exec >> log_file 2>> err_file
Run Code Online (Sandbox Code Playgroud)
如果你想将 stdout 和 stderr 重定向到同一个文件,那么你可以使用:
# Redirected Output
exec &> log_file
# This is semantically equivalent to
exec > log_file 2>&1
Run Code Online (Sandbox Code Playgroud)
如果您喜欢附加,请使用:
# Appending Redirected Output
exec >> log_file 2>&1
Run Code Online (Sandbox Code Playgroud)