我编写了一个脚本来管理创建lxc虚拟容器的创建.它由在主机上执行的部分和由容器(虚拟客户)执行的部分组成.如果我可以在文件中编写这两个脚本,并在需要时从中提取第二个脚本(并随后在容器上运行),那将更加优雅.
我依稀记得,在bash中有一种方法可以做到这一点,但我不知道如何找到这个功能的名称以及如何使用它.
我需要这样的东西:
#/bin/bash
# here are commands that are to
# be executed by the host
<declare start of the code chunk that will be exported to file>
# here go commands that
# need to be invoked by the guest
<end of code chunk. Paste the above code into the /var/lib/lxc/<mycontainer>/rootfs/tmp/second-stage.sh>
sudo lxc-attach -n <container name> /tmp/second-stage.sh
Run Code Online (Sandbox Code Playgroud)
您可以使用此文档将多行文本重定向到相关文件.由于您的文档正文包含代码,因此您需要通过引用heredoc分隔符字符串('EOT')来禁用参数替换以避免有趣的副作用:
[...]
cat >/var/lib/lxc/<mycontainer>/rootfs/tmp/second-stage.sh <<'EOT'
# your text/code goes here
EOT
[...]
Run Code Online (Sandbox Code Playgroud)