[英]Shell Script that does chroot and execute commands in chroot
If in shell script I write 如果在shell脚本中我写
chroot /home/mayank/chroot/codebase
cd SBC
when I run this shell script It does go in the chroot
but does not execute the command cd SBC
, when I exit chroot
then it executes cd SBC
. 当我运行这个shell脚本它确实进入了chroot
但没有执行命令cd SBC
,当我退出chroot
然后它执行cd SBC
。
How can I achieve something that does chroot
and execute commands in chroot
through shell script ?? 我怎样才能做到的东西做chroot
中和执行命令chroot
通过shell脚本?
When you run chroot
without telling it what to do, it will try to start chrooted interactive shell session. 当你运行chroot
而不告诉它该做什么时,它将尝试启动chrooted交互式shell会话。 So your script would "pause" at that point and when you are done with that interactive shell session, it continues out of chroot again. 因此,您的脚本将在此时“暂停”,当您完成该交互式shell会话时,它将再次继续chroot。
One of the quick and dirt options would be to abuse here-document, like this: 其中一个快速而肮脏的选择就是滥用 - 文档,如下所示:
chroot /home/mayank/chroot/codebase /bin/bash <<"EOT"
cd /tmp/so
ls -l
echo $$
EOT
Which takes all lines up to EOT
and feeds them into bash
started through chroot
. 将所有线路连接到EOT
并通过chroot
将它们送入bash
。 Those double quotes around "EOT"
should ensure bash passes the content not trying to expand variables and such. 那些围绕"EOT"
双引号应该确保bash传递内容而不是试图扩展变量等。 Hence that echo $$
should be PID of the inner chrooted bash
. 因此, echo $$
应该是内部chrooted bash
PID。
somewhat I found a solution, 有点我找到了解决方案,
chroot /work3/tmp_GU/$build_env/sbcbuild/chroot ./test.sh chroot / work3 / tmp_GU / $ build_env / sbcbuild / chroot ./test.sh
after chroot giving a script there is working fine for me. 在chroot给出一个脚本之后,对我来说工作正常。
test.sh present in the chroot folder. test.sh出现在chroot文件夹中。 All commands in test.sh will be executed in chroot folder. test.sh中的所有命令都将在chroot文件夹中执行。
So basically giving a command after chroot 所以基本上在chroot之后发出命令
man chroot 男人chroot
chroot [OPTION] NEWROOT [COMMAND [ARG]...] chroot [OPTION] NEWROOT [COMMAND [ARG] ...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.