Shell 腳本攻略之小試牛刀
作者: Escape
鏈接:https://www.escapelife.site/posts/e8431cda.html
Shell 簡述簡介
通篇系列博客中均采用Bash,它是目前大多數(shù)GUN/Linux系統(tǒng)默認(rèn)的shell環(huán)境,所以大部分案例和討論都是假定在此基礎(chǔ)之上進(jìn)行的。
用戶分類
$ 表示的是普通用戶
# 表示管理員用戶root
shebang
shell腳本通常是以shebang起始的文本文件,如#!/bin/bash 在Unix的行話里,用sharp或hash來稱呼#,用bang來成為! /bin/bash表示Bash解釋器命令的位置,當(dāng)然也可以使用其他解釋器 Linux內(nèi)核會自動讀取首行并注意到#!/bin/bash,運(yùn)行/bin/bash來執(zhí)行腳本
運(yùn)行方式
將腳本作為bash的命令行參數(shù)
bash script.sh
授予腳本執(zhí)行權(quán)限,變成為可執(zhí)行文件
相對路徑
chmod a+x script.sh
./script.sh
絕對路徑
chmod a+x script.sh
/home/path/script.sh
命令終端打印
終端是交互的工具,用戶可以通過它來與shell環(huán)境進(jìn)行交互,打印是最為基礎(chǔ)的任務(wù)了。
echo 命令
使用規(guī)則
在默認(rèn)情況下,echo在每次調(diào)用后會添加一個換行符
使用不帶引號的echo時,無法在所要顯示的文本中使用分號;,因?yàn)榉痔柋挥米髅疃ń绶?br>使用雙引號的時候,遇到特殊字符的時候需要轉(zhuǎn)義字符\進(jìn)行轉(zhuǎn)義,而在單引號中則不用
變量替換在單引號中無效,如${username}等
轉(zhuǎn)義字符
\a 發(fā)出警告聲
\b 刪除前一個字符
\c 最后不加上換行符號
\f 換行但光標(biāo)仍舊停留在原來的位置
\n 換行且光標(biāo)移至行首
\r 光標(biāo)移至行首,但不換行
\t 插入tab
\v 與\f相同
\\ 插入\字符
\nnn 插入nnn(八進(jìn)制)所代表的ASCII字符
常用選項(xiàng)
可以使用直接輸出命令結(jié)果
可以使用-n選項(xiàng)來忽略結(jié)尾的換行符
可以使用-e選項(xiàng)在echo中轉(zhuǎn)義換行符
打印彩色輸出
文字閃動
0 關(guān)閉所有屬性
1 設(shè)置高亮度(加粗)
2 淡化
3 斜體
4 下劃線
5 閃爍
7 反顯
8 消隱
字的顏色
重置=0,黑色=30,紅色=31,綠色=32,黃色=33,藍(lán)色=34,洋紅=35,青色=36,白色=37
背景顏色
重置=0,黑色=40,紅色=41,綠色=42,黃色=43,藍(lán)色=44,洋紅=45,青色=46,白色=47
實(shí)例演示
[root@localhost] $ echo -e "\e[1;31mWord is red \e[0m"
Word is red
[root@localhost] $ echo -e "\e[1;42mthe background is green \e[0m"
the background is green
[root@localhost] $ echo -e "\e[1;42;31mWord is red, the background is green \e[0m"
Word is red, the background is green
[root@localhost] $ echo -e "\e[5;37;31mMySQL Server Stop...\e[0m"
MySQL Server Stop...
[root@localhost] $ echo -e "\e[4;37;31mMySQL Server Stop...\e[0m"
MySQL Server Stop...
printf命令使用規(guī)則
在默認(rèn)情況下,printf并不像echo一樣會自動添加換行符,必須手動添加
格式替換符
%s 字符串
%c ASCII 字符
%d 十進(jìn)制整數(shù)
%f 浮點(diǎn)數(shù)
對齊格式
-左對齊
不指定,默認(rèn)為右對齊
可以在對齊符號之后,添加數(shù)字,表示寬度
對于浮點(diǎn)數(shù),可以使用其他參數(shù)對小數(shù)部分進(jìn)行取舍,如%-4.2f
實(shí)例演示
[root@localhost] $ printf "%-5s %-10s %-4.2f\n" 1 escape 100.123
1 escape 100.12
變量和環(huán)境變量
變量是任何一種編程語言都必不可少的組成部分,用于存放各類數(shù)據(jù)。
在 Bash 中,每一個變量的值都是字符串 無論你給變量賦值時有沒有使用引號,值都會是字符串的形式存儲的 環(huán)境變量就是用來被 shell 環(huán)境和操作系統(tǒng)環(huán)境用來存儲一些特殊值的變量
查看環(huán)境變量
所有與終端相關(guān)
env命令
對于進(jìn)程而已
cat /proc/$PID/environ
默認(rèn)彼此之前由\0分割,可以使用tr命令進(jìn)行轉(zhuǎn)換
使用變量
定義變量
var=value
使用變量
echo $var
echo ${var}
添加 PATH 變量路徑
PATH變量通常定義在/etc/envitonment或/etc/profile或~/.bashrc中
export PATH="$PATH:/home/escape/bin"
變量的用途
獲得字符串長度
length=$(#var)
識別當(dāng)前所使用的shell
echo $SHELL
echo $0
檢查是否為超級用戶
[ $UID -ne 0 ]
修改 Bash 提示符
echo $PS1
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$'
可以使用類似\e[1;31的特定轉(zhuǎn)義字符來設(shè)置彩色提示字符串
使用函數(shù)添加環(huán)境變量
把下面的函數(shù)加入~/.bashrc中即可
prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }
判斷第二個參數(shù)指定的目錄是否存在 將第一個參數(shù)所指向的變量值設(shè)置成為第二個參數(shù)的值加上:$1的形式 設(shè)置為環(huán)境變量
使用方法
prepend PATH /opt/myapp/bin
改進(jìn)方案
prepend() { [ -d $2 ] && eval $1=\"$2\$\{$1+':'\$$1\}\" && export $1; }
#其中$1不為空的時候使用表達(dá)式':'\$$1
#$$1表示第一個參數(shù)的實(shí)際值
實(shí)例演示
[root@localhost] $ pgrep gedit
12501
[root@localhost] $ cat /proc/12501/environ
[root@localhost] $ cat /proc/12501/environ | tr '\0' '\n'
bash
[root@localhost] $ count=5
[root@localhost] $ fruit=apple
[root@localhost] $ echo "We hava $count ${fruit}(s)"
We have 5 apple(s)
[root@localhost] $ echo '$var' will print $var
$var will print $var
bash
[root@localhost] $ echo $PATH
/usr/local/var/pyenv/shims:/usr/local/bin
[root@localhost] $ export PATH="$PATH:/home/escape/bin"
[root@localhost] $ PATH="$PATH:/home/escape/bin"
[root@localhost] $ export PATH
[root@localhost] $ echo $PS1
${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)
進(jìn)行數(shù)學(xué)運(yùn)算
在Bash shell環(huán)境中,可以使用let、(())、[]執(zhí)行基本的算術(shù)操作,使用expr和bc可以進(jìn)行高級操作
基本算數(shù)
let
let result=num1+num2
let num++
let num--
let num+=5
let num-=5
$(())
result=$((num1+num2))
result=$(($num+5))
$[]
result=$[ num1+num2 ]
result=$[ $num+5 ]
高級算數(shù)
expr
result=expr 3+4
result=$(expr $num+5)
bc
普通用法 echo "4 * 0.56" | bc
設(shè)定小數(shù)精度 echo "scale=2; 3/8" | bc
十進(jìn)制轉(zhuǎn)二進(jìn)制 echo "obase=2; $num" | bc
二進(jìn)制轉(zhuǎn)十進(jìn)制 echo "obase=10; ibase=2; $num" | bc
平方根 echo "sqrt(100)" | bc
計算平方 echo "10^10" | bc
腳本判斷條件
寫好 shell 腳本需要熟悉下面的判斷條件和規(guī)則
邏輯判斷
! 邏輯非
-a 邏輯且
-o 邏輯或
語句判斷
解釋說明
[-e file] #如果 file 文件存在則為真
[-d file] #如果 file 存在且是一個目錄則為真
[-f file] #如果 file 存在且是一個普通文件則為真
[-b file] #如果 file 存在且是一個塊特殊文件則為真
[-s file] #如果 file 存在且大小不為 0 則為真
[-c file] #如果 file 存在且是一個字特殊文件則為真
[-h file] #如果 file 存在且是一個符號連接則為真
[-p file] #如果 file 存在且是一個名字管道則為真
[-r file] #如果 file 存在且是可讀的則為真
[-w file] #如果 file 存在且是可寫的則為真
[-x file] #如果 file 存在且是可執(zhí)行的則為真
[-L file] #如果 file 存在且是一個符號連接則為真
[-S file] #如果 file 存在且是一個套接字則為真
[-O file] #如果 file 存在且屬有效用戶 ID 則為真
[-G file] #如果 file 存在且屬有效用戶組則為真
[-t FD] #如果文件描述符 FD 打開且指向一個終端則為真
[-u file] #如果 file 存在且設(shè)置了 SUID(用 chmod u+s 設(shè)置)則為真
[-g file] #如果 file 存在且已經(jīng)設(shè)置了 SGID(用 chmod g+s 設(shè)置)則為真
[-k file] #如果 file 存在且已經(jīng)設(shè)置粘制位(用 chmod a+t 設(shè)置)則為真
條件表達(dá)式
解釋說明
[string] #string 的長度為非零 non-zero 則為真
[-n string] #string 的長度為非零 non-zero 則為真
[-z string] #string 的長度為零則為真
[sting1==string2] #如果 2 個字符串相同則為真
[string1!=string2] #如果 2 個字符串不相等則為真
[string1 –eq string2] #等于為真
[string1 -ne string2] #不等于為真
[string1 -le string2] #小于為真
[string1 –gt string2] #大于為真
[string1 –ge string2] #大于等于為真
[file1 –nt file2] #如果 file1 比 file2 要新或者 file1 存在且 file2 不存在則為真
[file1 –ot file2] #如果 file1 比 file2 要老或 file2 存在且 file1 不存在則為真
[file1 –ef file2] #如果 file1 和 file2 指向相同的設(shè)備和節(jié)點(diǎn)號則為真
評論
圖片
表情


