调用有三种方法:

1、fork:不同的shell,调用后返回父shell,子shell从父shell中继承变量,但子shell的变量不会带回父shell,直接用path/to/file.sh调用;

2、exec:同一个shell,调用后不返回,用exec path/to/file.sh调用;

3、source:同一个shell,调用后返回,用source path/to/file.sh调用

第一个脚本quote1.sh,代码如下:

 1 #!/bin/bash
 2 #
 3 A=1
 4 echo "ID1=$$"
 5 export A
 6 echo -e "A1=$A\n"
 7 case $1 in
 8         --exec)
 9                 echo "use exec"
10                 exec ./quote2.sh;;
11         --source)
12                 echo "use source"
13                 source ./quote2.sh;;
14         *)
15                 echo "use fork"
16                 ./quote2.sh;;
17 esac
18 echo -e "\nID1=$$"
19 echo "A1=$A"

第二个脚本quote2.sh,代码如下:

1 #!/bin/bash
2 #
3 echo "A1=$A"
4 A=2
5 echo "ID2=$$"
6 export A
7 echo "A2=$A"

# chmod +x quote*  //添加权限

# cd /root/test/script/  //进入文件所在目录

1、选择fork方法

# ./quote1.sh  //无参数执行脚本1

linux中shell脚本引用另一shell脚本

 两个脚本的进程ID号不同,所以在不同的shell下执行的,但调用完quote2.sh会返回继续执行。

2、选择exec方法

# ./quote1.sh --exec  //观察exec执行结果

linux中shell脚本引用另一shell脚本

两个进程ID号相同,所以是在同一shell下执行,但调用完quote2.sh就会结束。

3、选择source方法

# ./quote.sh --source  //观察source执行结果

linux中shell脚本引用另一shell脚本

两个进程ID号相同,所以是在同一shell下执行,但调用完quote2.sh会返回继续执行。

相关文章:

  • 2021-09-27
  • 2021-06-20
  • 2021-10-29
  • 2018-06-02
  • 2021-07-10
  • 2021-09-29
猜你喜欢
  • 2021-12-26
  • 2021-12-04
  • 2018-05-27
  • 2021-09-07
  • 2022-12-23
  • 2021-09-10
  • 2021-08-23
相关资源
相似解决方案