目錄
Linux Shell
數據類型
變量類型
運算符
算術運算符
賦值運算符
拼接運算符
比較運算符
關系運算符
控制結構
順序結構
條件分支結構
if 條件語句
case 分支語句?
循環結構
for 循環
while 循環
until 循環
break 語句
continue語句
函數
函數定義?
函數名
函數體
返回值
參數
函數的局部性
簡單函數示例
函數的遞歸
實例操作
數組遍歷操作
九九乘法表
基本上,每一門編程語言,都能從數據類型、變量、運算符、控制結構、函數五個方面著手,初步掌握這些內容就可以快速入門為一名初級程序員。
Linux Shell
Shell是Linux命令行解釋器,主要用于執行操作系統命令和腳本。Linux Shell編程語言是一種用于與操作系統內核進行交互的命令行腳本語言,屬于解釋型、弱類型的動態語言。
數據類型
bool、數字、字符串、數組
變量類型
環境變量、用戶變量、全局變量、只讀變量
set let export readonly? env
運算符
算術運算符
+ - * / %
賦值運算符
=,沒有+=、-=、*=、/=這類復合賦值
拼接運算符
+= ,只能用于字符串的拼接
比較運算符
==、!=
關系運算符
-eq?? ?檢測兩個數是否相等,相等返回 true。
-ne?? ?檢測兩個數是否不相等,不相等返回 true。
-gt?? ?檢測左邊的數是否大于右邊的,如果是,則返回 true。
-lt?? ?檢測左邊的數是否小于右邊的,如果是,則返回 true。
-ge?? ?檢測左邊的數是否大于等于右邊的,如果是,則返回 true。
-le?? ?檢測左邊的數是否小于等于右邊的,如果是,則返回 true。
控制結構
順序結構
順序結構是最簡單的算法結構,語句與語句之間是按從上到下的順序進行的,它是由若干個依次執行的處理步驟組成的。
注:同一行可以有多個語句,只要用分號“;”來分隔即可。
條件分支結構
if 條件語句
可以細分為?if , if-else , if-elif-else 多種形式
hann@HannYang:~$ help -m if
NAMEif - Execute commands based on conditional.SYNOPSISif COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fiDESCRIPTIONExecute commands based on conditional.The `if COMMANDS' list is executed. If its exit status is zero, then the`then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list isexecuted in turn, and if its exit status is zero, the corresponding`then COMMANDS' list is executed and the if command completes. Otherwise,the `else COMMANDS' list is executed, if present. The exit status of theentire construct is the exit status of the last command executed, or zeroif no condition tested true.Exit Status:Returns the status of the last command executed.......
case 分支語句?
hann@HannYang:~$ help -m case
NAMEcase - Execute commands based on pattern matching.SYNOPSIScase WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esacDESCRIPTIONExecute commands based on pattern matching.Selectively execute COMMANDS based upon WORD matching PATTERN. The`|' is used to separate multiple patterns.Exit Status:Returns the status of the last command executed.......
循環結構
for 循環
hann@HannYang:~$ help -m for
NAMEfor - Execute commands for each member in a list.SYNOPSISfor NAME [in WORDS ... ] ; do COMMANDS; doneDESCRIPTIONExecute commands for each member in a list.The `for' loop executes a sequence of commands for each member in alist of items. If `in WORDS ...;' is not present, then `in "$@"' isassumed. For each element in WORDS, NAME is set to that element, andthe COMMANDS are executed.Exit Status:Returns the status of the last command executed.......
while 循環
hann@HannYang:~$ help -m while
NAMEwhile - Execute commands as long as a test succeeds.SYNOPSISwhile COMMANDS; do COMMANDS; doneDESCRIPTIONExecute commands as long as a test succeeds.Expand and execute COMMANDS as long as the final command in the`while' COMMANDS has an exit status of zero.Exit Status:Returns the status of the last command executed.......
until 循環
hann@HannYang:~$ help -m until
NAMEuntil - Execute commands as long as a test does not succeed.SYNOPSISuntil COMMANDS; do COMMANDS; doneDESCRIPTIONExecute commands as long as a test does not succeed.Expand and execute COMMANDS as long as the final command in the`until' COMMANDS has an exit status which is not zero.Exit Status:Returns the status of the last command executed.......
break 語句
hann@HannYang:~$ help -m break
NAMEbreak - Exit for, while, or until loops.SYNOPSISbreak [n]DESCRIPTIONExit for, while, or until loops.Exit a FOR, WHILE or UNTIL loop. If N is specified, break N enclosingloops.Exit Status:The exit status is 0 unless N is not greater than or equal to 1.......
continue語句
hann@HannYang:~$ help -m continue
NAMEcontinue - Resume for, while, or until loops.SYNOPSIScontinue [n]DESCRIPTIONResume for, while, or until loops.Resumes the next iteration of the enclosing FOR, WHILE or UNTIL loop.If N is specified, resumes the Nth enclosing loop.Exit Status:The exit status is 0 unless N is not greater than or equal to 1.......
兩者的區別?
break語句:
break語句用于退出本層循環,當執行到break會立即跳出當前循環,執行后續代碼。
在多層嵌套循環中,break只會跳出最近的一層循環。
continue語句:
continue語句用于結束本次循環,跳過本次循環中剩余的代碼,直接進入下一次循環。
在多層嵌套循環中,continue只會跳過最近的一層循環。
函數
hann@HannYang:~$ help -m function
NAMEfunction - Define shell function.SYNOPSISfunction name { COMMANDS ; } or name () { COMMANDS ; }DESCRIPTIONDefine shell function.Create a shell function named NAME. When invoked as a simple command,NAME runs COMMANDs in the calling shell's context. When NAME is invoked,the arguments are passed to the function as $1...$n, and the function'sname is in $FUNCNAME.Exit Status:Returns success unless NAME is readonly.
函數定義?
函數名
Shell函數用關鍵字 function 聲明,跟在后面的 name 即函數名。聲明后就用"函數名 [參數]"來調用函數。function 非必須,也能用函數名加一對括號?name() { ... } 來聲明定義函數。
函數體
函數名后的 { Commands; } 即函數體,是實現函數功能的主體。
返回值
Shell函數可以有一個返回值,可以使用return語句返回一個值。返回值的范圍是0到255之間,0表示成功,非零值表示錯誤。如果函數中沒有return語句,或者使用exit命令退出函數,則函數的返回值為退出命令的返回值。
參數
Shell函數可以通過參數接收輸入的值。在函數定義時,可以在括號中指定參數列表。參數可以在函數體中使用,也可以通過特殊變量$#獲取函數的參數個數,通過特殊變量$@獲取所有的參數。
函數的局部性
Shell函數的變量是局部的,即在函數內部定義的變量只在該函數內部可見,不會影響到函數外部的變量。如果要使用全局變量,需要在函數外部定義。
簡單函數示例
hann@HannYang:~$ function add {
> num1=$1
> num2=$2
> sum=$((num1 + num2))
> echo "The sum of $num1 and $num2 is $sum."
> }
hann@HannYang:~$ add 10 20
The sum of 10 and 20 is 30.
?語句比較少的函數可以在一行內完成,函數體中的語句間用分號“;”來分隔即可。
hann@HannYang:~$ function sub { num1=$1; num2=$2; sum=$((num1 - num2)); echo "The difference between $num1 and $num2 is $sum."; }
hann@HannYang:~$ sub 30 20
The difference between 30 and 20 is 10.
函數的遞歸
Shell函數可以遞歸調用自身,這在處理嵌套數據結構或遞歸算法時非常有用。需要注意的是,遞歸調用可能會導致棧溢出或效率低下的問題,因此在使用時需要謹慎。
示例:階乘函數
hann@HannYang:~$ factorial() {
> if [ $1 -le 1 ]
> then
> echo 1
> else
> echo $(( $1 * $(factorial $(( $1 - 1 ))) ))
i
}> fi
> }
hann@HannYang:~$ read -p "請輸入一個整數:" num
請輸入一個整數:6
hann@HannYang:~$ result=$(factorial $num)
hann@HannYang:~$ echo "$num 的階乘為 $result"
6 的階乘為 720
實例操作
數組遍歷操作
hann@HannYang:~$ for i in 1 2 3 4 5; do echo -n $i; done; echo
12345
hann@HannYang:~$ for i in {1..5}; do echo -n $i; done; echo
12345
hann@HannYang:~$ sum=0;for i in {1..100};do let sum+=i;done;echo $sum
5050
hann@HannYang:~$ for i in {1..5}{8..10}; do echo -n $i; done; echo
18191102829210383931048494105859510
hann@HannYang:~$ for i in {1..5}{8..10}; do echo -n $i" "; done; echo
18 19 110 28 29 210 38 39 310 48 49 410 58 59 510
hann@HannYang:~$ for i in {A..C}{a..d}; do echo -n $i" "; done; echo
Aa Ab Ac Ad Ba Bb Bc Bd Ca Cb Cc Cd
九九乘法表
hann@HannYang:~$ cat 99mul.sh
#!/bin/bashfor i in {1..9}
dofor j in {1..9}doif [ $j -le $i ]thenecho -n "$j*$i=$(($i*$j)) "fidoneecho ""
done
hann@HannYang:~$ bash 99mul.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
hann@HannYang:~$ cat 99mul.sh
#!/bin/bashfor i in {1..9}
dofor j in {1..9}doif [ $j -le $i ]thenprintf "%d*%d=%2d " $j $i $(($i*$j))fidoneecho ""
done
hann@HannYang:~$ bash 99mul.sh
1*1= 1
1*2= 2 2*2= 4
1*3= 3 2*3= 6 3*3= 9
1*4= 4 2*4= 8 3*4=12 4*4=16
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
hann@HannYang:~$ cat 99mul.sh?
#!/bin/bashfor i in {1..9}
dofor j in {1..9}doif [ $j -le $i ]thenif [ $j -eq 1 ]thenprintf "%d*%d=%d " $j $i $(($i*$j))elseprintf "%d*%d=%2d " $j $i $(($i*$j))fifidoneecho ""
done
hann@HannYang:~$ bash 99mul.sh
1*1=1
1*2=2 2*2= 4
1*3=3 2*3= 6 3*3= 9
1*4=4 2*4= 8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
本文簡單提一下Linux Shell編程語言的入門要點,之后再對各個環節進行分類詳細舉例說明。