更多精彩內(nèi)容药有,請(qǐng)關(guān)注微信公眾號(hào):后端技術(shù)小屋
《Linux Shell腳本攻略》是一本適合初學(xué)者系統(tǒng)學(xué)習(xí)Bash Shell的書(shū)籍毅戈,墻裂推薦。以下是我閱讀這本書(shū)的筆記塑陵,希望對(duì)讀者有用嫂沉。
- 輸出顏色字符
echo -e "\e[1:41m" # 1表示背景色
- echo打印!需轉(zhuǎn)義
echo "hello, bash!" # 報(bào)錯(cuò)赖淤,因?yàn)?在shell中屬于特殊字符,需要轉(zhuǎn)義
echo "hello, bash\!" # 不報(bào)錯(cuò),!已轉(zhuǎn)義
echo 'hello, bash!' # 不報(bào)錯(cuò)基公,在單引號(hào)中!無(wú)需轉(zhuǎn)移
- printf可用于格式化輸出
# 用法類似C中的`printf`
printf "format" var1 var2 ...
- echo常用選項(xiàng)
echo -n # 不在字符串末尾添加換行符
echo -e # 支持轉(zhuǎn)義字符表示
echo -e "\e[id XXXXXX" # 顯示顏色字體斑司,其中id表示背景顏色id
- pgrep
# 根據(jù)進(jìn)程名獲取進(jìn)程id
pgrep <進(jìn)程名> # 相當(dāng)于ps -ef
cat /proc/$PID/environ # 查看進(jìn)程運(yùn)行過(guò)程中的環(huán)境變量
- 用(())進(jìn)行算數(shù)運(yùn)算
# 計(jì)算兩個(gè)數(shù)之和,有以下兩種寫(xiě)法:
c=$((a+b)) # 賦值方式1
((c = a+b)) # 賦值方式2
- 輸出重定向
# 將標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出重定向到run.log中
./binary >run.log 2>&1
# 更簡(jiǎn)潔的寫(xiě)法:注意&和>之間不能有空格
sh mybash.sh &>run.log
- 用exec創(chuàng)建文件描述符
exec 4 < log.txt
exec 5 > log.txt
exec 6 >> log.txt
- 哈希數(shù)組
# 聲明
declare -a map
# 定義
${map[key]}=value
# 獲取所有key
${!map[@]}
declare -a HTTP_RESPONSE=(
[200]="OK"
[400]="Bad Request"
[403]="Forbidden"
[404]="Not Found"
[405]="Method Not Allowed"
[500]="Internal Server Error"
)
- date獲取時(shí)間
# 顯示秒數(shù)
date +%s
# 將秒數(shù)轉(zhuǎn)成字符串
date -d @秒數(shù)
11 tput控制終端光標(biāo)
# 設(shè)置光標(biāo)位置
tput cup 行號(hào) 列號(hào)
# 保存光標(biāo)位置
tput sc
# 回到保存的光標(biāo)位置
tput rc
# 清除當(dāng)前光標(biāo)到行尾
tput ed
- read讀取標(biāo)準(zhǔn)輸入
# 設(shè)置不回顯
read -s
# 設(shè)置超時(shí)時(shí)間
read -t
# 設(shè)置讀入字節(jié)數(shù)
read -n 字節(jié)數(shù)
# 設(shè)置提示
read -p "please input passwd"
- cat顯示文件內(nèi)容
# 顯示時(shí)忽略多余的空行
cat -s file
# 顯示每一行的行號(hào)
cat -n file
# 打印出制表符
cat -T file
- 用find進(jìn)行文件搜索
# 打印符合搜索條件的文件或目錄
find <path> -name XXX -print
# 刪除符合條件的文件或目錄
find <path> -name XXX -exec rm -rf {} \;
- xargs:從標(biāo)準(zhǔn)輸入拼裝命令
# 常用用法
cat file | xargs
# 設(shè)定每行的字段數(shù)
cat file | xargs -n 3
# 以null為換行符
cat file | xargs -0
# 代替命令中位置
cat file | xargs -I{} rm -rf {}
- tr: 替換或刪除標(biāo)準(zhǔn)輸入的字符
# 替換
tr 字符集1 字符集2
# 刪除
tr -d 字符集
# 補(bǔ)集
tr -d -c 要留下的字符集
# 去除重復(fù)空格
tr -s " "
- md5sum: 對(duì)文件生成md5 checksum
# 生成md5值
md5sum file > file.md5
# 校驗(yàn)
md5sum -c file.md5
- sort: 對(duì)文件內(nèi)容進(jìn)行排序
# 按照字符串排序
cat file | sort
#按照數(shù)字排序
cat file | sort -n
# 逆序排序
cat file | sort -r
# 按照某列排序
cat file | sort -k 列數(shù)
- dd:復(fù)制并轉(zhuǎn)換文件
# 從/dev/zero生成文件
dd if=/dev/zero of=data.file bs=100k count=1
- split:分割大文件
# 將文件分成大小為10k的小文件,且小文件命名中后綴長(zhǎng)度為3
split data.file -b 10k -a 3
- 字符串分割
# 獲取文件后綴
echo ${filename##*.}
# 獲取文件名
echo ${filename%.*}
- 單詞表
cat /usr/share/dict/words
- expect: 自動(dòng)化交互式輸入
# expect
spawn ./interactive.sh
expect "Password:"
send "XXX\n"
- comm: 求兩個(gè)文件的交集/差集/并集
# 求差集a-b
comm a.txt b.txt -1
# 求a+b并集
comm a.txt b.txt
# 求ab交集
comm a.txt b.txt -1 -2
- chattr: 修改文件屬性
# 將文件設(shè)置為不可修改
chattr +i file
# 去除不可修改屬性
chattr -i file
- 尋找軟連接
# 方法一
ll -rt | grep -P "^l" | awk '{print $8}'
# 方法二
find . -type l
- diff: 比對(duì)兩個(gè)文件(或目錄)
diff -abru 1.txt 2.txt
diff -abru 1.txt 2.txt > 12.patch
- patch: 對(duì)文件打補(bǔ)丁
patch -p1 1.txt < 12.patch
- 命令行當(dāng)前目錄管理
# pushd: 將當(dāng)前目錄壓入棧中
# popd: 從棧中彈出目錄,并作為當(dāng)前目錄
# dirs: 顯示棧中目錄
# 常規(guī)用法
pushd directory
popd
# 指定目錄
dirs
pushd +N
popd +N
- grep: 搜索文件內(nèi)容
# 匹配內(nèi)容顯示顏色
grep --color=auto
# 統(tǒng)計(jì)匹配行數(shù)
grep -c
# 顯示匹配行號(hào)
grep -n
# 顯示匹配字符偏移量
grep -o -b
# 顯示匹配文件列表
grep -l 1.txt 2.txt
# 使用文件匹配
grep -f pattern.txt file.txt
# 靜默模式
grep -q
# 定位代碼
grep -R -n
# 忽略大小寫(xiě)
grep -i
# 匹配多個(gè)樣式
grep -e pattern1 -e pattern2
# 顯示匹配行之前N行
grep -A
# 顯示匹配行之后N行
grep -B
# 顯示匹配行之前之后N行
grep -C
- sed: 文本過(guò)濾和轉(zhuǎn)換工具
# sed "s///" 替換
echo "hello wolrd" | sed "s/hello/goodbye/"
# sed -i 替換文件內(nèi)容
sed -i 's/hello/goodbye/' 1.txt
# sed 's///g' 替換所有匹配內(nèi)容
echo "thisthisthisthis" | sed 's/this/THIS/g'
# sed 's///ng' 從第n處匹配開(kāi)始替換
echo "thisthisthisthis" | sed 's/this/THIS/2g'
# sed '//d' 刪除匹配行
cat diff.sh | sed '/^$/d'
# 匹配字符串標(biāo)記
echo this is an example | sed 's/\w\+/[&]/g'
# 捕捉字符串
echo "this is a digit 7 in a numbger" | sed 's/digit \([0-9]\)/\1/'
- paste: 合并文件
# 按列合并文件
paste file1 file2
paste file1 file2 -d ","
- rev: 字符串逆序輸出
echo "1234" | rev
- tac: 對(duì)多行文件逆序輸出
# 行逆序
seq 1 10 | tac
- 設(shè)置命令行編輯模式
set -o vi
推薦閱讀
- STL源碼分析--vector
- STL源碼分析--hashtable
- STL源碼分析--algorithm
- zookeeper client原理總結(jié)
- redis實(shí)現(xiàn)分布式鎖
- 推薦幾個(gè)好用的效率神器
- C/C++關(guān)鍵字之restrict
- 現(xiàn)代C++之右值語(yǔ)義
- Python亂碼九問(wèn)
更多精彩內(nèi)容伐厌,請(qǐng)掃碼關(guān)注微信公眾號(hào):后端技術(shù)小屋晤锹。如果覺(jué)得文章對(duì)你有幫助的話,請(qǐng)多多分享衣陶、轉(zhuǎn)發(fā)拯欧、在看兜材。