Shell: Shell Process Control details, shell Process Control details
Shell: Shell Process Control details if else
If statement
Syntax:
if conditionthen command ...fi
Eg:
#!/bin/basha=100b=100if [ $a == $b ]then echo "$a == $b"fi
Output:
100 == 100
You can also write a line in the terminal:
[zhang@localhost ~]$ a=100[zhang@localhost ~]$ b=100[zhang@localhost ~]$ if [ $a == $b ]; then echo "$a == $b"; fi100 == 100
If else statement
Syntax:
if conditionthen command ...else command ...fi
Eg:
Enter
[zhang@localhost ~]$ a=100[zhang@localhost ~]$ b=200[zhang@localhost ~]$ if [ $a == $b ]> then> echo "$a == $b"> else> echo "$a != $b"> fi100 != 200
If elif else statement
Syntax:
if condition1then command1 ...elif comdition2then command2else commandfi
Eg:
#!/bin/basha=100b=200c=150if [ $[a + c] > $b ]then echo "$a + $c > $b return true"elif [ $[a + c] < $b ]then echo "$a + $c < $b return true"else echo "$a + $c = $b return true"fi
Output result:
100 + 150 > 200 return true
For Loop
For Syntax:
for var in item1 item2 ... itemndo command1 command2 ... commandndone
Write a line:
for var in item1 item2 ... itemn; do command1;command2 ... done;
Eg:
[zhang@localhost ~]$ for num in 1 2 4 8 16> do> echo $num> done124816
One row:
[zhang@localhost ~]$ for num in 1 2 3 4 5; do echo $num; done12345
While statement
While Syntax:
while conditiondo commanddone
Eg:
#!/bin/bashnum=1while(($num<=5))do echo $num let "num++"done
Output result:
12345
Infinite Loop
while :do commanddone
Or
while truedo commanddone
Or
for (( ; ; ))
Until Loop
Until Syntax:
until condiiondo commanddone
Generally, while is used as an alternative.
Case
Case Syntax:
case value invalue1) command1 command2 ... commandn ;;value2) command1 command2 ... commandn ;;value3) command1 command2 ... commandn ;;*) command1 command2 ... commandn ;;esac
;: All commands are executed. It is equivalent to break.
*: It is equivalent to default in java.
Eg:
#!/bin/bashvalue=2case $value in1) echo "$value equal 1" ;;2) echo "$value equal 2" ;;3) echo "$value equal 3" ;;*) echo "$value not find" ;;esac
Output result:
2 equal 2
Skip Loop
Break command
#!/bin/bashfor i in 1 2 3 4 5do if [ $[i % 2] == 0 ] then echo "$i" break fidone
Output: 2
Continue command
#!/bin/bashfor i in 1 2 3 4 5do if [ $[i % 2] == 0 ] then echo "$i" continue fidone
Output:
24
Esac command
Used Together with case as the end symbol of case.