Shell腳本執(zhí)行的幾種方式小結(jié)
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)文章
linux 下實(shí)現(xiàn)sleep詳解及簡單實(shí)例
這篇文章主要介紹了linux 下實(shí)現(xiàn)sleep詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06jenkins 實(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