从子 shell 运行 shell 命令 [英] Run shell command from child shell

查看:65
本文介绍了从子 shell 运行 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Unix shell 脚本 test.sh.在脚本中,我想调用另一个 shell,然后从子 shell 执行 shell 脚本中的其余命令并退出

I have a Unix shell script test.sh. Within the script i would like to invoke another shell and then execute the rest of the commands in the shell script from the child shell and exit

说清楚:

#! /bin/bash

/bin/bash /* create child shell */

<shell-command1>
<shell-command2>
......

<shell-commandN>

exit 0

我的意图是从子 shell 运行 shell-commands1 到 shell-commandN.请告诉我如何做到这一点

What my intention is to run the shell-commands1 to shell-commandN from the child shell. Kindly tell me how to do this

推荐答案

您可以在一个组中进行设置,例如.

You can setup in a group, like.

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

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

subshell

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

( and ) creates a subshell in which you run a group of commands, otherwise a simple function will do. I don't know if ( and ) is POSIX compatible.

更新:如果我正确理解您的评论,您希望将 -c 选项与 bash 一起使用,例如.

Update: If I understand your comment correctly, you want to be using -c option with bash, like.

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

这篇关于从子 shell 运行 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆