简体   繁体   English

如何在 shell 脚本文件中执行新的 bash 命令?

[英]How to execute new bash command inside shell script file?

how can I put the follow script to work properly?我怎样才能让下面的脚本正常工作?

1  #/bin/bash
2
3  # some commands
4
5  bash
6  # a lot of commands
7  # ...
8  exit
9  
10 bash
11 # A lot of other commands
12 # ...
13 exit
14 
15 exit 0

The problem is that when executing the script in shell, a new interactive bash shell is entered and the execution stopped right there waiting for standard input.问题是在 shell 中执行脚本时,输入了一个新的交互式 bash shell 并在此处停止执行,等待标准输入。 As the sub-environment may not be BASH in practice, bash command in line 4 and 9 are just examples and that's why I need a new solution other than putting those commands into separate files and invoke.由于子环境实际上可能不是 BASH,因此第 4 行和第 9 行中的bash命令只是示例,这就是为什么我需要一个新的解决方案,而不是将这些命令放入单独的文件中并调用。 Thanks for any help.谢谢你的帮助。

This is the documented behaviour.这是记录在案的行为。 If you execute a mere bash , you get an interactive shell.如果仅执行bash ,您将获得交互式 shell。 If you want to run several bash commands in a child process - which probably is what you want, since you tagged your question subshell , you write如果你想在子进程中运行几个 bash 命令 - 这可能是你想要的,因为你标记了你的问题subshel l ,你写

bash FILENAME

where FILENAME is the name of the file which contains the commands.其中 FILENAME 是包含命令的文件的名称。 If you want to run several bash commands inside the current process, you write one of如果要在当前进程中运行多个 bash 命令,请编写以下命令之一

source FILENAME
. FILENAME

The differences between source and . source. are minor, and you find them described in the bash man page.是次要的,您会在 bash手册页中找到它们的描述。

If you want to run several commands in a child process, without putting them into a separate file, you can do it with如果您想在子进程中运行多个命令,而不是将它们放入单独的文件中,您可以使用

(
   some command
   some other command
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM