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

 

调用有三种方法:shell

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

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

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

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

 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,代码以下:blog

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/  //进入文件所在目录进程

一、选择fork方法ip

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

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

二、选择exec方法

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

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

三、选择source方法

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

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

相关文章
相关标签/搜索