如何从另一个Shell脚本调用一个Shell脚本? [英] How to call one shell script from another shell script?

查看:143
本文介绍了如何从另一个Shell脚本调用一个Shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何在外壳脚本a.sh中调用b.sh?

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

推荐答案

有两种不同的方法可以实现:

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

  1. 使其他脚本可执行,在顶部添加#!/bin/bash行,并将文件所在的路径添加到$ PATH环境变量中.然后您可以将其作为常规命令调用;

  1. 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;

或使用source命令(别名为.)调用它,如下所示:source /path/to/script;

Or call it with the source command (alias is .) like this: source /path/to/script;

或使用bash命令执行它:/bin/bash /path/to/script;

Or use the bash command to execute it: /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.

在第二种方法中,如果您在第二个脚本中使用exit,它将同样退出第一个脚本.第一种和第三种方法不会发生这种情况.

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.

这篇关于如何从另一个Shell脚本调用一个Shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆