Conditional statements
1#!/bin/Bash2 3Read-p"input your name:"name4 #第一种判断5 if["$name"=="Mike" ];then #注意这里用 [...] When comparing two strings, the variable $name quoted, and there are spaces on both sides of "= ="; "[" followed by a space; "]" preceded by a space 6 Echo "eq"7 Else8 Echo "Not eq"9 fiTen A#The second kind of judgment - ifTest$name="Mike";Then #注意这里用 Test compares two strings, the variable $name is not quoted , and "=" has spaces on both sides - Echo "eq" the Else - Echo "Not eq" - fi
The first judgment and the second is equivalent.
Case statement
1#!/bin/Bash2 3Read-p"input your name:"name4 5 Case$nameinch 6 "Mike" )7 Echo "You are Mike"8 ;;9 "Tom" )Ten Echo "You are Tom" One ;; A*) #相当于通配符, 0~ Infinite number of arbitrary characters - Echo "anything" - ;; the Esac
Cycle
1 while[Condition] #表示condition成立时, execute2 Do3 #程序段落4 Done5 6 7 until[condition] #表示condition不成立时, execution, or, if established, exit the loop8 Do9 #程序段落Ten Done One A - forVarinchcon1 con2 con3 ... # first cycle, $var content is con1; second time is con2 .... - Do the #程序段落 - Done - - + for((i=1; i< -; i=i+1)) - Do +s=$ (($s +Si)) A Done
Shell script-Conditional statement, loop statement