Bash 不光能執行互動模式下的指令,還可以執行文本當中的指令。
執行 文本中的指令可以通過 source 或 .
如下
[[email protected] sh01]# cat sh01.sh
ls -l /
echo "hello world"
[[email protected] sh01]# source sh01.sh
總用量 94
dr-xr-xr-x. 2 root root 4096 9月 22 12:08 bin
dr-xr-xr-x. 5 root root 1024 9月 21 03:27 boot
drwxr-xr-x. 18 root root 3700 9月 21 06:01 dev
drwxr-xr-x. 75 root root 4096 9月 22 12:08 etc
drwxr-xr-x. 7 root root 4096 9月 22 04:22 home
dr-xr-xr-x. 8 root root 4096 9月 22 06:58 lib
dr-xr-xr-x. 10 root root 12288 9月 22 12:08 lib64
drwx------. 2 root root 16384 9月 21 03:25 lost+found
drwxr-xr-x. 2 root root 4096 9月 23 2011 media
dr-xr-xr-x. 7 root root 4096 3月 29 2017 mnt
drwxrwx---. 2 root groupshare 4096 9月 22 05:17 ooxx
drwxr-xr-x. 3 root root 4096 9月 22 07:33 opt
dr-xr-xr-x. 87 root root 0 9月 21 06:00 proc
dr-xr-x---. 4 root root 4096 9月 22 12:33 root
dr-xr-xr-x. 2 root root 12288 9月 22 09:30 sbin
drwxr-xr-x. 7 root root 0 9月 21 06:00 selinux
drwxr-xr-x. 2 root root 4096 9月 23 2011 srv
drwxr-xr-x. 13 root root 0 9月 21 06:00 sys
drwxrwxrwt. 3 root root 4096 9月 22 12:28 tmp
drwxr-xr-x. 14 root root 4096 9月 22 08:24 usr
drwxr-xr-x. 17 root root 4096 9月 21 03:26 var
hello world
[[email protected] sh01]# . sh01.sh
總用量 94
dr-xr-xr-x. 2 root root 4096 9月 22 12:08 bin
dr-xr-xr-x. 5 root root 1024 9月 21 03:27 boot
drwxr-xr-x. 18 root root 3700 9月 21 06:01 dev
drwxr-xr-x. 75 root root 4096 9月 22 12:08 etc
drwxr-xr-x. 7 root root 4096 9月 22 04:22 home
dr-xr-xr-x. 8 root root 4096 9月 22 06:58 lib
dr-xr-xr-x. 10 root root 12288 9月 22 12:08 lib64
drwx------. 2 root root 16384 9月 21 03:25 lost+found
drwxr-xr-x. 2 root root 4096 9月 23 2011 media
dr-xr-xr-x. 7 root root 4096 3月 29 2017 mnt
drwxrwx---. 2 root groupshare 4096 9月 22 05:17 ooxx
drwxr-xr-x. 3 root root 4096 9月 22 07:33 opt
dr-xr-xr-x. 87 root root 0 9月 21 06:00 proc
dr-xr-x---. 4 root root 4096 9月 22 12:33 root
dr-xr-xr-x. 2 root root 12288 9月 22 09:30 sbin
drwxr-xr-x. 7 root root 0 9月 21 06:00 selinux
drwxr-xr-x. 2 root root 4096 9月 23 2011 srv
drwxr-xr-x. 13 root root 0 9月 21 06:00 sys
drwxrwxrwt. 3 root root 4096 9月 22 12:28 tmp
drwxr-xr-x. 14 root root 4096 9月 22 08:24 usr
drwxr-xr-x. 17 root root 4096 9月 21 03:26 var
hello world
通過 bash 指令可以建立一個子bash,通過echo $$ 可以檢視目前bash的程序号,通過pstree指令可以檢視程序樹,我們找到pstree指令,然後往前看,可以看出bash的層級關系
[[email protected] sh01]# echo $$
21502
[[email protected] sh01]# pstree
init─┬─auditd───{auditd}
├─crond
├─login───bash
├─master─┬─pickup
│ └─qmgr
├─5*[mingetty]
├─nginx───nginx
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
└─udevd───2*[udevd]
[[email protected] sh01]# bash
[[email protected] sh01]# echo $$
21834
[[email protected] sh01]# pstree
init─┬─auditd───{auditd}
├─crond
├─login───bash
├─master─┬─pickup
│ └─qmgr
├─5*[mingetty]
├─nginx───nginx
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───bash───pstree
└─udevd───2*[udevd]
[r[email protected] sh01]# exit
exit
[[email protected] sh01]# echo $$
21502
bash 指令 再建立一個 bash子程序的同時可以 執行文本中的指令,執行完畢以後會退出子程序
[[email protected] sh01]# bash sh01.sh
如果在檔案中加入 一行 #! /bin/bash ,就可以通過 ./檔案名的方式去執行該文本檔案中的指令,并且是自動建立一個bash程序去執行的該文本檔案
原理,目前bash會執行指令行的指令,指令行給出了目前路徑下的一個檔案,然後就會讀取檔案的内容發現第一行是
#! bin/bash,就會新啟動一個bash程序再次讀取該檔案,這是它發現第一行是 # 号開頭,代表注釋掉了,然後就繼續執行下面的指令,執行完畢以後推出目前bash。
[[email protected] sh01]# cat sh01.sh
#! /bin/bash
ls -l /
echo "hello world"
注意,以 ./檔案名的方式去執行的話,需要給該檔案添加上可執行權限
[[email protected] sh01]# chmod +x sh01.sh
[[email protected] sh01]# ll
總用量 4
-rwxr-xr-x. 1 root root 40 9月 22 13:12 sh01.sh
[[email protected] sh01]# ./sh01.sh
同理,awk也可以這樣使用
[[email protected] ~]# cat command.awk
#! /bin/awk -f
{
split($3,date,"-");
if(date[2]=="01"){
name[$1]+=$5;
if($2=="0"){
role[$1]="M";
}else{
role[$1]="W";
}
}
}
END{
for(i in name){
print i "\t" name[i] "\t" role[i];
}
}
[[email protected] ~]# cat awk.txt
Tom 0 2012-12-11 car 3000
John 1 2013-01-13 bike 1000
vivi 1 2013-01-18 car 2800
Tom 0 2013-01-20 car 2500
John 1 2013-01-28 bike 3500
[[email protected] ~]# ./command.awk awk.txt
vivi 2800 W
Tom 2500 M
John 4500 W
總結 : . 和 source去執行 是在目前bash中去執行的,哪怕文本中 第一行寫了 #! ... 。 如果使用 ./ 且配合 第一行的 #! ... ,則會開啟一個新的bash程序去執行文本中的指令,執行完畢以後會關閉該bash程序。
bash還支援函數程式設計
如下
[[email protected] ~]# test(){
> echo "hello world"
> echo $param
> }
[ro[email protected] ~]# param=123
[[email protected] ~]# test
hello world
123