shell 腳本入門到精通(中級)
一、shell 腳本的執行
二、輸出格式化
三、數據類型
四、重定向
五、變量
一、shell 腳本的執行
1. 腳本執行的4種方法
$ ls /tmp/test.sh
/tmp/test.sh
#!/bin/bash
# test.sh
# 這里借助SHLVL這個變量,SHLVL可以顯示shell的層級,
# 每啟動一個shell,這個值就加1
echo "shell level :$SHLVL"
echo "hello world!"
-
切換到shell腳本所在目錄執行
root@localhost:/# cd /tmp/
root@localhost:/tmp# chmod +x test.sh
root@localhost:/tmp# ./test.sh
shell level :2
hello world! -
以絕對路徑執行
root@localhost:~# chmod +x /tmp/test.sh
root@localhost:~# /tmp/test.sh
shell level :2
hello world! -
直接使用bash或sh 來執行bash shell腳本
root@localhost:/tmp# bash test.sh
shell level :2
hello world!
root@localhost:/tmp# sh test.sh
shell level :1
hello world! -
在當前shell 環境中執行
root@localhost:/tmp# . test.sh
shell level :1
hello world!
root@localhost:/tmp# source test.sh
shell level :1
hello world!
總結:注意看SHLVL的值,前3種方式都在子shell中執行(sh除外),第4種在當前shell種執行。
2.調試腳本
bash -x script.sh 跟蹤調試shell腳本
例:
root@localhost:/tmp# bash -x test.sh
+ echo 'shell level :2'
shell level :2
+ echo 'hello world!'
hello world!
-x 打印所執行的每一行命令以及當前狀態
set -x : 在執行時顯示參數和命令
set +x : 禁止調試
set -v : 當命令進行讀取時顯示輸入
set +v : 禁止打印輸入
二、輸出格式化
1. C語言風格的格式化
#!/bin/bash
printf "%-5s %-10s %-4s\n" NO. Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
root@localhost:/tmp# ./test.sh
NO. Name Mark
1 Sarath 80.35
2 James 91.00
2. echo
- 不換行
echo -n "hello world" - 轉義
echo -e "hello\t\tworld" - 彩色輸出
顏色 | 重置 | 黑 | 紅 | 綠 | 黃 | 藍 | 紫 | 青 | 白 |
---|---|---|---|---|---|---|---|---|---|
前景色 | 0 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 |
背景色 | 0 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
echo -e "\e[1;31m This is red test \e[0m"
或
echo -e "\033[47;31m This is red test \033[0m"
三、數據類型
1.字符串
獲取字符串長度
root@localhost:/# str="hello"
root@localhost:/# echo ${#str}
5
2. 數組
- 數組的定義
方法一:
arr=(1 2 3 4 5)
方法二:
arr[0]=1
arr[1]=2
arr[2]=3
echo ${arr[*]}
1 2 3
-
打印數組中的值
root@localhost:~# arr=(1 2 3 4 5)
root@localhost:~# echo ${arr[2]}
3
root@localhost:~# echo ${arr[*]}
1 2 3 4 5
root@localhost:~# echo ${arr[@]}
1 2 3 4 5 -
關聯數組 -- 數組的高級用法
普通數組只能使用整數作為索引值,而關聯數組可以使用任意文本作為索引值(有點類似於Python中的字典,不知道這樣理解對不對),關聯數組只在bash 4.0以上支持。
查看bash版本的方法:
bash -version
關聯數組的定義和使用
root@localhost:~# declare -A person
root@localhost:~# person=([name]="Wang" [age]=18)
root@localhost:~# echo ${person[name]}
Wang
root@localhost:~# echo ${person[age]}
18
root@localhost:~# echo ${person[*]}
Wang 18
四、重定向
符號 | 含義 | 用法 | 例 |
---|---|---|---|
< | 標准輸入 | 從文件中輸入 | wc -l file.txt |
> | 標准輸出 | 目標文件不存在會新建一個;目標文件存在會覆蓋原內容 | echo " " > /var/www/html/index.php |
>> | 追加到文件 | 目標文件不存在會新建一個;目標文件存在會在文件末尾追加 | echo "add text" >> file.txt |
例:
從定向的用法
# cat >> file.sh << EOF
> #!/bin/bash
> echo "hello"
> EOF
/dev/null 相當於垃圾桶
例:把標准輸出重定向到/dev/null
yum makecache > /dev/null
五、變量
1. 只讀變量
root@localhost:~# cat file.sh
#!/bin/bash
readonly hours_per_day=24
hours_per_day=12
更改變量會觸發異常
root@localhost:~# ./file.sh
./file.sh: line 3: hours_per_day: readonly variable
2. 展開運算符
運算符 | 用途 | 例 |
---|---|---|
${varname:-word} | 如果變量存在且非null,則返回其值; 否則返回word |
# echo ${service:-"service is not defined"} service is not defined |
${varname:=word} | 如果變量存在且非null,則返回其值; 否則設置它的值為word並返回 |
root@localhost:~# echo ${service:=httpd} |
${varname:+word} | 如果變量存在且非null,則返回word; 否則返回null |
echo ${service:+1} |
${varname:?message} | 如果變量存在且非null,則返回其值; 否則顯示message,並退出當前腳本或命令; message默認信息為:parameter null or not set |
# echo ${b:?} -bash: b: parameter null or not set # echo ${b:?"not defined"} -bash: b: not defined |