问题描述
所以我想知道如何在每个脚本中编写某种命令,如果脚本成功与否,将输出并将其附加到文件中。可能吗?
解决方法
您可以通过使用 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