我有两个shell脚本,a.sh和b.sh。
我如何从shell脚本a.sh调用b.sh ?
我有两个shell脚本,a.sh和b.sh。
我如何从shell脚本a.sh调用b.sh ?
有几种不同的方法可以做到这一点:
使用chmod a+x /path/to/file(Nathan Lilienthal的注释)使另一个脚本可执行,添加#!/bin/bash行(称为shebang),以及文件到$ path环境变量的路径。然后你可以调用它作为一个普通的命令; 或者用source命令(它是.的别名)调用它,就像这样: 源/道路/ /脚本 或者使用bash命令来执行它,比如: /bin/bash /道路/ /脚本
第一种和第三种方法将脚本作为另一个进程执行,因此另一个脚本中的变量和函数将不可访问。 第二种方法在第一个脚本的进程中执行脚本,并从另一个脚本中引入变量和函数(因此它们可以从调用脚本中使用)。
在第二种方法中,如果你在第二个脚本中使用exit,它也会退出第一个脚本。这在第一种和第三种方法中是不会发生的。
看看这个。
#!/bin/bash
echo "This script is about to run another script."
sh ./script.sh
echo "This script has just run another script."
pathToShell="/home/praveen/"
chmod a+x $pathToShell"myShell.sh"
sh $pathToShell"myShell.sh"
你可以使用/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
输出将是:
# ./mainscript.sh
You are login as: root
Date is: Thu Oct 17 02:56:36 EDT 2013
假设新文件为“/home/satya/app/app_specific_env”,文件内容如下
#!bin/bash
export FAV_NUMBER="2211"
将该文件引用附加到~/。bashrc文件(
source /home/satya/app/app_specific_env
当您重新启动机器或重新登录时,尝试在终端中echo $FAV_NUMBER。它将输出该值。
以防万一,如果您想立即看到效果,请使用source ~/。在命令行中使用Bashrc。
首先,你必须包含你调用的文件:
#!/bin/bash
. includes/included_file.sh
然后像这样调用函数:
#!/bin/bash
my_called_function
我一直在寻找的答案:
( exec "path/to/script" )
如前所述,exec替换shell而不创建新进程。但是,我们可以把它放在一个子壳层中,这是用副函数完成的。
编辑: 实际上("path/to/script")就足够了。
只需在一行中添加您在终端中输入的任何内容来执行脚本! 例如:
#!bin/bash
./myscript.sh &
如果要执行的脚本不在同一目录下,请使用脚本的完整路径。 例如:“/ home / user /脚本目录/。/ myscript.sh &
取决于。 短暂的…… 如果你想在当前控制台加载变量并执行,你可以在你的代码中使用myshell .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
如果你只想执行一个文件,你唯一感兴趣的是结果,你可以这样做:
#!/bin/bash
set -x
./executing_only.sh
bash i_can_execute_this_way_too.sh
bash or_this_way.sh
set +x
#!/bin/bash
# Here you define the absolute path of your script
scriptPath="/home/user/pathScript/"
# Name of your script
scriptName="myscript.sh"
# Here you execute your script
$scriptPath/$scriptName
# Result of script execution
result=$?
有几种方法可以做到这一点。执行脚本的终端:
#!/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")
所有这些都是正确的路径与空格!!
简单的源代码将帮助你。 前女友。
#!/bin/bash
echo "My shell_1"
source my_script1.sh
echo "Back in shell_1"
从其他文件导入函数会有一些问题。 首先:您不需要执行此文件。最好不要这样做! 只需添加
. file
导入所有函数。所有这些都是在文件中定义的。 第二:你可以定义同名的函数。它将被覆盖。它是坏的。你可以那样宣布
declare -f new_function_name=old_function_name
and only after that do import. So you may call old function by new name. Third: You may import only full list of functions defined in file. If some not needed you may unset them. But if you rewrite your functions after unset they will be lost. But if you set reference to it as described above you may restore after unset with the same name. Finally In common procedure of import is dangerous and not so simple. Be careful! You may write script to do this more easier and safe. If you use only part of functions(not all) better split them in different files. Unfortunately this technique not made well in bash. In python for example and some other script languages it's easy and safe. Possible to make partial import only needed functions with its own names. We all want that in next bush versions will be done the same functionality. But now We must write many additional cod so as to do what you want.
chmod a+x /path/to/file-to-be-executed
那是我唯一需要的东西。一旦要执行的脚本像这样可执行,您(至少在我的情况下)在调用脚本时不需要任何其他额外的操作,如sh或./。
感谢@Nathan Lilienthal的评论
上面的答案建议添加#!/bin/bash行到被调用的下标脚本的第一行。但是即使你添加了shebang,在子shell中运行脚本并捕获输出也要快得多:
$(源SCRIPT_NAME)
当你想要继续运行同一个解释器(例如,从bash到另一个bash脚本)并确保子脚本的shebang行不被执行时,这是有效的。
例如:
#!/bin/bash
SUB_SCRIPT=$(mktemp)
echo "#!/bin/bash" > $SUB_SCRIPT
echo 'echo $1' >> $SUB_SCRIPT
chmod +x $SUB_SCRIPT
if [[ $1 == "--source" ]]; then
for X in $(seq 100); do
MODE=$(source $SUB_SCRIPT "source on")
done
else
for X in $(seq 100); do
MODE=$($SUB_SCRIPT "source off")
done
fi
echo $MODE
rm $SUB_SCRIPT
输出:
~ ❯❯❯ time ./test.sh
source off
./test.sh 0.15s user 0.16s system 87% cpu 0.360 total
~ ❯❯❯ time ./test.sh --source
source on
./test.sh --source 0.05s user 0.06s system 95% cpu 0.114 total
*例如,当病毒或安全工具在设备上运行时,可能需要额外的100ms来执行一个新进程。
使用引号。
$ ./script-that-consumes-argument.sh `sh script-that-produces-argument.sh`
然后获取生产者脚本的输出,作为消费者脚本的参数。
如果你在同一个目录下有另一个文件,你可以这样做:
bash another_script.sh
or
source another_script.sh
or
. another_script.sh
当使用bash而不是source时,脚本不能改变父脚本的环境。的。命令是POSIX标准的,而源命令是更易于阅读的bash同义词。(我更喜欢来源而不是。)如果您的脚本驻留在其他地方,只需提供该脚本的路径。相对路径和全路径都可以工作。