如何从一个shell脚本调用另一个shell脚本?

发布于 2024-12-19 12:33:53 字数 121 浏览 0 评论 0原文

我有两个 shell 脚本,a.shb.sh

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

I have two shell scripts, a.sh and b.sh.

How can I call b.sh from within the shell script a.sh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

じее 2024-12-26 12:33:53

您可以通过多种不同的方法来执行此操作:

  1. 使用 chmod a+x /path/to/file

    使其他脚本可执行,添加 #!/bin/bash 行(称为 shebang)位于顶部,以及文件所在的路径到 $PATH 环境变量。然后就可以像普通命令一样调用了;

  2. 或者使用 source 命令(这是 . 的别名)调用它,如下所示:

    源/路径/到/脚本
    
  3. 或者使用 < code>bash 命令来执行它,例如:

    <代码>/bin/bash /path/to/script
    

第一种和第三种方法将脚本作为另一个进程执行,因此其他脚本中的变量和函数将无法访问。
第二种方法在第一个脚本的进程中执行脚本,并从另一个脚本中提取变量和函数(因此它们可以从调用脚本中使用)。它当然会运行其他脚本中的所有命令,而不仅仅是设置变量。

在第二种方法中,如果您在第二个脚本中使用 exit,它也会退出第一个脚本。这在第一种和第三种方法中不会发生。

There are a couple of different ways you can do this:

  1. Make the other script executable with chmod a+x /path/to/file, add the #!/bin/bash line (called shebang) at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command;

  2. Or call it with the source command (which is an alias for .), like this:

    source /path/to/script
    
  3. Or use the bash command to execute it, like:

    /bin/bash /path/to/script
    

The first and third approaches execute the script as another process, so variables and functions in the other script will not be accessible.
The second approach 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). It will of course run all the commands in the other script, not only set variables.

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.

多情癖 2024-12-26 12:33:53

看看这个。

#!/bin/bash
echo "This script is about to run another script."
sh ./script.sh
echo "This script has just run another script."

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."
撩心不撩汉 2024-12-26 12:33:53

有几种方法可以做到这一点。执行脚本的终端:

#!/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")

对于带空格的路径来说,所有这些都是正确的!!!

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!!!

好菇凉咱不稀罕他 2024-12-26 12:33:53

我正在寻找的答案:

( exec "path/to/script" )

如上所述,exec 替换了 shell,而不创建新进程。 但是,我们可以将其放入子 shell 中,这是使用括号完成的。

编辑:
实际上 ( "path/to/script" ) 就足够了。

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.

EDIT:
Actually ( "path/to/script" ) is enough.

铃予 2024-12-26 12:33:53

如果同一目录中有另一个文件,您可以执行以下操作:

bash another_script.sh

source another_script.sh

. another_script.sh

当您使用 bash 而非 source 时,脚本无法更改父脚本的环境。 . 命令是 POSIX 标准,而 source 命令是 . 的更具可读性的 bash 同义词(我更喜欢 source) <代码>.)。如果您的脚本位于其他地方,只需提供该脚本的路径。相对路径和完整路径都应该有效。

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.

你在我安 2024-12-26 12:33:53

取决于。
简要地...
如果您想在当前控制台上加载变量并执行,您可以在代码中使用 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

如果您只想执行一个文件并且您唯一感兴趣的是结果,您可以执行以下操作:

#!/bin/bash
set -x
./executing_only.sh
bash i_can_execute_this_way_too.sh
bash or_this_way.sh
set +x

Depends on.
Briefly...
If you want load variables on current console and execute you may use source myshellfile.sh on your code. Example:

#!/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
bash i_can_execute_this_way_too.sh
bash or_this_way.sh
set +x
可遇━不可求 2024-12-26 12:33:53

您可以使用 /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

You can use /bin/sh to call or execute another script (via your actual script):

 # 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
埋葬我深情 2024-12-26 12:33:53

首先,您必须包含您调用的文件:

#!/bin/bash
. includes/included_file.sh

然后您像这样调用您的函数:

#!/bin/bash
my_called_function

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
清引 2024-12-26 12:33:53

简单的来源会对您有所帮助。
对于前。

#!/bin/bash
echo "My shell_1"
source my_script1.sh
echo "Back in shell_1"

Simple source will help you.
For Ex.

#!/bin/bash
echo "My shell_1"
source my_script1.sh
echo "Back in shell_1"
香橙ぽ 2024-12-26 12:33:53

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

#!bin/bash
./myscript.sh &

如果要执行的脚本不在同一目录中,则使用脚本的完整路径。
例如:`/home/user/script-directory/./myscript.sh &

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 &

口干舌燥 2024-12-26 12:33:53

最上面的答案建议将 #!/bin/bash 行添加到正在调用的子脚本的第一行。但即使您添加了 shebang,在子 shell 中运行脚本并捕获输出也会更快*

$(source 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

* 例如,当病毒或安全工具在设备上运行时,可能需要额外 100 毫秒来执行新进程。

The top answer suggests adding #!/bin/bash line to the first line of the sub-script being called. But even if you add the shebang, it is much faster* to run a script in a sub-shell and capture the output:

$(source SCRIPT_NAME)

This works when you want to keep running the same interpreter (e.g. from bash to another bash script) and ensures that the shebang line of the sub-script is not executed.

For example:

#!/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

Output:

~ ❯❯❯ 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

* For example when virus or security tools are running on a device it might take an extra 100ms to exec a new process.

沉溺在你眼里的海 2024-12-26 12:33:53

这对我有用,这是执行另一个脚本的主 sh 脚本的内容。

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

This was what worked for me, this is the content of the main sh script that executes the other one.

#!/bin/bash 
source /path/to/other.sh
pathToShell="/home/praveen/"   
chmod a+x $pathToShell"myShell.sh"
sh $pathToShell"myShell.sh"
pathToShell="/home/praveen/"   
chmod a+x $pathToShell"myShell.sh"
sh $pathToShell"myShell.sh"
欢你一世 2024-12-26 12:33:53
 #!/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

 # 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=$?
想你只要分分秒秒 2024-12-26 12:33:53
chmod a+x /path/to/file-to-be-executed

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

感谢@Nathan Lilienthal 的评论

chmod a+x /path/to/file-to-be-executed

That was the only thing I needed. Once the script to be executed is made executable like this, you (at least in my case) don't need any other extra operation like sh or ./ while you are calling the script.

Thanks to the comment of @Nathan Lilienthal

心房的律动 2024-12-26 12:33:53

假设新文件是“/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

Assume the new file is "/home/satya/app/app_specific_env" and the file contents are as follows

#!bin/bash

export FAV_NUMBER="2211"

Append this file reference to ~/.bashrc file

source /home/satya/app/app_specific_env

When ever you restart the machine or relogin, try echo $FAV_NUMBER in the terminal. It will output the value.

Just in case if you want to see the effect right away, source ~/.bashrc in the command line.

锦爱 2024-12-26 12:33:53

从其他文件导入函数时出现一些问题。
首先:您不需要将此文件执行为可执行文件。最好不要这样做!
只需添加

. file

即可导入所有功能。所有这些都将如同在您的文件中定义的一样。
第二:您可以定义具有相同名称的函数。它将被覆盖。这很糟糕。您可以这样声明

declare -f new_function_name=old_function_name 

,然后才进行导入。
所以你可以用新名称调用旧函数。
第三:您只能导入文件中定义的函数的完整列表。
如果有些不需要,您可以取消设置它们。但是,如果您在取消设置后重写函数,它们将会丢失。但是,如果您如上所述设置对它的引用,则可以在使用相同名称取消设置后恢复。
最后通常的进口程序是危险的,而且并不那么简单。当心!您可以编写脚本来更轻松、更安全地执行此操作。
如果您只使用部分函数(不是全部),最好将它们拆分到不同的文件中。不幸的是,这项技术在 bash 中表现不佳。例如,在 python 和其他一些脚本语言中,它既简单又安全。可以仅部分导入具有自己名称的所需函数。我们都希望在下一个布什版本中能够实现相同的功能。但现在我们必须编写许多额外的代码才能做你想做的事。

There are some problems to import functions from other file.
First: You needn't to do this file executable. Better not to do so!
just add

. file

to import all functions. And all of them will be as if they are defined in your file.
Second: You may be define the function with the same name. It will be overwritten. It's bad. You may declare like that

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.

飘然心甜 2024-12-26 12:33:53

使用反引号。

$ ./script-that-consumes-argument.sh `sh script-that-produces-argument.sh`

然后获取生产者脚本的输出作为消费者脚本的参数。

Use backticks.

$ ./script-that-consumes-argument.sh `sh script-that-produces-argument.sh`

Then fetch the output of the producer script as an argument on the consumer script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文