標籤:
一、fork ( /directory/script.sh) :如果shell中包含執行命令,那麼子命令並不影響父級的命令,在子命令執行完後再執行父級命令。子級的環境變數不會影響到父級。
fork是最普通的, 就是直接在指令碼裡面用/directory/script.sh來調用script.sh這個指令碼.
啟動並執行時候開一個sub-shell執行調用的指令碼,sub-shell執行的時候, parent-shell還在。
sub-shell執行完畢後返回parent-shell. sub-shell從parent-shell繼承環境變數.但是sub-shell中的環境變數不會帶回parent-shell
二、exec (exec /directory/script.sh):執行子級的命令後,不再執行父級命令。
exec與fork不同,不需要新開一個sub-shell來執行被調用的指令碼. 被調用的指令碼與父指令碼在同一個shell內執行。但是使用exec調用一個新指令碼以後, 父指令碼中exec行之後的內容就不會再執行了。這是exec和source的區別
三、source (source /directory/script.sh):執行子級命令後繼續執行父級命令,同時子級設定的環境變數會影響到父級的環境變數。
與fork的區別是不新開一個sub-shell來執行被調用的指令碼,而是在同一個shell中執行. 所以被調用的指令碼中聲明的變數和環境變數, 都可以在主指令碼中得到和使用.
可以通過下面這兩個指令碼來體會三種調用方式的不同:
cat 1.sh:
#!/bin/bashA=B echo "PID for 1.sh before exec/source/fork:$$"export Aecho "1.sh: \$A is $A"case $1 in exec) echo "using exec…" exec ./2.sh ;; source) echo "using source…" . ./2.sh ;; *) echo "using fork by default…" ./2.sh ;;esacecho "PID for 1.sh after exec/source/fork:$$"echo "1.sh: \$A is $A"
cat 2.sh:
#!/bin/bashecho "PID for 2.sh: $$"echo "2.sh get \$A=$A from 1.sh"A=Cexport Aecho "2.sh: \$A is $A"
結果:
[[email protected] ~]# ./1.sh PID for 1.sh befor exec/source/fork:10761.sh:$A is Busing fork by default...PID for 2.sh: 10772.sh get $A=B from 1.sh2.sh: $A is CPID for 1.sh after exec/source/fork:10761.sh:$A is B[[email protected] ~]# ./1.sh execPID for 1.sh befor exec/source/fork:10781.sh:$A is Busing exec...PID for 2.sh: 10782.sh get $A=B from 1.sh2.sh: $A is C[[email protected] ~]# ./1.sh sourcePID for 1.sh befor exec/source/fork:10791.sh:$A is Busing source...PID for 2.sh: 10792.sh get $A=B from 1.sh2.sh: $A is CPID for 1.sh after exec/source/fork:10791.sh:$A is C[[email protected] ~]#
由此可見:
fork() 1.sh和2.sh各是一個不同的進程,子進程繼承父進程的環境變數,創進新的進程,子進程結束後反回父進程。這是個是shell中最常用的。
exec() 1.sh和2.sh都列印了同一個進程號,是同一進程,子進程創進是替代父進程,子進程結束時,退了進程,沒有列印原屬於1.sh的after部分。驗證可以在shell中執行 exec ls這樣會退出登陸。
source() 1.sh和2.sh是同一進程,子程式的命令完成後續繼執行父程式的命令,當然,子程式改變了環境變數會影響父程式的因為同一進程。
在shell指令碼中調用另一個指令碼的三種不同方法(fork, exec, source)