標籤:abs bash-shell advanced
我喜歡的一句話
the only way to really learn scripting is to write scripts
學習指令碼的唯一方法就是寫指令碼
更好的命令列參數檢測
E_WRONGARGS=85 # Non-numerical argument (bad argument format).
41 #
42 # case "$1" in
43 # "" ) lines=50;;
44 # *[!0-9]*) echo "Usage: `basename $0` lines-to-cleanup";
參數中間沒有數字,無效參數
45 # exit $E_WRONGARGS;;
46 # * ) lines=$1;;
47 # esac
更有效目錄檢測
3 # cd /var/log || {
64 # echo "Cannot change to necessary directory." >&2
65 # exit $E_XCD;
66 # }
#!/bin/sh invokes the default shell interpreter, which
defaults to /bin/bash on a Linux machine.
#!/bin/bash 叫做sha-bang magic number
測試調用的參數數量是否正確
1 E_WRONG_ARGS=85
2 script_parameters="-a -h -m -z"
3 # -a = all, -h = help, etc.
4
5 if [ $# -ne $Number_of_expected_args ]
6 then
7 echo "Usage: `basename $0` $script_parameters"
8 # `basename $0` is the script‘s filename.
9 exit $E_WRONG_ARGS
10 fi
呼叫指令碼
bash scriptname
chmod 555 scriptname (gives everyone read/execute permission) [9]
or
chmod +rx scriptname (gives everyone read/execute permission)
chmod u+rx scriptname (gives only the script owner read/execute permission)
./scriptname
1 #!/bin/rm
2 # Self-deleting script.
echo "This line will never print (betcha!)."
這是一個自殺的指令碼
# Nothing much seems to happen when you run this...
except that the file disappears.
將文檔改成#!/bin/more 並且添加可執行許可權
結果就是自列表文檔 類似cat XX | more
特殊字元
# 注釋 ,行開頭#注釋,不被執行,取消文法檢測
在引號和逃逸符裡面不是注釋
6 echo ${PATH#*:} # Parameter substitution, not a comment.
7 echo $(( 2#101011 )) # Base conversion, not a comment.
; 命令分隔字元
1 echo hello; echo there
;; case選項分隔字元
1 case "$variable" in
2 abc) echo "\$variable = abc" ;;
. 相當於source 重新整理設定檔,重新載入
作為檔案名稱的一部分,隱藏檔案
相當於目前的目錄,..相當於上級目錄
在Regex中匹配單個字元
" 部分引用或弱引用,抑制大部分的特殊字元
‘ 強引用,抑制所有的特殊字元
, 連結一串算數操作,只返回最後的結果
1 let "t2 = ((a = 9, 15 / 3))"
2 # Set "a = 9" and "t2 = 15 / 3"
連結字串
1 for file in /{,usr/}bin/*calc
2 # ^ Find all executable files ending in "calc"
3 #+ in /bin and /usr/bin directories.
\ 逃逸字元,表達字元字面值的意思
/ 檔案路徑分隔字元
` 輸出命令結果給變數
: 不做任何事,預留位置
:>將檔案長度改為0,並且不改變許可權,不在則建立
: > data.xxx # File "data.xxx" now empty.
Same effect as cat /dev/null >data.xxx
也可作為域分隔字元 在/etcpasswd中
! 轉換退出狀態或者測試的感覺
change the sense of equal ( = ) to not-equal ( != )
也可調用命令曆史
* 通配
在正則中匹配0個或多個字元
在算數中 單個表示乘號 兩個表示階乘
? 在雙括弧中,?作為三元操作符
(( var0 = var1<98?9:21 ))
在通配和正則中代表單個字元
$ 變數
跟變數名表示變數的值
在正則中表示一行的結尾
[email protected] $* 位置參數
$? 退出狀態的變數
$$ 表示當前指令碼的進程ID
() 命令組,括弧中的命令是子shell,對外面不可見
數組初始化
1 Array=(element1 element2 element3)
{} 命令擴充
8 cp file22.{txt,backup}
9 # Copies "file22.txt" to "file22.backup"
----
echo {file1,file2}\ :{\ A," B",‘ C‘}
file1 : A file1 : B file1 : C file2 : A file2 : B
file2 : C
----
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
----
8 base64_charset=( {A..Z} {a..z} {0..9} + / = )
9 # Initializing an array, using extended brace expansion.
表示代碼塊
對指令碼其他地方可見,非子shell
1 a=123
2 { a=321; }
3 echo "a = $a" # a = 321 (value inside code block)
也可作為預留位置
ls . | xargs -i -t cp ./{} $1
[] 表示測試
數組元素
1 Array[1]=slot_1
2 echo ${Array[1]}
表示字元範圍
$(()) 整數運算式
1 a=3
2 b=7
3
4 echo $(($a+$b)) # 10
> &> >& >> < <> 重新導向操作符
< > 作為ASCII碼比較
\<,\> 單詞錨定
| 管道
Passes the output (stdout) of a previous command to the input (stdin) of the next one
管道作為子shell運行,因此不能修改父shell的變數
>| 強制重新導向
|| 邏輯或
& 後台運行job
&& 邏輯與
^ 行頭部匹配
備份目前的目錄最近24小時內修改的檔案
1 #!/bin/bash
2
3 # Backs up all files in current directory modified within last 24 hours
4 #+ in a "tarball" (tarred and gzipped file).
5
6 BACKUPFILE=backup-$(date +%m-%d-%Y)
7 # Embeds date in backup filename.
8 # Thanks, Joshua Tschida, for the idea.
9 archive=${1:-$BACKUPFILE}
10 # If no backup-archive filename specified on command-line,
11 #+ it will default to "backup-MM-DD-YYYY.tar.gz."
12
13 tar cvf - `find . -mtime -1 -type f -print` > $archive.tar
14 gzip $archive.tar
15 echo "Directory $PWD backed up in archive file \"$archive.tar.gz\"."
16
17
18 # Stephane Chazelas points out that the above code will fail
19 #+ if there are too many files found
20 #+ or if any filenames contain blank characters.
21
22 # He suggests the following alternatives:
23 # -------------------------------------------------------------------
24 # find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar"
25 # using the GNU version of "find".
26
27
28 # find . -mtime -1 -type f -exec tar rvf "$archive.tar" ‘{}‘ \;
29 # portable to other UNIX flavors, but much slower.
30 # -------------------------------------------------------------------
31
32
33 exit 0
本文出自 “Linux is belong to you” 部落格,請務必保留此出處http://jwh5566.blog.51cto.com/7394620/1640293
Advanced Bash-Shell Guide(Version 10) 學習筆記一