【问题标题】:Bash script of history command redirection to a file in a docker container历史命令重定向到 docker 容器中的文件的 Bash 脚本
【发布时间】:2020-06-22 16:31:58
【问题描述】:

所以我有这个脚本在我的主机上运行良好

#!/bin/bash -i
history > history.txt

但是当我在我的 docker 容器中执行它时,我的 history.txt 看起来像这样:(它将我的脚本复制到文件中,如果我重新执行该脚本,它会再次执行相同的操作!)

1  #!/bin/bash -i
2  history > history.txt

顺便说一句,当我直接从终端执行“history > history.txt”时,它工作正常。

【问题讨论】:

  • 你能分享你的 Dockerfile 吗?

标签: linux bash docker


【解决方案1】:

使用bash -i,已执行的行会记录在历史记录中。您执行脚本中的行,因此脚本中的行显示在 history.txt 中。

您也可以在主机上的历史记录末尾看到这一点:

$ tail -n 2 history.txt
29419  #!/bin/bash -i
29420  history > history.txt

在一个干净的 Docker 实例中,显然没有以前的历史记录,所以你得到的只有这两行:

$ tail -n 2 history.txt
    1  #!/bin/bash -i
    2  history > history.txt

没有办法从内部直接访问 Docker 之外的 bash 历史记录,所以如果你想这样做,你应该先将它转储到一个文件中,然后让 Docker 访问这个文件。

【讨论】:

  • 我删除了 -i 并且脚本没有输出任何内容,我在执行脚本之前在终端中传递了一些命令,并且“历史”命令显示了这些命令
  • 非交互式 shell 没有历史记录。您可以通过bash -c 'history' 看到这一点。新的交互式 shell 实例从 .bash_history 加载历史记录,默认情况下仅在注销时保存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多