我有两个shell脚本,a.sh和b.sh。

我如何从shell脚本a.sh调用b.sh ?


当前回答

这是为我工作的,这是执行另一个主sh脚本的内容。

#!/bin/bash 
source /path/to/other.sh

其他回答

这是为我工作的,这是执行另一个主sh脚本的内容。

#!/bin/bash 
source /path/to/other.sh

如果你在同一个目录下有另一个文件,你可以这样做:

bash another_script.sh

or

source another_script.sh

or

. another_script.sh

当使用bash而不是source时,脚本不能改变父脚本的环境。的。命令是POSIX标准的,而源命令是更易于阅读的bash同义词。(我更喜欢来源而不是。)如果您的脚本驻留在其他地方,只需提供该脚本的路径。相对路径和全路径都可以工作。

 #!/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=$?
chmod a+x /path/to/file-to-be-executed

那是我唯一需要的东西。一旦要执行的脚本像这样可执行,您(至少在我的情况下)在调用脚本时不需要任何其他额外的操作,如sh或./。

感谢@Nathan Lilienthal的评论

只需在一行中添加您在终端中输入的任何内容来执行脚本! 例如:

#!bin/bash
./myscript.sh &

如果要执行的脚本不在同一目录下,请使用脚本的完整路径。 例如:“/ home / user /脚本目录/。/ myscript.sh &