#!/bin/bash # test.sh # 這里借助SHLVL這個(gè)變量,SHLVL可以顯示shell的層級(jí), # 每啟動(dòng)一個(gè)shell,這個(gè)值就加1 echo "shell level :$SHLVL" echo "hello world!"
切換到shell腳本所在目錄執(zhí)行
root@localhost:/# cd /tmp/
root@localhost:/tmp# chmod +x test.sh
root@localhost:/tmp# ./test.sh
shell level :2
hello world!
以絕對(duì)路徑執(zhí)行
root@localhost:~# chmod +x /tmp/test.sh
root@localhost:~# /tmp/test.sh
shell level :2
hello world!
直接使用bash或sh 來(lái)執(zhí)行bash shell腳本
root@localhost:/tmp# bash test.sh
shell level :2
hello world!
root@localhost:/tmp# sh test.sh
shell level :1
hello world!
在當(dāng)前shell 環(huán)境中執(zhí)行
root@localhost:/tmp# . test.sh
shell level :1
hello world!
root@localhost:/tmp# source test.sh
shell level :1
hello world!
總結(jié):注意看SHLVL的值,前3種方式都在子shell中執(zhí)行(sh除外),第4種在當(dāng)前shell種執(zhí)行。
bash -x script.sh 跟蹤調(diào)試腳本
root@localhost:/tmp# bash -x test.sh + echo 'shell level :2' shell level :2 + echo 'hello world!' hello world!
-x 打印所執(zhí)行的每一行命令及當(dāng)前狀態(tài)
set -x:在執(zhí)行是顯示參數(shù)和命令
set +x:禁止調(diào)試
set -v :當(dāng)命令進(jìn)行讀取時(shí)顯示輸入
set +v :當(dāng)命令進(jìn)行讀取時(shí)禁止打印輸入
#!/bin/bash printf " % -5s %-10s %-4s\n" NO. Name Mark
printf " % -5s %-10s %-4.2f\n" 1 joe 80.55
printf " % -5s %-10s %-4.1f\n" 2 joe 80.5
printf " % -5s %-10s %-4.3f\n" 3 joe 80.500
1.不換行
echo -n "hello world"
2.轉(zhuǎn)義
echo -e "hello\t\tworld"
3.彩色輸出
顏色 重置 黑 紅 綠 黃 藍(lán) 紫 青 白 前景色 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"
獲取字符串長(zhǎng)度
echo ${#str}
數(shù)組的定義 方法一: arr=(1 2 3 4 5) 方法二: arr[0]=1 arr[1]=2 arr[2]=3 echo ${arr[*]} 1 2 3
打印數(shù)組中的值
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
普通數(shù)組只能使用整數(shù)作為索引值,而關(guān)聯(lián)數(shù)組可以使用任意文本作為索引值(有點(diǎn)類(lèi)似于Python中的字典,不知道這樣理解對(duì)不對(duì)),關(guān)聯(lián)數(shù)組只在bash 4.0以上支持。
查看bash版本的方法:
bash -version
關(guān)聯(lián)數(shù)組的定義和使用
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
符號(hào) 含義 用法 例 < 標(biāo)準(zhǔn)輸入 從文件中輸入 wc -l file.txt > 標(biāo)準(zhǔn)輸出 目標(biāo)文件不存在會(huì)新建一個(gè);目標(biāo)文件存在會(huì)覆蓋原內(nèi)容 echo "" > /var/www/html/index.php >> 追加到文件 目標(biāo)文件不存在會(huì)新建一個(gè);目標(biāo)文件存在會(huì)在文件末尾追加 echo "add text" >> file.txt
#!/bin/bash readonly hours_per_day=24 hours_per_day=12
更改變量會(huì)觸發(fā)異常
運(yùn)算符 | 用途 | 例 |
---|---|---|
${varname:-word} | 如果變量存在且非null,則返回其值; 否則返回word |
# echo ${service:-"service is not defined"} service is not defined |
${varname:=word} | 如果變量存在且非null,則返回其值; 否則設(shè)置它的值為word并返回 |
root@localhost:~# echo ${service:=httpd} |
${varname:+word} | 如果變量存在且非null,則返回word; 否則返回null |
echo ${service:+1} |
${varname:?message} | 如果變量存在且非null,則返回其值; 否則顯示message,并退出當(dāng)前腳本或命令; message默認(rèn)信息為:parameter null or not set |
# echo ${b:?} -bash: b: parameter null or not set # echo ${b:?"not defined"} -bash: b: not defined |
聯(lián)系客服