fork,source和exec運行指令碼時的差異

來源:互聯網
上載者:User

fork,source和exec運行指令碼時的差異
    使用 fork 方式運行 script 時, 就是讓 shell(parent process) 產生一個 child process 去執行該 script, 當 child process 結束後, 會返回 parent process,  但 parent process 的環境是不會因 child process 的改變而改變的. source   使用 source 方式運行 script 時, 就是讓 script 在當前 process 內執行, 而不是產生一個 child process 來執行. 由於所有執行結果均於當前 process 內完成, 若 script 的環境有所改變, 當然也會改變當前 process 環境了. exec   使用 exec 方式運行script時, 它和 source 一樣, 也是讓 script 在當前process內執行, 但是 process 內的原代碼剩下部分將被終止. 同樣, process 內的環境隨script 改變而改變. 結論:通常如果我們執行時,都是預設為fork的。大家可以通過pstree命令看看關於父子進程的關係。如上,如果想讓父進程得到子進程的環境變數,就是source方式了 1.sh的指令碼[sql] #!/bin/bash    A=B  echo "PID for 1.sh before exec/source/fork:$$"  export A  echo "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 ;;  esac  echo "PID for 1.sh after exec/source/fork:$$"  echo "1.sh: \$A is $A"  2.sh的指令碼[sql] #!/bin/bash    echo "PID for 2.sh: $$"  echo "2.sh get \$A=$A from 1.sh"  A=C  export A  echo "2.sh: \$A is $A"  3. 實驗3.1 fork[sql] PID for 1.sh before exec/source/fork:25992  1.sh: $A is B  using fork by default…  PID for 2.sh: 25993  2.sh get $A=B from 1.sh  2.sh: $A is C  PID for 1.sh after exec/source/fork:25992  1.sh: $A is B  3.2 source[sql] PID for 1.sh before exec/source/fork:25994  1.sh: $A is B  using source…  PID for 2.sh: 25994  2.sh get $A=B from 1.sh  2.sh: $A is C  PID for 1.sh after exec/source/fork:25994  1.sh: $A is C  注意: 三個PID都是25994, 子程式的環境變數被帶了出來.3.3 exec[sql] PID for 1.sh before exec/source/fork:25997  1.sh: $A is B  using exec…  PID for 2.sh: 25997  2.sh get $A=B from 1.sh  2.sh: $A is C  注意: 主程式的剩下部分不執行了.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.