gpt4 book ai didi

shell - 从子 shell 运行 shell 命令

转载 作者:行者123 更新时间:2023-12-01 13:42:01 26 4
gpt4 key购买 nike

我有一个 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-commands1 到 shell-commandN。请告诉我如何做到这一点

最佳答案

您可以在一个组中设置,例如。

#!/bin/bash
(
Command1
Command2
etc..
)

subshell() {
echo "this is also within a subshell"
}

subshell

( 和 ) 创建一个子 shell,您可以在其中运行一组命令,否则一个简单的函数就可以了。我不知道 ( 和 ) 是否兼容 POSIX。

更新:如果我对你的评论理解正确,你想使用 -c 选项和 bash,比如。

/bin/bash -c "Command1 && Command2...." &

关于shell - 从子 shell 运行 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com