Dr.*_*ini 11 bash string files output
我有一个 bash 脚本,它调用各种命令并打印一些输出(来自被调用的命令本身,例如git pull,以及脚本本身生成的信息性消息,例如Operation took XX minutes.
我想整个输出捕获到文件,从脚本本身:基本上我试图避免需要调用./myscript.sh | tee file.txt的非相关,在这里的原因。
基本上我想做这样的事情:
startCapture
git pull
echo "Text"
other-command
endCapture
Run Code Online (Sandbox Code Playgroud)
我还要求在脚本运行时将输出打印在我的 shell 上。
最终目标是:
./myscript.sh无需额外的 shell 结构即可运行这甚至可能吗?
您始终可以script在脚本内部调用来记录所有内容。
至于在 bash 脚本中同时打印和记录所有内容log.txt:
#!/bin/bash
if [ -z "$SCRIPT" ]
then
/usr/bin/script log.txt /bin/bash -c "$0 $*"
exit 0
fi
echo teste
Run Code Online (Sandbox Code Playgroud)
查看日志log.txt:
$ ./a.sh
Script started, output file is log.txt
teste
Script done, output file is log.txt
$ cat log.txt
Script started on Fri Feb 16 17:57:26 2018
command: /bin/bash -c ./a.sh
teste
Script done on Fri Feb 16 17:57:26 2018
Run Code Online (Sandbox Code Playgroud)
我发现捕获任何会话的所有输出的方法是启动一个新的 bash 会话并发送到日志文件。它对于跟踪不仅仅是一个脚本确实很有用。
bash | tee ~/bash.log #这将保存标准输出,直到 bash 会话结束 bash 2>&1 | bash 2>&1 | tee ~/bash.log #这将保存所有输出,包括错误,直到 bash 会话结束
或者你可以直接编写脚本
./myscript.sh | tee ./myscript.log #this will log only the output of the script.
| 归档时间: |
|
| 查看次数: |
52604 次 |
| 最近记录: |