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會返回繼續執行。

相關文章
相關標籤/搜索