shell指令碼執行的幾種方式

來源:互聯網
上載者:User

標籤:

執行shell指令碼有以下幾種方式

1、相對路徑方式,需先cd到指令碼路徑下

[[email protected] tmp]# cd /tmp[[email protected] tmp]# ./ceshi.sh 指令碼執行成功

2、絕對路徑方式

[[email protected] tmp]# /tmp/ceshi.sh 指令碼執行成功
3、bash命令調用

[[email protected] /]# bash /tmp/ceshi.sh 指令碼執行成功

4、. (空格)  相對或絕對方式

[[email protected] /]# . /tmp/ceshi.sh 指令碼執行成功

說下幾種方式的區別

第一種和第二種沒有什麼區別,兩種方式都需要提前賦予指令碼以執行許可權。

第三種是把指令碼當做bash的調用來處理,所以,指令碼不需要有執行許可權就可以執行。

前三種方式都是在當前shell中開啟一個子shell來執行指令碼內容,當指令碼內容結束,則子shell關閉,回到父shell中。

第四種是使指令碼內容在當前shell裡執行,而不是單獨開子shell執行。

開子shell與不開子shell的區別就在於,環境變數的繼承關係,如在子shell中設定的當前變數,不做特殊通道處理的話,父shell是不可見的。

而在當前shell中執行的話,則所有設定的環境變數都是直接生效可用的。

驗證

[[email protected] /]# cat /tmp/ceshi.sh top

1、前三種執行方式下的pstree顯示

     ├─sshd─┬─sshd───bash───bash───top     │      └─sshd───bash───pstree
2、第四種執行方式下的pstree顯示

     ├─sshd─┬─sshd───bash───top     │      └─sshd───bash───pstree

3、驗證環境變數設定的繼承關係及可見關係

建立兩個指令碼,father.sh和subshell.sh。其中father.sh調用subshell.sh

[[email protected] /]# cat /tmp/father.sh v_ceshi='father'#-------父shell中定義變數echo "以子shell方式呼叫指令碼"/tmp/subshell.shecho "輸出v_ceshi值為${v_ceshi}"echo ""echo "在當前shell中執行指令碼". /tmp/subshell.shecho "輸出v_ceshi值為${v_ceshi}"[[email protected] /]# [[email protected] /]# cat /tmp/subshell.sh v_ceshi=son[[email protected] /]# 
執行結果為

[[email protected] /]# /tmp/father.sh 以子shell方式呼叫指令碼輸出v_ceshi值為father在當前shell中執行指令碼輸出v_ceshi值為son





shell指令碼執行的幾種方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.