1.shell 腳本是區(qū)分小寫的
2.Unix特殊字符有: ( ; $ ? & * () [] ` ‘ “ + 使用其時要進行轉(zhuǎn)義()
3.Shell的注釋以#開頭
4.函數(shù)的定義
Function fuction_name(){
Command to execute
}
調(diào)用時直接用function_name.
5.控制結(jié)構(gòu)
1)If...then語句
If [ test_command ]
Then
Commands
if
2)If...then...else語句
If [ test_command ]
Then
Commands
Else
commands
if
3)If...then...elif...then...(else)語句
If [ test_command ]
Then
Commands
Elif [ test_command ]
Then
Commands
Else
Commands
Fi
4)for ... In語句
For loop_varible in argument_list
Do
Commands
done
5)while語句
While test_command_is_true
Do
Commands
Done
6)until 語句
Until test_command_is_true
Do
Commands
Done
7)case語句
Case $variable in
Match_1)
Commands_for_1
Match_2)
Commands_for_2
.
.
.
*) #option for other values
Commands_for_no_match
esac
6.break、continue、exit和return語句
Break跳出整個循環(huán)體,然后執(zhí)行循環(huán)體外接下來的代碼;
Continue 結(jié)束本次循環(huán),繼續(xù)下次循環(huán);
Exit 退出整個腳本,一般在其后加入一個整數(shù)(如exit 0),作為返回代碼發(fā)送給系統(tǒng);
Return 用于在函數(shù)中返回數(shù)據(jù),或返回一個結(jié)果給調(diào)用函數(shù)
7.here文檔
用于將輸入重定向到某個交互式shell腳本或程序,而不需要用戶介入。
Program_name << LABLE
Program_input_1
Program_input_2
.
.
Program_input_#
LABLE
注意,程序輸入行中的LABLE標記之間是沒有空白的,且輸入的必須是程序所期望的準確數(shù)據(jù),否則可能會失效。
8.符號命令
( ) 在一個子shell中運行括號所括起來的命令
(( )) 在某個shell中對變量進行求值和賦值,并進行數(shù)學(xué)運算
$(( )) 對括起來的表達式進行求值
[ ] 與test命令相同
[[ ]] 用于字符串比較
$( ) 命令替換
` ` 命令替換
9.命令行參數(shù)
命令行參數(shù)$0,$1,$2,...,$9是位置參數(shù),$0指向的是命令本身。
命令shift用于位置參數(shù)向左移動,如shift命令命令$2成為$1。Shift加入一個數(shù)字來移動多個位置,如shift 3使得$4成為$1。shift是一種按照參數(shù)列出順序來處理每個位置參數(shù)的良好方式。
10.特殊參數(shù)
$* 指定所有的命令行參數(shù),與$@的意義一樣。兩者只有在加雙引號時意義不同,如
“$*”將整個參數(shù)列表作為一個參數(shù)來獲取,”$@”獲取整個參數(shù)列表,并將它分隔成不同的參數(shù)。
$? 檢查返回代碼。一個成功執(zhí)行完的命令返回代碼為0,不成功是一個非0值。
11.雙引號,單引號和 `(esc下面的按鍵)
單引號''對內(nèi)容進行全引用,也就是說,對變量工命令語句使用文字正文,不進行任何替換;而雙引號則進行部分引用,則允許字符替換或命令替換。
新聞熱點
疑難解答