bash 如何从另一个shell脚本调用一个shell脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8352851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call one shell script from another shell script?
提问by Praveen
I have two shell scripts, a.sh
and b.sh
.
我有两个 shell 脚本,a.sh
和b.sh
.
How can I call b.sh
from within the shell script a.sh
?
如何b.sh
从 shell 脚本中调用a.sh
?
回答by Some programmer dude
There are a couple of different ways you can do this:
有几种不同的方法可以做到这一点:
Make the other script executable, add the
#!/bin/bash
line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command;Or call it with the
source
command (alias is.
) like this:source /path/to/script
;Or use the
bash
command to execute it:/bin/bash /path/to/script
;
使另一个脚本可执行,
#!/bin/bash
在顶部添加一行,以及文件到 $PATH 环境变量的路径。然后就可以作为普通命令调用了;或与调用它
source
的命令(别名.
)是这样的:source /path/to/script
;或者使用
bash
命令来执行它:/bin/bash /path/to/script
;
The first and third methods execute the script as another process, so variables and functions in the other script will not be accessible.
The second method executes the script in the first script's process, and pulls in variables and functions from the other script so they are usable from the calling script.
第一种和第三种方法将脚本作为另一个进程执行,因此其他脚本中的变量和函数将无法访问。
第二种方法在第一个脚本的进程中执行脚本,并从另一个脚本中提取变量和函数,以便它们可以从调用脚本中使用。
In the second method, if you are using exit
in second script, it will exit the first script as well. Which will not happen in first and third methods.
在第二种方法中,如果您exit
在第二个脚本中使用,它也会退出第一个脚本。这不会发生在第一种和第三种方法中。
回答by Budha
Check this out.
看一下这个。
#!/bin/bash
echo "This script is about to run another script."
sh ./script.sh
echo "This script has just run another script."
回答by Andrei Krasutski
There are a couple of ways you can do this. Terminal to execute the script:
有几种方法可以做到这一点。执行脚本的终端:
#!/bin/bash
SCRIPT_PATH="/path/to/script.sh"
# Here you execute your script
"$SCRIPT_PATH"
# or
. "$SCRIPT_PATH"
# or
source "$SCRIPT_PATH"
# or
bash "$SCRIPT_PATH"
# or
eval '"$SCRIPT_PATH"'
# or
OUTPUT=$("$SCRIPT_PATH")
echo $OUTPUT
# or
OUTPUT=`"$SCRIPT_PATH"`
echo $OUTPUT
# or
("$SCRIPT_PATH")
# or
(exec "$SCRIPT_PATH")
All this is correct for the path with spaces!!!
所有这些对于带空格的路径都是正确的!!!
回答by Maxim Chetrusca
The answer which I was looking for:
我正在寻找的答案:
( exec "path/to/script" )
As mentioned, exec
replaces the shell without creating a new process. However, we can put it in a subshell, which is done using the parantheses.
如前所述,exec
在不创建新进程的情况下替换外壳。但是,我们可以将它放在子shell 中,这是使用括号完成的。
EDIT:
Actually ( "path/to/script" )
is enough.
编辑:实际上( "path/to/script" )
已经足够了。
回答by Douglas Bonafé
Depends on.
Briefly...
If you want load variables on current console and execute you may use source myshellfile.sh
on your code. Example:
取决于。简而言之...如果您想在当前控制台上加载变量并执行,您可以source myshellfile.sh
在您的代码中使用。例子:
!#/bin/bash
set -x
echo "This is an example of run another INTO this session."
source my_lib_of_variables_and_functions.sh
echo "The function internal_function() is defined into my lib."
returned_value=internal_function()
echo $this_is_an_internal_variable
set +x
If you just want to execute a file and the only thing intersting for you is the result, you can do:
如果您只想执行一个文件,而唯一让您感兴趣的是结果,您可以执行以下操作:
!#/bin/bash
set -x
./executing_only.sh
sh i_can_execute_this_way_too.sh
bash or_this_way.sh
set +x
I hope helps you. Thanks.
我希望对你有帮助。谢谢。
回答by Ranjithkumar T
You can use /bin/sh
to call or execute another script (via your actual script):
您可以使用/bin/sh
调用或执行另一个脚本(通过您的实际脚本):
# cat showdate.sh
#!/bin/bash
echo "Date is: `date`"
# cat mainscript.sh
#!/bin/bash
echo "You are login as: `whoami`"
echo "`/bin/sh ./showdate.sh`" # exact path for the script file
The output would be:
输出将是:
# ./mainscript.sh
You are login as: root
Date is: Thu Oct 17 02:56:36 EDT 2013
回答by Anon
Just add in a line whatever you would have typed in a terminal to execute the script!
e.g.:
只需添加一行您在终端中键入的内容即可执行脚本!
例如:
#!bin/bash
./myscript.sh &
if the script to be executed is not in same directory, just use the complete path of the script.
e.g.:`/home/user/script-directory/./myscript.sh &
如果要执行的脚本不在同一目录下,则使用脚本的完整路径即可。
例如:`/home/user/script-directory/./myscript.sh &
回答by Ghazi Triki
First you have to include the file you call:
首先,您必须包含您调用的文件:
#!/bin/bash
. includes/included_file.sh
then you call your function like this:
然后你像这样调用你的函数:
#!/bin/bash
my_called_function
回答by Nitin Jawarkar
Simple source will help you. For Ex.
简单的来源会帮助你。对于前。
#!/bin/bash
echo "My shell_1"
source my_script1.sh
echo "Back in shell_1"
回答by Shital Shah
If you have another file in same directory, you can either do:
如果您在同一目录中有另一个文件,您可以执行以下操作:
bash another_script.sh
or
或者
source another_script.sh
or
或者
. another_script.sh
When you use bash
instead of source
, the script cannot alter environment of the parent script. The .
command is POSIX standard while source
command is a more readable bash synonym for .
(I prefer source
over .
). If your script resides elsewhere just provide path to that script. Both relative as well as full path should work.
当您使用bash
代替时source
,脚本不能改变父脚本的环境。该.
命令是POSIX标准而source
命令为更可读的bash同义词.
(我喜欢source
过.
)。如果您的脚本驻留在别处,只需提供该脚本的路径。相对路径和完整路径都应该有效。