shell 程式設計
變數定義:name=Tom
變數使用:echo $name
自定義環境變數:export name
常見環境變數:$HOME $PATH
檢視環境變數:env
宣告只讀變數:readonly name
特殊變數:
$0shell執行程式名 $n位置引數,n = 1...9 $*所有位置引數組成的字串 $#位置引數的個數 $$程式執行後的pid,常用來生產臨時檔案 $!最後一個後臺程式執行後的pid $?命令執行後的返回值,0表示成功,其他失敗
位置引數只能有9個,可以用shift命令將位置引數後移一格
set命令可以改變位置引數,執行set “p1 p2 p3”後,$0=p1 $1=p2 $2=p3
數值運算
整數運算
# 4種方式實現自增 i=0 ((i++)) let i++ expr $i + 1 echo $i 1 | awk '{printf($1+$2);}'
浮點運算
(()),let和expr都無法進行浮點運算,bc和awk可以
echo "10 3" | awk '{printf("%0.3f", $1/$2)}'
隨機數
環境變數RANDOM產生0到32767的隨機數,而awk的rand函式可以產生0到1之間的隨機數
echo $RANDOM echo "" | awk '{srand(); printf("%f", rand());}'
序列數
可以用迴圈實現,建議用seq工具
seq 5# 1 2 3 4 5 seq 1 2 5# 1 3 5 seq -s: 1 2 5# 1:3:5 seq -w 1 3 10# 01 04 07 10 {1..5}# 1 2 3 4 5
函式
可以有return語句,但是隻能返回整數
# 定義hello函式 hello () { echo "hello $1" } hello hatlonely# 呼叫hello,hatlonely為引數
控制語句
#!/bin/bash # 遍歷目錄 for file in `ls`; do if [ -f $file ]; then echo "$file is a file" else echo "$file is not a file" fi done # for迴圈 for ((i = 0; i < 10; i++)) do echo $i done # while迴圈 i=0 while [ $i -lt 10 ]; do echo $i ((i++)) done # 處理命令列引數 usage() { echo "usage: $0 [-a] [-e <admin>] [-f <serverfile>] [-h] [-d <domain>] [-s <whois_server>] [-q] [-x <warndays>]" } while getopts ae:f:hd:s:qx: option; do case "${option}" in a) ALARM="TRUE";; e) ADMIN=${OPTARG};; d) DOMAIN=${OPTARG};; f) SERVERFILE=$OPTARG;; s) WHOIS_SERVER=$OPTARG;; q) QUIET="TRUE";; x) WARNDAYS=$OPTARG;; \?) usage; exit 1;; esac done echo "ALARM=$ALARM" echo "ADMIN=$ADMIN" # 讀取輸入 while read line; do echo $line done
字串
判斷字串的值
${var}變數var的值, 與$var相同 ${var-DEFAULT}如果var沒有被宣告, 那麼就以$DEFAULT作為其值 * ${var:-DEFAULT} 如果var沒有被宣告, 或者其值為空, 那麼就以$DEFAULT作為其值 * ${var=DEFAULT}如果var沒有被宣告, 那麼就以$DEFAULT作為其值 * ${var:=DEFAULT} 如果var沒有被宣告, 或者其值為空, 那麼就以$DEFAULT作為其值 * ${var+OTHER}如果var聲明瞭, 那麼其值就是$OTHER, 否則就為null字串 ${var:+OTHER}如果var被設定了, 那麼其值就是$OTHER, 否則就為null字串 ${var?ERR_MSG}如果var沒被宣告, 那麼就列印$ERR_MSG * ${var:?ERR_MSG} 如果var沒被設定, 那麼就列印$ERR_MSG * ${!varprefix*}匹配之前所有以varprefix開頭進行宣告的變數 ${!varprefix@}匹配之前所有以varprefix開頭進行宣告的變數
字串操作
${#string}$string的長度 ${string:position}在$string中, 從位置$position開始提取子串 ${string:position:length}在$string中, 從位置$position開始提取長度為$length的子串 ${string#substring}從變數$string的開頭, 刪除最短匹配$substring的子串 ${string##substring}從變數$string的開頭, 刪除最長匹配$substring的子串 ${string%substring}從變數$string的結尾, 刪除最短匹配$substring的子串 ${string%%substring}從變數$string的結尾, 刪除最長匹配$substring的子串 ${string/substring/replacement}使用$replacement, 來代替第一個匹配的$substring ${string//substring/replacement}使用$replacement, 代替所有匹配的$substring ${string/#substring/replacement}如果$string的字首匹配$substring, 那麼就用$replacement來代替匹配到的$substring ${string/%substring/replacement}如果$string的字尾匹配$substring, 那麼就用$replacement來代替匹配到的$substring
條件測試
使用[]測試時,前後都要空格
檔案測試
-a file exists. -b file exists and is a block special file. -c file exists and is a character special file. -d file exists and is a directory. -e file exists (just the same as -a). -f file exists and is a regular file. -g file exists and has its setgid(2) bit set. -G file exists and has the same group ID as this process. -k file exists and has its sticky bit set. -L file exists and is a symbolic link. -n string length is not zero. -o Named option is set on. -O file exists and is owned by the user ID of this process. -p file exists and is a first in, first out (FIFO) special file or named pipe. -r file exists and is readable by the current process. -s file exists and has a size greater than zero. -S file exists and is a socket. -t file descriptor number fildes is open and associated with a terminal device. -u file exists and has its setuid(2) bit set. -w file exists and is writable by the current process. -x file exists and is executable by the current process. -z string length is zero.
整數比較
-eq== -ne!= -ge>= -gt> -le<= -lt<
字串測試
string測試字串不為空 -n string測試字串不為空 -z string測試字串為空 str1=str2字串相同 str1!=str2字串不同
邏輯運算
!expressionnot expr1 -a expr2and expr1 -o expr2or
路徑處理
$ temp=/home/hatlonely/hatlonely/librayrs.txt $ echo `basename ${temp}` $ echo `dirname ${temp}` $ echo ${temp##*/} $ echo ${temp%/*}
顏色
echo "/033[字背景顏色;字型顏色m字串/033[控制碼"
如果單純顯示字型顏色可以固定控制碼位0m
。
例如:echo "/033[字背景顏色;字型顏色m字串/033[0m"
字背景顏色範圍:40 - 49
40:黑 41:深紅 42:綠 43:黃色 44:藍色 45:紫色 46:深綠 47:白色
字顏色:30 - 39
30:黑 31:紅 32:綠 33:黃 34:藍色 35:紫色 36:深綠 37:白色
ANSI控制碼
\33[0m 關閉所有屬性 \33[01m 設定高亮度 \33[04m 下劃線 \33[05m 閃爍 \33[07m 反顯 \33[08m 消隱 \33[30m -- \33[37m 設定前景色 \33[40m -- \33[47m 設定背景色 \33[nA 游標上移n行 \33[nB 游標下移n行 \33[nC 游標右移n行 \33[nD 游標左移n行 \33[y;xH設定游標位置 \33[2J 清屏 \33[K 清除從游標到行尾的內容 \33[s 儲存游標位置 \33[u 恢復游標位置 \33[?25l 隱藏游標 \33[?25h 顯示游標