如何将sh脚本命令保存在日志文件中?

发布于 2025-02-04 02:58:03 字数 617 浏览 3 评论 0原文

我的命令是:bash test.sh

如何使用test.sh命令将命令上下文'bash test.sh'保存到logfile?

动机:当我使用test.sh进行很长的参数解析时(例如,bash test.sh -a -b -c -d .... - -S ....- -s),我必须用不同的论点解析。因此,我想通过参数解析自动保存这些命令。

请注意,echo bash test.sh -a - b ...> log.txt&&& bash test.sh -a -b ...也可以做。但是,我不想每次重新重新运行test.sh.s.键入如此长的命令。因此,如果有任何简单的方法来解决问题,我会寻求帮助。

我试图修改test.sh作为以下内容,并运行bash test.sh -a -b ...无法保存上下文bash test.sh -a-- -b ... to log.txt。

HISTFILE=~/.bash_history
set -o history
history 10 > ./log.txt
functional commands...

My command is: bash test.sh

How to save the command context 'bash test.sh' to logfile using the command with test.sh?

Motivation: When I use the test.sh with very long argument parsing (e.g., bash test.sh --a --b --c --d .... --s) and I have to rerun the command with different argument parsing. Therefore, I want to automatically save these commands with argument parsing.

Note that echo bash test.sh --a --b ... >log.txt && bash test.sh --a --b ... can do as well. However, I don't want to type in such long command every time I rerun the test.sh. So I ask for help if there is any simple way to fix the problem.

I have tried to modify test.sh as the following, and running bash test.sh --a --b ... can not save the context bash test.sh --a --b ... to log.txt.

HISTFILE=~/.bash_history
set -o history
history 10 > ./log.txt
functional commands...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2025-02-11 02:58:03
  1. echo“!bash”> your_file它将用bash进行最后的cammand,然后将其写入文件,您可能需要>>而不是单个>
  2. 如果无法使用〜/.bash_history(可能是奇怪的,但可能是可能的),那么您可以在调用特定命令echo bash test.sh>>>之前将其写入文件。 your_file&& bash test.sh
  1. echo "!bash" > your_file which would fetch last cammand with bash and write it to file, you might want >> instead of single >
  2. if the ~/.bash_history cannot be used (what would be odd, but possible) then you might write it to out file right before invoking specific command echo bash test.sh >> your_file && bash test.sh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文