《Linux Shell腳本攻略》讀書筆記

更多精彩內容,請關注微信公衆號:後端技術小屋git

《Linux Shell腳本攻略》是一本適合初學者系統學習Bash Shell的書籍,牆裂推薦。如下是我閱讀這本書的筆記,但願對讀者有用。redis

  1. 輸出顏色字符
echo -e "\e[1:41m" # 1表示背景色
  1. echo打印!需轉義
echo "hello, bash!"  # 報錯,由於!在shell中屬於特殊字符,須要轉義
echo "hello, bash\!" # 不報錯,!已轉義
echo 'hello, bash!'  # 不報錯,在單引號中!無需轉移
  1. printf可用於格式化輸出
# 用法相似C中的`printf`
printf "format"  var1 var2 ...
  1. echo經常使用選項
echo -n  # 不在字符串末尾添加換行符

echo -e  # 支持轉義字符表示
echo -e "\e[id XXXXXX" # 顯示顏色字體,其中id表示背景顏色id
  1. pgrep
# 根據進程名獲取進程id
pgrep <進程名>           # 至關於ps -ef  

cat /proc/$PID/environ  # 查看進程運行過程當中的環境變量
  1. 用(())進行算數運算
# 計算兩個數之和,有如下兩種寫法:
c=$((a+b))   # 賦值方式1
((c = a+b))  # 賦值方式2
  1. 輸出重定向
# 將標準輸出和標準錯誤輸出重定向到run.log中
./binary >run.log 2>&1 

# 更簡潔的寫法:注意&和>之間不能有空格 
sh mybash.sh &>run.log
  1. 用exec建立文件描述符
exec 4 < log.txt
exec 5 > log.txt
exec 6 >> log.txt
  1. 哈希數組
# 聲明
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"
)
  1. date獲取時間
# 顯示秒數
date +%s 

# 將秒數轉成字符串
date -d @秒數

11 tput控制終端光標shell

# 設置光標位置
tput cup 行號  列號

# 保存光標位置
tput sc 

# 回到保存的光標位置
tput rc

# 清除當前光標到行尾
tput ed
  1. read讀取標準輸入
# 設置不回顯
read -s
# 設置超時時間 
read -t 
# 設置讀入字節數
read -n 字節數
# 設置提示
read -p "please input passwd"
  1. cat顯示文件內容
# 顯示時忽略多餘的空行 
cat -s file

# 顯示每一行的行號
cat -n file 

# 打印出製表符
cat -T file
  1. 用find進行文件搜索
# 打印符合搜索條件的文件或目錄
find <path> -name XXX  -print 

# 刪除符合條件的文件或目錄
find  <path> -name XXX -exec rm -rf {} \;
  1. xargs:從標準輸入拼裝命令
# 經常使用用法
cat file | xargs

# 設定每行的字段數
cat file | xargs -n 3 

# 以null爲換行符
cat file | xargs -0

# 代替命令中位置
cat file | xargs -I{} rm -rf {}
  1. tr: 替換或刪除標準輸入的字符
# 替換 
tr  字符集1 字符集2 

# 刪除
tr -d 字符集

# 補集
tr -d -c 要留下的字符集

# 去除重複空格
tr -s " "
  1. md5sum: 對文件生成md5 checksum
# 生成md5值
md5sum file  > file.md5

# 校驗 
md5sum -c file.md5
  1. sort: 對文件內容進行排序
# 按照字符串排序 
cat file | sort 

#按照數字排序
cat file | sort -n 

# 逆序排序
cat file | sort -r

# 按照某列排序 
cat file | sort -k 列數
  1. dd:複製並轉換文件
# 從/dev/zero生成文件 
dd if=/dev/zero of=data.file bs=100k count=1
  1. split:分割大文件
# 將文件分紅大小爲10k的小文件,且小文件命名中後綴長度爲3
split data.file -b 10k -a 3
  1. 字符串分割
# 獲取文件後綴
echo ${filename##*.}

# 獲取文件名
echo ${filename%.*}
  1. 單詞表
cat /usr/share/dict/words
  1. expect: 自動化交互式輸入
# expect 
spawn  ./interactive.sh 
expect "Password:"
send "XXX\n"
  1. comm: 求兩個文件的交集/差集/並集
# 求差集a-b
comm a.txt b.txt -1

# 求a+b並集
comm a.txt b.txt 

# 求ab交集
comm a.txt b.txt -1 -2
  1. chattr: 修改文件屬性
# 將文件設置爲不可修改
chattr +i file
# 去除不可修改屬性
chattr -i file
  1. 尋找軟鏈接
# 方法一
ll -rt | grep -P "^l" | awk '{print $8}'

# 方法二
find . -type l
  1. diff: 比對兩個文件(或目錄)
diff -abru 1.txt 2.txt 
diff -abru 1.txt 2.txt > 12.patch
  1. patch: 對文件打補丁
patch -p1 1.txt < 12.patch
  1. 命令行當前目錄管理
# pushd: 將當前目錄壓入棧中
# popd: 從棧中彈出目錄,並做爲當前目錄
# dirs: 顯示棧中目錄

# 常規用法
pushd  directory
popd

# 指定目錄
dirs
pushd +N
popd +N
  1. grep: 搜索文件內容
# 匹配內容顯示顏色
grep --color=auto 

# 統計匹配行數
grep -c 

# 顯示匹配行號
grep -n 

# 顯示匹配字符偏移量
grep -o -b

# 顯示匹配文件列表
grep -l  1.txt 2.txt

# 使用文件匹配
grep -f pattern.txt  file.txt

# 靜默模式
grep -q 

# 定位代碼
grep -R -n 

# 忽略大小寫
grep -i 

# 匹配多個樣式
grep -e pattern1 -e pattern2

# 顯示匹配行以前N行
grep -A 

# 顯示匹配行以後N行
grep -B 

# 顯示匹配行以前以後N行
grep -C
  1. sed: 文本過濾和轉換工具
# sed "s///" 替換
echo  "hello wolrd" | sed "s/hello/goodbye/"

# sed -i 替換文件內容
sed -i 's/hello/goodbye/' 1.txt

# sed 's///g' 替換全部匹配內容
echo "thisthisthisthis" | sed 's/this/THIS/g'

# sed 's///ng' 從第n處匹配開始替換
echo "thisthisthisthis" | sed 's/this/THIS/2g'

# sed '//d' 刪除匹配行
cat diff.sh | sed '/^$/d' 

# 匹配字符串標記
echo this is an example | sed 's/\w\+/[&]/g'

# 捕捉字符串
echo "this is a digit 7 in a numbger" | sed 's/digit \([0-9]\)/\1/'
  1. paste: 合併文件
# 按列合併文件
paste file1 file2
paste file1 file2 -d  ","
  1. rev: 字符串逆序輸出
echo "1234" | rev
  1. tac: 對多行文件逆序輸出
# 行逆序
seq 1 10 | tac
  1. 設置命令行編輯模式
set -o vi

推薦閱讀後端

更多精彩內容,請掃碼關注微信公衆號:後端技術小屋。若是以爲文章對你有幫助的話,請多多分享、轉發、在看。
二維碼數組

相關文章
相關標籤/搜索