最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2023年09月18日 10:33:53   作者:stormkai  
本文介紹了Shell腳本執(zhí)行的幾種方式小結(jié),主要介紹了5種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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)建了個(gè)父進(jìn)程是3499的子進(jìn)程;第二次執(zhí)行bash后,創(chuàng)建兩個(gè)父進(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í)行腳本時(shí),不管腳本有沒有x(可執(zhí)行權(quán)限),腳本都可以執(zhí)行我們平時(shí)配置完環(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)限時(shí)候,使用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í)行腳本時(shí)候,不管腳本有沒有x(可執(zhí)行)權(quán)限,都可以執(zhí)行。
下面我們李子都使用沒有x(可執(zhí)行)權(quán)限的x腳本來驗(yàn)證。

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)文章

  • linux查看所有用戶和查看用戶組的方法(修改用戶組)

    linux查看所有用戶和查看用戶組的方法(修改用戶組)

    linux里并沒有像windows的net user,net localgroup這些方便的命令來管理用戶,下面介紹查看所有用戶和用戶組的方法
    2014-01-01
  • 查看linux中某個(gè)端口(port)是否被占用的方法

    查看linux中某個(gè)端口(port)是否被占用的方法

    下面小編就為大家?guī)硪黄榭磍inux中某個(gè)端口(port)是否被占用的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Shell用sed命令刪除特定行的方法

    Shell用sed命令刪除特定行的方法

    這篇文章主要介紹了Shell用sed命令刪除特定行的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 詳解Linux解壓縮文件

    詳解Linux解壓縮文件

    這篇文章給大家介紹了Linux解壓縮文件的命令,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-05-05
  • Linux echo命令的使用及三種實(shí)現(xiàn)方式

    Linux echo命令的使用及三種實(shí)現(xiàn)方式

    這篇文章主要介紹了Linux echo命令的使用及三種實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Linux更新Python版本及修改python默認(rèn)版本的方法

    Linux更新Python版本及修改python默認(rèn)版本的方法

    很多情況下拿到的服務(wù)器python版本很低,需要自己動(dòng)手更改默認(rèn)python版本,但是有好多朋友都被這個(gè)問題難倒了,接下來,通過本篇文章給大家介紹linux更新Python版本及修改默認(rèn)版本的方法,感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Xshell全局去除提示音圖文方法詳解

    Xshell全局去除提示音圖文方法詳解

    這篇文章主要為大家介紹了Xshell全局去除提示音圖文方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Linux系統(tǒng)中查看目錄權(quán)限的命令詳解

    Linux系統(tǒng)中查看目錄權(quán)限的命令詳解

    在?Linux?系統(tǒng)中,文件和目錄的權(quán)限管理是保證系統(tǒng)安全和數(shù)據(jù)安全的重要機(jī)制,正確理解和使用權(quán)限設(shè)置,可以幫助用戶有效地控制對文件和目錄的訪問,本文給大家介紹了Linux系統(tǒng)中查看目錄權(quán)限的命令,需要的朋友可以參考下
    2024-12-12
  • Linux使用cal命令查看日歷的實(shí)用技巧分享

    Linux使用cal命令查看日歷的實(shí)用技巧分享

    在 Linux 系統(tǒng)中,cal 命令是一個(gè)簡單卻非常實(shí)用的小工具,專門用于顯示日歷信息,無論是查看當(dāng)前月份的日歷、特定年份的日歷,還是進(jìn)行日期計(jì)算,cal 命令都能輕松應(yīng)對,本文將深入探討 cal 命令的功能,從基礎(chǔ)用法到高級(jí)技巧,需要的朋友可以參考下
    2026-02-02
  • Linux中shell腳本獲取當(dāng)前工作目錄的方法

    Linux中shell腳本獲取當(dāng)前工作目錄的方法

    今天小編就為大家分享一篇Linux中shell腳本獲取當(dāng)前工作目錄的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論

小金县| 嘉定区| 贺兰县| 新沂市| 平湖市| 开鲁县| 临洮县| 肇源县| 云阳县| 原平市| 高要市| 措美县| 穆棱市| 池州市| 罗田县| 陕西省| 南安市| 定陶县| 崇仁县| 横峰县| 清流县| 扎赉特旗| 舞阳县| 化州市| 揭西县| 东阿县| 偃师市| 唐海县| 吉木萨尔县| 鹤庆县| 确山县| 乐东| 贵定县| 黔西县| 黎川县| 且末县| 高陵县| 浑源县| 静宁县| 宝清县| 鱼台县|