我有一个Unix shell脚本test.sh。在脚本中,我想调用另一个shell,然后从子shell执行shell脚本中的其余命令并退出
为了说明这一点:
test.sh
#! /bin/bash
/bin/bash /* create child shell */
<shell-command1>
<shell-command2>
......
<shell-commandN>
exit 0我的目的是从子shell运行shell-command1到shell-commandN。请告诉我怎么做
发布于 2011-01-11 21:36:06
你可以在一个组中设置,比如。
#!/bin/bash
(
Command1
Command2
etc..
)
subshell() {
echo "this is also within a subshell"
}
subshell(和)创建一个子subshell,您可以在其中运行一组命令,否则一个简单的函数就可以了。我不知道(和)是否兼容POSIX。
更新:如果我没理解错的话,你应该在bash中使用-c选项,比如。
/bin/bash -c "Command1 && Command2...." &发布于 2011-01-11 21:36:04
下面是来自http://tldp.org/LDP/abs/html/subshells.html的一个示例:
#!/bin/bash
# subshell-test.sh
(
# Inside parentheses, and therefore a subshell . . .
while [ 1 ] # Endless loop.
do
echo "Subshell running . . ."
done
)
# Script will run forever,
#+ or at least until terminated by a Ctl-C.
exit $? # End of script (but will never get here).https://stackoverflow.com/questions/4658128
复制相似问题