1 If ... else ...
Basic Format:
If condition
Then
Commend
Else
Commend
Fi
Of course you can write to a line, use [] to indicate the boundary, or to indicate a branch. For example: if [ $ (ps -ef | grep -c "SSH" ) -gt 1 ];then echo "true" ; fi judging conditions in square brackets to denote demarcation, meaning to count the current process
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > Number of strings with ' ssh ', if this number is greater than 1, then output true, end.
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > also has a format with Elif, as follows:
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > If condition
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > then
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > commend /span>
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > elif condition1
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > then
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > &NBSP;COMMEND1 /span>
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > Else
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > Commend2
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > fi
< Span class= "pun" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > instance:
a=10b=20if [$a = = $b] #此处方括号和内部的内容之间必须有空格then echo "a equals B" elif [$a-gt $b] #这种写法也可以改写成test $a-gt $bthen echo "A greater than B" elif [$a-lt $b]then echo "A less than B" else echo "does not meet the conditions" fi# note, c=[$a-l T $b];echo $c can not get any output value, it is not like Python, true and false can not be printed out, it is really want to try to use [$a-lt $b] && echo hehe (| | ) also (get hehe on the front is ture) a=1b=2[$a-lt $b] && echo hehe1 #且, the first one is true, the second one still needs to be judged, so as to know whether the overall is true or false[$a-gt $b] || Echo Hehe2 #或, the first one is true, the second one does not have to be executed to get the overall return value is true
2 for Loop
basic format:for I in Item1 item2
Do
Commend
Done
It is still separated by semicolons when written in a line: For I in Item1 item2; Do commend; Done
Example:
For loop in 1 2 3 4 5do echo ' The value is: $loop ' done #结果是 #the value is:1#the value is:2 ... Until 5.
One thing to note, for I in "a string", the output of I will be: a string, here and Python is not the same, here will not take the letter one by one to disassemble the iteration.
3 While loop
Executes as long as the condition is met. The General format is:
While condition #condition not write, or write true, it can cause an infinite loop.
Do
Commend
done$
example; #得到的输出是1, 2,3,4,5,let is used to calculate multiple expressions, do not need to add variable indicator $, self-add operation: Letno++, self- subtraction: Letno--。 abbreviated form let no+=10,let no-=20, respectively, is equivalent to let no=no+10,let no=no-20.
Int=1while (($int <=5) #写成: [$int-le 5] line do echo $int let ' int++ ' done
4 until cycle
It has been circulating until there is a ture. Just the opposite of Wihile.
Untill condition
Do
Commend
Done
Example:
A=0until [! $a-lt] #除非a大于等于10否则一直运行do echo $a a= ' expr $a + 1 ' done
5 Multiple SELECT statements
is to select one of the multiple statements, note the quotation marks, and wrap.
Instance:
Anum=3case $aNum in #变量aNum可以是数字或者字符串abc等 1) echo ' You selected 1 ' ;; #俩分号 2) echo ' You have chosen 2 ' ;; 3) echo ' You have chosen 3 ' ;; 4) echo ' You have chosen 4 ' ;; *) echo ' You did not enter a number between 1 and 4 ' ;; Esac #esca是反过来的
6 jumping out of the loop
The break command jumps out of all loops.
Instance:
While:d o echo-n "Enter a number from 1 to 5:" read Anum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ; *) echo "The number you entered is not between 1 and 5!" Game Over " #* represents a break #直接跳出并且终止这个程序 other than 12345 ;; Esacdone
Continue jump out of this layer of cycle
Instance:
While:d o echo-n "Enter a number from 1 to 5:" read Anum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ; *) echo "The number you entered is not between 1 and 5!" Continue the number between #如果输入的值不是1-5, the Contiuune will be transferred directly to the "echo-n" input number between 1 and 5 " echo" Game Over "; ; Esacdone
Shell Programming flow control (conditional statements and loop statements)