如何将bst脚本中的stdout和stderr都重定向到文件? [英] How to redirect both stdout and stderr to a file from within a bash script?

查看:73
本文介绍了如何将bst脚本中的stdout和stderr都重定向到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的bash脚本中添加一条命令,将所有stderr和stdout定向到特定文件.来自和许多其他来源,我知道在命令行中我会使用:

I want to add a command to my bash script that directs all stderr and stdout to specific files. From this and many other sources, I know that from the command line I would use:

/path/to/script.sh >> log_file 2>> err_file

但是,我想要在脚本中添加一些东西,类似于这些slurm标志:

However, I want something inside my script, something akin to these slurm flags:

#!/bin/bash
#SBATCH -o slurm.stdout.txt # Standard output log
#SBATCH -e slurm.stderr.txt # Standard error log

<code>

是否有一种方法可以直接从脚本内进行输出,还是需要使用>> ;?log_file 2>>每次我调用脚本时都出现err_file 吗?谢谢

Is there a way to direct output from within a script, or do I need to use >> log_file 2>> err_file every time I call the script? Thanks

推荐答案

您可以在bash脚本的开头使用它:

You can use this at the start of your bash script:

# Redirected Output
exec > log_file 2> err_file

如果文件确实存在,则将其截断为零大小.如果您想附加,请使用以下命令:

If the file does exist it is truncated to zero size. If you prefer to append, use this:

# Appending Redirected Output
exec >> log_file 2>> err_file

如果要将stdout和stderr都重定向到同一文件,则可以使用:

If you want to redirect both stdout and stderr to the same file, then you can use:

# Redirected Output
exec &> log_file
# This is semantically equivalent to
exec > log_file 2>&1

如果您想附加,请使用此:

If you prefer to append, use this:

# Appending Redirected Output
exec >> log_file 2>&1

这篇关于如何将bst脚本中的stdout和stderr都重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆