1.shell 腳本是區分小寫的
2.Unix特殊字符有: ( ; $ ? & * () [] ` ‘ “ + 使用其時要進行轉義()
3.Shell的注釋以#開頭
4.函數的定義
Function fuction_name(){
Command to execute
}
調用時直接用function_name.
5.控制結構
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跳出整個循環體,然后執行循環體外接下來的代碼;
Continue 結束本次循環,繼續下次循環;
Exit 退出整個腳本,一般在其后加入一個整數(如exit 0),作為返回代碼發送給系統;
Return 用于在函數中返回數據,或返回一個結果給調用函數
7.here文檔
用于將輸入重定向到某個交互式shell腳本或程序,而不需要用戶介入。
Program_name << LABLE
Program_input_1
Program_input_2
.
.
Program_input_#
LABLE
注意,程序輸入行中的LABLE標記之間是沒有空白的,且輸入的必須是程序所期望的準確數據,否則可能會失效。
8.符號命令
( ) 在一個子shell中運行括號所括起來的命令
(( )) 在某個shell中對變量進行求值和賦值,并進行數學運算
$(( )) 對括起來的表達式進行求值
[ ] 與test命令相同
[[ ]] 用于字符串比較
$( ) 命令替換
` ` 命令替換
9.命令行參數
命令行參數$0,$1,$2,...,$9是位置參數,$0指向的是命令本身。
命令shift用于位置參數向左移動,如shift命令命令$2成為$1。Shift加入一個數字來移動多個位置,如shift 3使得$4成為$1。shift是一種按照參數列出順序來處理每個位置參數的良好方式。
10.特殊參數
$* 指定所有的命令行參數,與$@的意義一樣。兩者只有在加雙引號時意義不同,如
“$*”將整個參數列表作為一個參數來獲取,”$@”獲取整個參數列表,并將它分隔成不同的參數。
$? 檢查返回代碼。一個成功執行完的命令返回代碼為0,不成功是一個非0值。
11.雙引號,單引號和 `(esc下面的按鍵)
單引號''對內容進行全引用,也就是說,對變量工命令語句使用文字正文,不進行任何替換;而雙引號則進行部分引用,則允許字符替換或命令替換。
新聞熱點
疑難解答