什么是子Shell
? ? 子Shell的概念其實(shí)是貫穿整個(gè)Shell的,如果想要更好的理解和寫(xiě)Shell腳本則必須要了解子Shell的相關(guān)知識(shí)。其概念如下所示:
子Shell本質(zhì)就是從當(dāng)前的Shell環(huán)境中打開(kāi)一個(gè)新的Shell環(huán)境,而新開(kāi)的Shell稱之為子Shell(SubShell),相應(yīng)的開(kāi)啟子Shell的環(huán)境稱之為父Shell。子Shell和父Shell是子進(jìn)程和父進(jìn)程的關(guān)系,而這個(gè)進(jìn)程則全部是bash進(jìn)程。子Shell可以從父Shell中繼承變量、命令全路徑、文件描述符、當(dāng)前工作目錄等。在子Shell中常用的兩個(gè)變量如下所示:
- $BASH_SUBSHELL:查看從當(dāng)前進(jìn)程開(kāi)始的子Shell層數(shù)
- $BASHPID:查看當(dāng)前所處BASH的PID
在Linux系統(tǒng)中,系統(tǒng)運(yùn)行的程序基本都是從CentOS 6.x(init)或CentOS7.x(systemd)PID為1的進(jìn)程)繼承而來(lái)的,所有的程序都可以看作為init的子進(jìn)程。
# CentOS 6.x
[root@localhost data]# pstree -hp
init(1)─┬─NetworkManager(3643)
├─Xvnc(22811)
├─abrtd(4760)
├─acpid(3755)
├─atd(4801)
├─auditd(3392)───{auditd}(3393)
├─automount(3849)─┬─{automount}(3850)
│ ├─{automount}(3851)
│ ├─{automount}(3854)
│ └─{automount}(3857)
# CentOS 7.x
[root@localhost ~]# pstree -hp
systemd(1)─┬─ModemManager(1051)─┬─{ModemManager}(1068)
│ └─{ModemManager}(1076)
├─Xvnc(5563)─┬─{Xvnc}(5566)
│ ├─{Xvnc}(5567)
│ ├─{Xvnc}(5568)
子Shell產(chǎn)生的途徑
通過(guò)后臺(tái)作業(yè):&
[root@localhost Test]# cat jobs.sh
#!/bin/bash
parentShell="ParentShell"
echo "Parent Shell start and Level:"$BASH_SUBSHELL
# define subshell
{
echo "SubShell start and Level:"$BASH_SUBSHELL
subShell="SubShell"
echo "SubShell value: ${subShell}"
echo "parentShell value: ${parentShell}"
# sleep 5
echo "SubShell end and Level: $BASH_SUBSHELL "
} & # running in backgroud
echo "Parent end and Level:$BASH_SUBSHELL"
if [ -z "${subShell}" ]
then
echo "subShell is not defined in ParentShell"
else
echo "subShell is defined in ParentShel"
fi
[root@localhost Test]# bash jobs.sh
Parent Shell start and Level:0
Parent end and Level:0
subShell is not defined in ParentShell
SubShell start and Level:1
SubShell value: SubShell
parentShell value: ParentShell
SubShell end and Level: 1
根據(jù)運(yùn)行結(jié)果,結(jié)論如下所示:
- 在Shell中可以使用&產(chǎn)生子Shell
- 由&產(chǎn)生的子Shell可以直接引用父Shell的變量,而子Shell產(chǎn)生的變量不能被父Shell引用
- 在Shell中使用&可以實(shí)現(xiàn)多線程并發(fā)
通過(guò)管道:|
[root@localhost Test]# cat jobs.sh
#!/bin/bash
parentShell="ParentShell"
echo "Parent Shell start and Level:"$BASH_SUBSHELL
# define subshell
echo "" | # 管道
{
echo "SubShell start and Level:"$BASH_SUBSHELL
subShell="SubShell"
echo "SubShell value: ${subShell}"
echo "parentShell value: ${parentShell}"
# sleep 5
echo "SubShell end and Level: $BASH_SUBSHELL "
}
echo "Parent end and Level:$BASH_SUBSHELL"
if [ -z "${subShell}" ]
then
echo "subShell is not defined in ParentShell"
else
echo "subShell is defined in ParentShel"
fi
[root@localhost Test]# bash jobs.sh
Parent Shell start and Level:0
SubShell start and Level:1
SubShell value: SubShell
parentShell value: ParentShell
SubShell end and Level: 1
Parent end and Level:0
subShell is not defined in ParentShell
根據(jù)運(yùn)行結(jié)果,結(jié)論如下所示:
- 在Shell中可以使用管道產(chǎn)生子Shell
- 由管道產(chǎn)生的子Shell可以直接引用父Shell的變量,而子Shell產(chǎn)生的變量不能被父Shell引用
- 由管道產(chǎn)生的Shell是順序執(zhí)行的,僅能在子Shell執(zhí)行完成后才能返回父Shell中繼續(xù)執(zhí)行,這一點(diǎn)也是與&最大的區(qū)別。
通過(guò)()
[root@localhost Test]# cat jobs.sh
#!/bin/bash
parentShell="ParentShell"
echo "Parent Shell start and Level:"$BASH_SUBSHELL
# define subshell
(
echo "SubShell start and Level:"$BASH_SUBSHELL
subShell="SubShell"
echo "SubShell value: ${subShell}"
echo "parentShell value: ${parentShell}"
# sleep 5
echo "SubShell end and Level: $BASH_SUBSHELL "
)
echo "Parent end and Level:$BASH_SUBSHELL"
if [ -z "${subShell}" ]
then
echo "subShell is not defined in ParentShell"
else
echo "subShell is defined in ParentShel"
fi
[root@localhost Test]# bash jobs.sh
Parent Shell start and Level:0
SubShell start and Level:1
SubShell value: SubShell
parentShell value: ParentShell
SubShell end and Level: 1
Parent end and Level:0
subShell is not defined in ParentShell
根據(jù)運(yùn)行結(jié)果,結(jié)論如下所示:
- 在Shell中可以使用()產(chǎn)生子Shell
- 由()產(chǎn)生的子Shell可以直接引用父Shell的變量,而子Shell產(chǎn)生的變量不能被父Shell引用
- 由()產(chǎn)生的Shell是順序執(zhí)行的,僅能在子Shell執(zhí)行完成后才能返回父Shell中繼續(xù)執(zhí)行,
看到這個(gè)結(jié)果,大家會(huì)不會(huì)覺(jué)得使用()跟使用管道一樣的?
通過(guò)調(diào)用外部Shell
[root@localhost Test]# cat subShell.sh parentShell.sh -n
# SubShell
1 #!/bin/bash
2 echo "SubShell start and Level:"$BASH_SUBSHELL
3 subShell="SubShell"
4 echo "SubShell value: ${subShell}"
5 echo "parentShell value: ${parentShell}"
6 echo "parentExportShell value: ${parentExportShell}"
7 if [ -z "${parentShell}" ];then
8 echo "parentShell value is : null"
9 else
10 echo "parentShell value is : "${parentShell}
11 fi
12
13 # ParentShell
14 #!/bin/bash
15 parentShell="Parent"
16 export parentExportShell="parentExportShell"
17 echo "Parent Shell start and Level:"$BASH_SUBSHELL
18 bash ./subShell.sh # invoke subshell
19 sleep 3
20 echo "Parent Shell end and Level:"$BASH_SUBSHELL
21 if [ -z "${subShell}" ]
22 then
23 echo "subShell is not defined in ParentShell"
24 else
25 echo "subShell is defined in ParentShell"
26 fi
[root@localhost Test]# bash parentShell.sh
Parent Shell start and Level:0
SubShell start and Level:0
SubShell value: SubShell
parentShell value:
parentExportShell value: parentExportShell
parentShell value is : null
Parent Shell end and Level:0
subShell is not defined in ParentShell
根據(jù)運(yùn)行結(jié)果,結(jié)論如下所示:
- 在Shell中可以通過(guò)外部Shell腳本產(chǎn)生子Shell
- 在調(diào)用外部Shell時(shí),父Shell定義的變量不能被子Shell繼承,如果要繼承父Shell的變量,必須使用export使其成為全局環(huán)境變量。
- 調(diào)用外部Shell產(chǎn)生的Shell是順序執(zhí)行的,僅能在子Shell執(zhí)行完成后才能返回父Shell中繼續(xù)執(zhí)行,
Shell腳本調(diào)用模式
? ? 通常在大型的項(xiàng)目中,都會(huì)將較大模塊進(jìn)行拆分為多個(gè)小模塊進(jìn)行代碼編寫(xiě)調(diào)試等。因此在一個(gè)Shell腳本中也不可能包含所有模塊,一般都采用在一個(gè)腳本中去調(diào)用當(dāng)前用到的腳本,這種被稱之為Shell嵌套。在一個(gè)腳本中嵌套腳本的方式主要有fork、exec和source。
fork模式調(diào)用腳本
? ? fork模式是最普通的腳本調(diào)用方式。在使用該方式調(diào)用腳本時(shí),系統(tǒng)會(huì)創(chuàng)建一個(gè)子Shell去調(diào)用腳本。其調(diào)用方式如下所示:
/bin/bash /path/shellscript.sh # 未給腳本添加執(zhí)行權(quán)限時(shí)
或
/path/shellscript.sh # 腳本擁有執(zhí)行權(quán)限時(shí)
fork本質(zhì)是復(fù)制進(jìn)程。使用該方式時(shí),fork會(huì)復(fù)制當(dāng)前進(jìn)程做為一個(gè)副本,而后將這些資源交給子進(jìn)程。因此子進(jìn)程會(huì)繼承父進(jìn)程的一些資源,如環(huán)境變量、變量等。而父進(jìn)程卻是完全獨(dú)立的,子進(jìn)程和父進(jìn)程相當(dāng)于面向?qū)ο笾幸粋€(gè)對(duì)象的兩個(gè)實(shí)例。
exec模式調(diào)用腳本
? ? exec調(diào)用腳本時(shí),不會(huì)開(kāi)啟一個(gè)新的子Shell來(lái)進(jìn)行調(diào)用腳本,被調(diào)用的腳本和調(diào)用腳本在同一個(gè)Shell內(nèi)執(zhí)行。但需要注意的是使用exec調(diào)用新腳本后,在執(zhí)行完新腳本的內(nèi)容后,不再返回到調(diào)用腳本中執(zhí)行后續(xù)未執(zhí)行的內(nèi)容,這也是與fork調(diào)用腳本的主要區(qū)別。其主要調(diào)用方式如下所示:
exec /path/shellscript.sh
exec的本質(zhì)是加載另外一個(gè)程序來(lái)代替當(dāng)前運(yùn)行的進(jìn)程。即在不創(chuàng)建新進(jìn)程的情況下去加載一個(gè)新程序,而在進(jìn)程執(zhí)行完成后就直接退出exec所在的Shell環(huán)境。
source模式調(diào)用腳本
? ? source調(diào)用腳本時(shí),也不會(huì)開(kāi)啟一個(gè)新的子Shell來(lái)執(zhí)行被調(diào)用的腳本,同樣也是在同一個(gè)Shell中執(zhí)行,因此被調(diào)用腳本是可以繼承調(diào)用腳本的變量、環(huán)境變量等。與exec調(diào)用方式的區(qū)別是,source在執(zhí)行完被調(diào)用腳本的內(nèi)容后,依然會(huì)返回調(diào)用腳本中,去執(zhí)行調(diào)用腳本中未執(zhí)行的內(nèi)容。其主要調(diào)用方式如下所示:
source /path/shellscript.sh
或
. /path/shellscript.sh # .和source是等價(jià)的
三種調(diào)用模式示例
? ? 示例代碼如下所示:
[root@localhost Test]# cat -n subShell.sh parentShell.sh
1 #!/bin/bash
2 echo "SubShell start and Level:"$BASH_SUBSHELL
3 echo "SubShell PID is:" $$
4 subShell="SubShell"
5 echo "SubShell value: ${subShell}"
6 echo "parentShell value: ${parentShell}"
7 echo "parentExportShell value: ${parentExportShell}"
8 if [ -z "${parentShell}" ];then
9 echo "parentShell value is : null"
10 else
11 echo "parentShell value is : "${parentShell}
12 fi
13 #!/bin/bash
14 # print usage
15 function Usage() {
16 echo "Usage:$0 {fork|exec|source}"
17 exit 1
18 }
19 # print return variable
20 function PrintPara() {
21 if [ -z "${subShell}" ]
22 then
23 echo "subShell is not defined in ParentShell"
24 else
25 echo "subShell is defined in ParentShell "${subShell}
26 fi
27 }
28 # invoke pattern
29 function ParentFunction() {
30 parentShell="Parent"
31 export parentExportShell="parentExportShell"
32 echo "Parent Shell start and Level:"$BASH_SUBSHELL
33 echo "Parent PID is:"$$
34 case "$1" in
35 fork)
36 echo "Using fork pattern"
37 /bin/bash ./subShell.sh
38 PrintPara ;;
39 exec)
40 echo "Using exec pattern"
41 exec ./subShell.sh
42 PrintPara ;;
43 source)
44 echo "Using source pattern"
45 source ./subShell.sh
46 PrintPara ;;
47 *)
48 echo "Input error ,usage is:" Usage
49 esac
50 }
51 # check parameter number
52 function CheckInputPara() {
53 if [ $# -ne 1 ]
54 then
55 Usage
56 fi
57 ParentFunction $*
58 }
59 CheckInputPara $*
1、fork調(diào)用結(jié)果:
[root@localhost Test]# bash parentShell.sh fork
Parent Shell start and Level:0
Parent PID is:26413
Using fork pattern
SubShell start and Level:0
SubShell PID is: 26414
SubShell value: SubShell
parentShell value:
parentExportShell value: parentExportShell
parentShell value is : null
subShell is not defined in ParentShell
1、父Shell和子Shell的PID不一樣,則可以說(shuō)明產(chǎn)生了新的子進(jìn)程
2、調(diào)用腳本中定義的全局變量可以傳入到被調(diào)用腳本,而被調(diào)用的腳本中定義的變量是無(wú)法返回到調(diào)用腳本中的
2、exec調(diào)用結(jié)果:
[root@localhost Test]# chmod +x subShell.sh
[root@localhost Test]# bash parentShell.sh exec
Parent Shell start and Level:0
Parent PID is:25543
Using exec pattern
SubShell start and Level:0
SubShell PID is: 25543
SubShell value: SubShell
parentShell value:
parentExportShell value: parentExportShell
parentShell value is : null
1、父Shell和子Shell的PID一樣,則可以說(shuō)明未產(chǎn)生新的子進(jìn)程
2、調(diào)用腳本中定義的全局變量可以傳入到被調(diào)用腳本
3、最重要的一點(diǎn)就是在執(zhí)行完被調(diào)用腳本后直接退出Shell了,而調(diào)用腳本未執(zhí)行的內(nèi)容并沒(méi)有被執(zhí)行
3、source調(diào)用結(jié)果:
[root@localhost Test]# bash parentShell.sh source
Parent Shell start and Level:0
Parent PID is:19955
Using source pattern
SubShell start and Level:0
SubShell PID is: 19955
SubShell value: SubShell
parentShell value: Parent
parentExportShell value: parentExportShell
parentShell value is : Parent
subShell is defined in ParentShell: SubShell
1、父Shell和子Shell的PID一樣,則可以說(shuō)明未產(chǎn)生新的子進(jìn)程
2、調(diào)用腳本中定義的普通變量和全局變量可以傳入到被調(diào)用腳本,反之亦然
3、最重要的一點(diǎn)就是在執(zhí)行完被調(diào)用腳本后返回調(diào)用腳本中繼續(xù)執(zhí)行剩下的內(nèi)容
三種調(diào)用模式使用場(chǎng)景
- 1、fork模式使用場(chǎng)景
fork模式常用于常規(guī)嵌套腳本執(zhí)行的場(chǎng)景中,僅僅是執(zhí)行嵌套腳本中命令,調(diào)用腳本也不需要使用被調(diào)用腳本中的變量和函數(shù)等信息。
- 2、exec模式使用場(chǎng)景
exec模式常用于調(diào)用腳本中末尾中。而這種模式在執(zhí)行完被調(diào)用腳本中就直接退出,因此使用較少,可使用source代替exec
- 3、source模式使用場(chǎng)景
source模式算是在Shell常用的一種腳本嵌套調(diào)用模式,常用于執(zhí)行嵌套腳本啟動(dòng)一些服務(wù)程序等,其最大的優(yōu)點(diǎn)就是嵌套腳本中定義的變量和函數(shù)等資源可以被調(diào)用腳本獲取和使用。