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

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

问题描述

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

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?

推荐答案

有几种不同的方法可以做到这一点:

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

  • 或者使用bash命令来执行,比如:

  • 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).

    在第二种方法中,如果您在第二个脚本中使用 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 脚本调用一个 shell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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