shell 中的for循环while循环和case语句

循环语句
1)for 循环


第一种语法格式:
for((初始化变量值;结束循环条件;循环控制语句))
do
循环体
done
eg.
#!/bin/sh
sum=0
for ((i=0;i<10;i++)) 
do
echo $i
sum=$[ $sum + i ]
done
echo $sum




第二种语法格式:
for 变量 in 值1 值2 ...值N
do 
循环体
done


eg.
#!/bin/sh
for MONTH in Jan Feb Mar Apr May Jun July Aug Sep Oct Nov Dec 
do
echo $MONTH
done


#!/bin/sh
for file in `/bin/ls ~`; 
do  
     echo $file; 
done


2)while 循环
第一种语法格式:
while [ condition  #循环条件 ]
do
#statements 
#【循环体】
#【循环控制】
done


eg.
#!/bin/sh
i=1
while [ $i -le 10 ]
do
sum=$((sum+i))
i=$[ i + 1 ]
done
echo $sum 




第二种语法格式:
while  read -r line
do 
#【循环体】
done


eg.
#!/bin/sh
#Read /ect/sysconfig/network-scripts/ifcfg-eh0 and print out
FILE=/ect/sysconfig/network-scripts/ifcfg-eh0
while read -r line
do 
echo $line
done < $FILE


3)case  类似于java中的 swich case 
第一种语法格式:
#!/bin/sh
echo "input from: one two three"
read input
case $input in
one) echo "your input is one"
;;
two) echo "your input is two"
;;
three) echo "your input is three"
;;
*) echo your input is $input
esac

第二种语法格式:
#!/bin/sh
echo "input from :one two three ....."
read input
case $input in
one | two) echo "your input is one or two"
;;
three | four) echo "your input is three or four "
;;
five) echo "your input is five"
;;
*) echo your input is $input
esac

你可能感兴趣的:(shell 中的for循环while循环和case语句)