Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell 脚本调用另一个脚本的三种方法_Dablelv的博客专栏-CSDN博客 #35

Open
zepang opened this issue Feb 10, 2022 · 0 comments

Comments

@zepang
Copy link
Owner

zepang commented Feb 10, 2022

假如有脚本 first.sh:

#!/bin/bash



echo 'your are in first file'

如何在其它 Shell 脚本中调用 first.sh 呢?主要有三种方法:source、点号以及 sh 命令。

#!/bin/bash



echo 'your are in second file'
source first.sh
#!/bin/bash
echo 'your are in second file'
. first.sh

注意,点号与脚本文件之间记得要有空格。

#!/bin/bash
echo 'your are in second file'
sh  first.sh

三者输出的结果都是:

your are in second file
your are in first file

使用 source 命令和点号是等价的,类似于 C/C++ 中的 #include 预处理指令,都是将指定的脚本内容拷贝至当前的脚本中,由一个 Shell 进程来执行。使用 sh 命令来调用另外的脚本和前面两种方法有着本质的区别。使用 sh 命令则会开启新的 Shell 进程来执行指定的脚本,这样的话,父进程中的变量在子进程中就无法访问。参考如下代码:

first.sh 内容如下,访问了 second.sh 中的变量 second。

#!/bin/bash
echo 'your are in first file'
echo 'second:' $second

second.sh 内容,通过上面介绍的三种方法来调用 first.sh,看看对 second.sh 的变量 second 访问有什么区别!

#!/bin/bash
second=lvlv
echo 'your are in second file'
source first
. first
sh first

程序的输出结果是:

your are in second file
your are in first file
second: lvlv
your are in first file
second: lvlv
your are in first file
second:

可见,使用 sh 命令开启一个子进程来调用指定的 shell 脚本无法访问父进程的变量。我们如何让子进程访问父进程中变量呢?可以使用 export 命令。

我们需要知道 Shell 中按照变量的作用域和生命周期,Shell 变量可分为四大类:

(1)永久环境变量

需要修改配置文件,变量永久生效。

(2)临时环境变量

使用 export 命令行声明即可,变量在 shell 脚本进程结束后仍然有效,但在关闭当前 shell 会话后失效。

(3)全局变量

在脚本中定义,仅在当前 Shell 脚本中有效,其他 Shell 脚本进程不能访问,其作用域从定义的位置开始,到脚本结束或被显示删除的地方为止。注意,全局变量既可以在 Shell 函数内定义,也可以在 Shell 函数外定义,因为 Shell 函数内定义的变量默认为 global,且作用域从 “函数被调用时执行变量定义的地方” 开始,到脚本结束或被显示删除的地方为止。

#!/bin/bash

globalVar=dablelv	#全局变量

(4)局部变量

在 Shell 函数内显示使用 local 关键字定义的变量。其作用域局限于函数内。同名 local 变量会屏蔽 global 变量。

#!/bin/bash

function test() {
	local localVar=dablelv	
}
test
echo $localVar			  	

所以,使用 export 命令我们申明的是临时环境变量,在当前 shell 会话中,所有的 shell 实例都可以访问由 export 命令申明的临时环境变量。因为当前 shell 会话中的所有 shell 实例,都是当前 shell 会话的子进程,所以可以与父进程一同访问环境变量。

second.sh 修改如下:

#!/bin/bash

export second=lvlv
echo 'your are in second file'
sh first.sh

执行 second.sh 将输出:

your are in second file
your are in first file
second: lvlv

至此,通过 export 命令设置临时环境变量的方式,使得 Shell 子进程能够访问父进程中的变量。

那么如何定义永久环境变量呢?可以采用如下三种方法:

(1) 在/etc/profile文件中添加环境变量,对所有用户永久生效。例如通过 vim 编辑/etc/profile文件,在最后一行添加 CLASSPATH 变量。

export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

注意,修改文件后要想马上生效需要执行source /etc/profile. /etc/profile,不然只能在下次重新登录时才生效。

(2) 在~/.bash_profile文件中添加环境变量,只对当前用户永久有效。例如通过 vim 编辑~/.bash_profile文件中添加环境变量。

export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

注意: 修改文件后要想马上生效需要执行source ~/.bash_profile. ~/.bash_profile,不然只能在下次重新登录时才生效。

(3)在~/.bashrc文件中添加环境变量,只对当前用户永久有效。例如通过 vim 编辑~/.bash_rc文件中添加环境变量。

export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

注意:

  • 修改文件后要想马上生效需要执行source ~/.bash_rc. ~/.bash_rc,不然只能在下次重新登录时才生效;
  • ~/.bash_profile会显式调用~/.bashrc文件,而~/.bashrc显式调用/etc/bashrc文件。

另外,如想删除已设置的环境变量,可以使用 unset(builtin)命令来清除环境变量 ,例如$unset CLASSPATH。使用 readonly 命令可设置只读变量。如果使用了 readonly 命令的话,变量不可以被修改或清除。


Linux 命令(49)—— export 命令(builtin)
https://blog.csdn.net/k346k346/article/details/86751705

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant