欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shell腳本執(zhí)行的幾種方式小結(jié)

 更新時間:2023年09月18日 10:33:53   作者:stormkai  
本文介紹了Shell腳本執(zhí)行的幾種方式小結(jié),主要介紹了5種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Shell腳本執(zhí)行的幾種方式 

前面兩種是通過創(chuàng)建子shell進(jìn)程來解釋執(zhí)行腳本。父shell可能獲取不到子shell的一些環(huán)境變量。
source是在當(dāng)前shell進(jìn)程里面直接執(zhí)行。

父子shell的演示

  • 創(chuàng)建子shell進(jìn)程

bash命令創(chuàng)建子shell進(jìn)程
ps -ef|grep bash 查看shell進(jìn)程

[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3496   3449  0 00:10 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# bash
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3534   3505  0 00:10 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# bash
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3535   3505  0 00:10 pts/0    00:00:00 bash
root       3566   3535  0 00:11 pts/0    00:00:00 grep --color=auto bash

可以看到,第一次執(zhí)行bash后,創(chuàng)建了個父進(jìn)程是3499的子進(jìn)程;第二次執(zhí)行bash后,創(chuàng)建兩個父進(jìn)程是3505(第一次創(chuàng)建的進(jìn)程)的子進(jìn)程。

  • 退出子shell進(jìn)程

exit 退出子shell進(jìn)程

[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3535   3505  0 00:10 pts/0    00:00:00 bash
root       3566   3535  0 00:11 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# exit
exit
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3616   3505  0 00:17 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# exit
exit
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3626   3449  0 00:17 pts/0    00:00:00 grep --color=auto bash

1. 創(chuàng)建shell腳本hello.sh

在/home目錄下創(chuàng)建文件夾jiaoben,在文件夾里面創(chuàng)建hello.sh文件

[root@localhost home]# mkdir jiaoben
[root@localhost home]# cd jiaoben/
[root@localhost jiaoben]# ll
總用量 0
[root@localhost jiaoben]# touch hello.sh
[root@localhost jiaoben]# vi hello.sh
[root@localhost jiaoben]# ll
總用量 4
-rw-r--r--. 1 root root 26 6月  27 23:29 hello.sh

hello.sh的內(nèi)容為

#!/bin/bash
echo "Hello!"

腳本以 #!/bin/bash 開頭,指定shell解析器

2. bash/sh 腳本的絕對路徑/相對路徑

不管腳本有沒有x(可執(zhí)行)權(quán)限,都可以執(zhí)行。

2.1 sh 腳本的相對路徑執(zhí)行腳本

[root@localhost jiaoben]# pwd
/home/jiaoben
[root@localhost jiaoben]# sh hello.sh
Hello!

2.2 sh 腳本的絕對路徑執(zhí)行腳本

[root@localhost jiaoben]# sh /home/jiaoben/hello.sh
Hello!

2.3 bash 腳本的相對路徑執(zhí)行腳本

[root@localhost jiaoben]# bash hello.sh
Hello!

2.4 bash 腳本的絕對路徑執(zhí)行腳本

[root@localhost jiaoben]# bash /home/jiaoben/hello.sh
Hello!

3. 腳本的絕對路徑/相對路徑(需要腳本的+x權(quán)限)

3.1 腳本的相對路徑執(zhí)行腳本

./hello.sh

[root@localhost jiaoben]# pwd
/home/jiaoben
[root@localhost jiaoben]# ll
總用量 4
-rw-r--r--. 1 root root 26 6月  27 23:29 hello.sh
[root@localhost jiaoben]# ./hello.sh
-bash: ./hello.sh: 權(quán)限不夠
[root@localhost jiaoben]# chmod +x hello.sh
[root@localhost jiaoben]# ./hello.sh
Hello!
[root@localhost jiaoben]# ll
總用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# ./hello.sh
Hello!

從上面可以看到,執(zhí)行./hello.sh需要x權(quán)限

3.2 腳本的絕對路徑執(zhí)行腳本

/home/jiaoben/hello.sh

  • 收回hello.sh的x(可執(zhí)行)權(quán)限,然后執(zhí)行腳本
[root@localhost jiaoben]# chmod -x hello.sh
[root@localhost jiaoben]# ll
總用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# /home/jiaoben/hello.sh
-bash: /home/jiaoben/hello.sh: 權(quán)限不夠
  • 給腳本hello.sh添加x(可執(zhí)行)權(quán)限,通過腳本絕對路徑再執(zhí)行腳本
[root@localhost jiaoben]# chmod +x hello.sh
[root@localhost jiaoben]# ll
總用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# /home/jiaoben/hello.sh
Hello!

4. source 腳本的絕對路徑/相對路徑執(zhí)行腳本

source執(zhí)行腳本時,不管腳本有沒有x(可執(zhí)行權(quán)限),腳本都可以執(zhí)行我們平時配置完環(huán)境變量,用的就是source執(zhí)行,如source /etc/profile

4.1 source相對路徑執(zhí)行腳本

source hello.sh

[root@localhost jiaoben]# ll
總用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# source hello.sh
Hello!

4.2 source絕對路徑執(zhí)行腳本

source /home/jiaoben/hello.sh

[root@localhost jiaoben]# ll
總用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# source /home/jiaoben/hello.sh
Hello!

hello.sh沒有x(可執(zhí)行)權(quán)限時候,使用source執(zhí)行情況:

[root@localhost jiaoben]# chmod -x hello.sh
[root@localhost jiaoben]# ll
總用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# source hello.sh
Hello!
[root@localhost jiaoben]# source /home/jiaoben/hello.sh
Hello!

5. .空格腳本的絕對路徑/相對路徑執(zhí)行腳本

.空格絕對路徑/相對路徑執(zhí)行腳本時候,不管腳本有沒有x(可執(zhí)行)權(quán)限,都可以執(zhí)行。
下面我們李子都使用沒有x(可執(zhí)行)權(quán)限的x腳本來驗證。

5.1 .空格腳本的相對路徑執(zhí)行腳本

. hello.sh

[root@localhost jiaoben]# ll
總用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# . hello.sh
Hello!

5.2 .空格腳本的絕對路徑執(zhí)行腳本

[root@localhost jiaoben]# ll
總用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# . /home/jiaoben/hello.sh
Hello!

 到此這篇關(guān)于Shell腳本執(zhí)行的幾種方式小結(jié)的文章就介紹到這了,更多相關(guān)Shell 腳本執(zhí)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Shell 字符串拼接的實(shí)現(xiàn)示例

    Shell 字符串拼接的實(shí)現(xiàn)示例

    這篇文章主要介紹了Shell 字符串拼接的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • SHELL腳本監(jiān)控JAVA進(jìn)程的代碼

    SHELL腳本監(jiān)控JAVA進(jìn)程的代碼

    這篇文章主要介紹了SHELL腳本監(jiān)控JAVA進(jìn)程,需要的朋友可以參考下
    2016-03-03
  • linux文本處理工具及正則表達(dá)式集錦

    linux文本處理工具及正則表達(dá)式集錦

    這篇文章主要介紹了linux文本處理工具及正則表達(dá)式集錦,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • linux 下實(shí)現(xiàn)sleep詳解及簡單實(shí)例

    linux 下實(shí)現(xiàn)sleep詳解及簡單實(shí)例

    這篇文章主要介紹了linux 下實(shí)現(xiàn)sleep詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Linux下使用expect命令編寫自動化交互腳本

    Linux下使用expect命令編寫自動化交互腳本

    今天小編就為大家分享一篇關(guān)于Linux下使用expect命令編寫自動化交互腳本,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-11-11
  • Apache服務(wù)器的安裝步驟(圖文教程)

    Apache服務(wù)器的安裝步驟(圖文教程)

    下面小編就為大家?guī)硪黄狝pache服務(wù)器的安裝步驟(圖文教程)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Linux常用命令全集(超全面)

    Linux常用命令全集(超全面)

    本文是小編日常收集整理的有關(guān)linux常用命令的知識,非常不錯具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-10-10
  • awk基礎(chǔ)知識小結(jié)

    awk基礎(chǔ)知識小結(jié)

    awk基礎(chǔ)知識小結(jié),方便學(xué)習(xí)awk的朋友
    2013-02-02
  • jenkins 實(shí)現(xiàn)shell腳本化定時執(zhí)行任務(wù)的方法

    jenkins 實(shí)現(xiàn)shell腳本化定時執(zhí)行任務(wù)的方法

    這篇文章主要介紹了jenkins 實(shí)現(xiàn)shell腳本化定時執(zhí)行任務(wù),解決訪問是jenkins構(gòu)建好之后將jar遠(yuǎn)程推送到生產(chǎn)服務(wù)器,提前退出后臺執(zhí)行服務(wù)器遠(yuǎn)程腳本,腳本通過ngnx提前切走nginx代理auction sleep 1800s,半小時后執(zhí)行更新重啟,具體操作過程跟隨小編一起看看吧
    2022-01-01
  • Shell退出狀態(tài)的使用

    Shell退出狀態(tài)的使用

    這篇文章主要介紹了Shell退出狀態(tài)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論