一 背景
讓我們先來看一下下面這個簡單的例子:
#!/bin/bash #=============================================================================== # FILE: process_test.sh # USAGE: . ${YOUR_PATH}/process_test.sh # DESCRIPTION: # AUTHOR: IVAN DU # E-MAIL: [email protected] # WECHAT: ecsboy # TECHBLOG: https://ivandu.blog.csdn.net # GITHUB: https://github.com/mrivandu # CREATED: 2019-05-01 23:56:32 # LICENSE: GNU General Public License. # COPYRIGHT: © IVAN DU 2019 # REVISION: v1.0 #=============================================================================== test_num=${RANDOM}; echo "Test start. Current process is: $$. Parent process is: ${PPID}. Test_num is: ${test_num}. "; # & { echo '-----------&------------'; echo "& test start. test_num is: ${test_num} "; sleep 30 echo "& test. Now pid is:$$"; test_num=${RANDOM} echo "& test_num is: ${test_num}. "; }& echo "& test end. Test_num is: ${test_num}. "; # | echo ""|\ { echo '-----------|------------'; echo "| test start. test_num is: ${test_num} "; sleep 30 echo "| test. Now pid is:$$"; test_num=${RANDOM} echo "| test_num is: ${test_num}. "; } echo "| test end. Test_num is: ${test_num}. "; # () ( echo '-----------()------------'; echo "() test start. test_num is: ${test_num} "; sleep 30 echo "() test. Now pid is:$$"; test_num=${RANDOM} echo "() test_num is: ${test_num}. "; ) echo "() test end. Test_num is: ${test_num}. "; echo "All tests stop. Parent process is: $$. Test_num is: ${test_num}.";
這個例子展示了三種創建子 shell 的方法,每個子 shell 的內容基本都一致。接下來讓我們看一下執行結果:
[gysl@gysl-dev ~]$ sh process_test.sh Test start. Current process is: 10432. Parent process is: 6118. Test_num is: 1457. & test end. Test_num is: 1457. -----------&------------ & test start. test_num is: 1457 -----------|------------ | test start. test_num is: 1457 & test. Now pid is:10432 & test_num is: 26453. | test. Now pid is:10432 | test_num is: 17987. | test end. Test_num is: 1457. -----------()------------ () test start. test_num is: 1457 () test. Now pid is:10432 () test_num is: 28781. () test end. Test_num is: 1457. All tests stop. Parent process is: 10432. Test_num is: 1457.
二 分析求證
執行該腳本的當前 shell 的 PID 是 6118 ,也就是當前 shell 的 PPID 。腳本開始時,我們使用一個隨機數對 test_num 進行了賦值,在當前腳本中的值是 1457 。在三種子 shell 的執行過程中,test_num 傳入了子 shell ,依然為 1457 。子 shell 中再次對 test_num 賦值能覆蓋傳入的 test_num 的值,但子shell 執行完畢之後,返回的值依然為 1457 。三種方式都出奇的的一致,這說明:子 shell 在執行過程中能引用父 shell 的變量,父 shell 中的變量在子 shell 中被修改後不返回父 shell ,作用域只存在於子 shell 中。簡而言之,父 shell 中的值能被子 shell 調用,父 shell 中的變量能被子 shell 修改,子 shell 中的變量值不能傳回父 shell 。
繼續分析,“& test end. Test_num is: 1457. ”出現在第二行,這一行原本是在 & 子 shell 執行完畢後才執行的,但是卻提前執行了。進一步觀察,我們發現,& 子 shell 和 | 子 shell 的執行結果混在一起了。而 () 子 shell 卻中規中矩的按照預期執行。這是為什麼呢?讓我們來看一下 pstree 命令返回的結果:
[gysl@gysl-dev ~]$ pstree -c -p 6118 bash(6118)───sh(10432)─┬─sh(10433)───sleep(10436) └─sh(10435)───sleep(10437) [gysl@gysl-dev ~]$ pstree -c -p 6118 bash(6118)───sh(10432)───sh(10439)───sleep(10440)
當前 shell 10432 在執行初期,先後產生了兩個子 shell,30 秒後,這兩個子 shell 執行結束,執行 () 子 shell 。結果顯而易見,& 子 shell 支持異步執行!
三 總結
3.1 實現子 shell 的方式有三種:&/|(管道)/()。
3.2 子 shell 能調用並修改父 shell 的變量值,但是子 shell 的變量值不返回父 shell 中,要牢記。
3.3 & 方式實現的子 shell 能異步執行,這是與其他兩種實現方式最大的不同之處。