我有一些 bash 脚本如何将其输出的成功或失败存储到文件中?

问题描述

所以我想知道如何在每个脚本中编写某种命令,如果脚本成功与否,将输出并将其附加到文件中。可能吗?

解决方法

您可以通过使用 trap - https://www.linuxjournal.com/content/bash-trap-command

在你的 bash 文件中添加这个命令

# add these lines at the top of your bash script
echo success > your_output_file
trap 'echo failed > your_output_file' ERR
,

您可以对文件使用标准错误和标准输出。例如

echo standard output >> standard_file
echo standard error 2>> standard_error