Shell scripts of the flow control (if, for, while)

if the judge

If statement in three formats:

(1)if

(2)if else

(3)if elif else

Syntax is as follows:

#if syntax 
if condition 
then 
    commands 1 ... 
    command ... 2       
Fi 
#if syntax for the else 
if condition 
then 
    commands 1 ... 
    command ... 2       
the else 
    command 1 ... 
    command 2 ...       
Fi 
#if syntax elif else 
if condition 1 
the then 
    command 1 ... 
    command 2 ...   
     elif condition 2 
     the then 
     command 1 ... 
     command ... 2   
the else 
    command 1 ... 
    command 2 ...       
Fi

Binary operators

Integer comparison:

It is equal to: -eq 
not equal: -ne 
greater than: -gt 
than or equal to: -ge 
less than: -lt 
less: -le

  

String comparison:

Equal: = 
not equal:! =

  

for loop

The for loop syntax:

for the variable a in the variable Variable 1 Variable 2 .... n- 
do 
    command ..... 1 
    Command 2 .... 
DONE

  

Digital cycle:

#格式1:
for ((i=1;i<=10;i++));
do
    echo ${i};
done
#格式2:
for i in $(seq 1 10)  
do   
echo ${i};
done 
#格式3:
for i in {1..10}  
do  
echo ${i};
done  

  

String loop:

list="a b c d"  
for i in ${list};  
do  
echo ${i};
done

  

 while loop

while loop syntax:

while conditions 
do 
    execute commands (if the command is executed condition is satisfied, not satisfied with the ending) 
DONE

  

chestnut:

(1) is calculated from the accumulated 1 to 100 and

! # / bin / bash
 SUM = 0 
I = . 1 
the while [I -le $ 100 ];
 do 
   the let SUM = SUM + I #let command bash for calculating tools do not need to calculate with variable $ 
   I = ` expr $ + I . 1 `#expr multifunctional counter to note that with the $ symbol and note the space 

DONE 
echo $ SUM
View Code

(2) while infinite loop

! # / bin / bash
 the while : # here: colon represents the infinite loop means
 do 
    command 
done
View Code

until loop

Exit the loop condition is false when, and for, while the opposite, for, while a condition is true cycle continues.

until loop syntax:

# condition until the condition is false to perform the following cycle 
do 
    command 
done

chestnut:

(1) is calculated from the accumulated 1 to 100 and

#!/bin/bash
sum=0
i=0
until [ $i -gt 100 ]
do
   let sum=sum+i
   i=`expr $i + 1`
done
echo $sum
View Code

break: out of the loop

break commonly used in the loop, the loop out of the entire, end all direct cycle. And use the same java

continue: the end of this cycle

continue commonly used in the loop, the end of this cycle, the next cycle. And use the same java

exit: exit the entire script

direct exit to exit the entire script ends, and similar usage of java

Guess you like

Origin www.cnblogs.com/GuixinChan/p/11436853.html
Recommended