更多精彩內容,請關注微信公衆號:後端技術小屋git
《Linux Shell腳本攻略》是一本適合初學者系統學習Bash Shell的書籍,牆裂推薦。如下是我閱讀這本書的筆記,但願對讀者有用。redis
echo -e "\e[1:41m" # 1表示背景色
echo "hello, bash!" # 報錯,由於!在shell中屬於特殊字符,須要轉義 echo "hello, bash\!" # 不報錯,!已轉義 echo 'hello, bash!' # 不報錯,在單引號中!無需轉移
# 用法相似C中的`printf` printf "format" var1 var2 ...
echo -n # 不在字符串末尾添加換行符 echo -e # 支持轉義字符表示 echo -e "\e[id XXXXXX" # 顯示顏色字體,其中id表示背景顏色id
# 根據進程名獲取進程id pgrep <進程名> # 至關於ps -ef cat /proc/$PID/environ # 查看進程運行過程當中的環境變量
# 計算兩個數之和,有如下兩種寫法: c=$((a+b)) # 賦值方式1 ((c = a+b)) # 賦值方式2
# 將標準輸出和標準錯誤輸出重定向到run.log中 ./binary >run.log 2>&1 # 更簡潔的寫法:注意&和>之間不能有空格 sh mybash.sh &>run.log
exec 4 < log.txt exec 5 > log.txt exec 6 >> log.txt
# 聲明 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 +%s # 將秒數轉成字符串 date -d @秒數
11 tput控制終端光標shell
# 設置光標位置 tput cup 行號 列號 # 保存光標位置 tput sc # 回到保存的光標位置 tput rc # 清除當前光標到行尾 tput ed
# 設置不回顯 read -s # 設置超時時間 read -t # 設置讀入字節數 read -n 字節數 # 設置提示 read -p "please input passwd"
# 顯示時忽略多餘的空行 cat -s file # 顯示每一行的行號 cat -n file # 打印出製表符 cat -T file
# 打印符合搜索條件的文件或目錄 find <path> -name XXX -print # 刪除符合條件的文件或目錄 find <path> -name XXX -exec rm -rf {} \;
# 經常使用用法 cat file | xargs # 設定每行的字段數 cat file | xargs -n 3 # 以null爲換行符 cat file | xargs -0 # 代替命令中位置 cat file | xargs -I{} rm -rf {}
# 替換 tr 字符集1 字符集2 # 刪除 tr -d 字符集 # 補集 tr -d -c 要留下的字符集 # 去除重複空格 tr -s " "
# 生成md5值 md5sum file > file.md5 # 校驗 md5sum -c file.md5
# 按照字符串排序 cat file | sort #按照數字排序 cat file | sort -n # 逆序排序 cat file | sort -r # 按照某列排序 cat file | sort -k 列數
# 從/dev/zero生成文件 dd if=/dev/zero of=data.file bs=100k count=1
# 將文件分紅大小爲10k的小文件,且小文件命名中後綴長度爲3 split data.file -b 10k -a 3
# 獲取文件後綴 echo ${filename##*.} # 獲取文件名 echo ${filename%.*}
cat /usr/share/dict/words
# expect spawn ./interactive.sh expect "Password:" send "XXX\n"
# 求差集a-b comm a.txt b.txt -1 # 求a+b並集 comm a.txt b.txt # 求ab交集 comm a.txt b.txt -1 -2
# 將文件設置爲不可修改 chattr +i file # 去除不可修改屬性 chattr -i file
# 方法一 ll -rt | grep -P "^l" | awk '{print $8}' # 方法二 find . -type l
diff -abru 1.txt 2.txt diff -abru 1.txt 2.txt > 12.patch
patch -p1 1.txt < 12.patch
# pushd: 將當前目錄壓入棧中 # popd: 從棧中彈出目錄,並做爲當前目錄 # dirs: 顯示棧中目錄 # 常規用法 pushd directory popd # 指定目錄 dirs pushd +N popd +N
# 匹配內容顯示顏色 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
# 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/'
# 按列合併文件 paste file1 file2 paste file1 file2 -d ","
echo "1234" | rev
# 行逆序 seq 1 10 | tac
set -o vi
推薦閱讀後端
更多精彩內容,請掃碼關注微信公衆號:後端技術小屋。若是以爲文章對你有幫助的話,請多多分享、轉發、在看。
數組