shellフロー制御文


linux shellには、条件文(if)、ループ文(for,while,until)、選択文(case/select)など、独自のプロセス制御文があります.次に、例を挙げて、各文の使い方を紹介します.
1:shellで$()は``と等価です.中間に含まれるコマンド文を実行し、実行結果を返します.2:効率的に言えばlet==(()>expr>bcである.letと()実行は組み込みコマンドであり,同じアルゴリズムを用いる.3:letとexprの演算は整数演算であり、浮動小数点予算は含まれません.4:exprとbcは外部プログラムであり、exprの体積はbcの1/3にほぼ等しく、一度にメモリをロードするのにかかる時間は異なります.5:演算能力から言えば、bcは1位です. 
shellのサイクルは主に3種類あり,for,while,untilshellの分岐判断は主に2種類あり,if,case
 
linux shell「()」二重括弧演算子使用
shellプログラミング制御構造:expr、let、for、while、until、shift、if、case、break、continue、関数、select
####ループ制御文、break continue exit returnbreakコマンドは現在のループ内breakの下の文を実行せずに現在のループから終了する.continueコマンドは、プログラムがこのループ内で次の文を無視し、ループヘッダからbreakを実行してループを終了し、ループを終了continueを終了します.ループ内でcontinueの下のコードを実行しないで、次のループexitに入ってスクリプトを終了します.exit 0 returnが関数の中でデータを返したり、呼び出し関数に返したりするスクリプトのような整数をシステムに持っています.
 
if条件
#if      Shell  ,           0,   then     。

if command 

then

command

command

fi

# test       expression   ,    ,   then     。

if test expression

then

command

fi

#      test expression  

if [ string/numeric expression ]

then

command

fi

#                      ,               。

if [[ string expression ]] #         ,        

then

command

fi



if (( numeric expression )) #let   

then

command

fi

 
forサイクルhttp://blog.csdn.net/qiudakun/article/details/7063559
すべてのforをスクリプトに書き込む
for i in f1 f2 f3
for i in {1..5}
for i in $a
for i in $(ls $a)
for i in $(seq 100)
for i in `ls *.sh`
for i in `seq 100`
for ((i=0;i<5;i++))
for i in ${arr[@]}
for i in $*
for f in /proc/sys/net/ipv4/conf/*/arp_accept

, 。 。 , [root@
250-shiyan sh]# cat > for for f in 1 2 3 4 5 do echo $f done [root@250-shiyan sh]# bash for 1 2 3 4 5 , [root@250-shiyan sh]# cat > for for i in {1..5} do echo $i done [root@250-shiyan sh]# bash for 1 2 3 4 5 ### , [root@250-shiyan sh]# cat > for IFS=':' a="root:x:0:0:root:/root:/bin/bash" for f in $a do echo $f done [root@250-shiyan sh]# bash for root x 0 0 root /root /bin/bash ### , [root@250-shiyan sh]# cat > for a="/root/sh" for f in $(ls $a) do echo "file-i:$f" done [root@250-shiyan sh]# bash for file-i:aa file-i:ab file-i:ac file-i:awk file-i:ccc file-i:cfont file-i:check-root.sh
[root@250-shiyan sh]# cat > for

for f in `ls *.sh`

do

name=`echo "$f"|awk -F. '{print $1}'`

echo $name

done

[root@250-shiyan sh]# bash for

check-root

eth

for

ser
###    (ls                )
[root@250-shiyan sh]# cat > for
for f in `find . -type f -name "*.sh"`
do
name=`echo "$f"|awk -F/ '{print $2}'`
echo $name
done
[root@250-shiyan sh]# bash for
eth.sh
ser.sh
check-root.sh
for.sh
[root@250-shiyan sh]# cat > for
for f in `seq 100`   for f in $(seq 100)
do
if((f%4==0))
then
echo $f
continue
fi
done
[root@250-shiyan sh]# bash for
4
8
12
16
20
24
 
   
###     ,for((  ;  ;    )),c   for    

[root@250-shiyan sh]# cat > for

for((i=0;i<=5;i++))

do

echo $(expr $i \* 3)

done

[root@250-shiyan sh]# bash for

0

3

6

9

12

15
[root@250-shiyan sh]# cat > for
for((i=1;i<100;i++))
do
if((i%3==0))
then
echo $i
continue
fi
done
[root@250-shiyan sh]# bash for
3
6
9
12
15
18
[root@250-shiyan sh]# cat > for
arr=("a" "b" "c")
for f in ${arr[@]}
do
echo $f
done
[root@250-shiyan sh]# bash for
a
b
c
[root@250-shiyan sh]# cat > for
for f in $*
do
echo $f
done
[root@250-shiyan sh]# chmod u+x for
[root@250-shiyan sh]# ./for a b c d e f
a
b
c
d
e
f
[root@250-shiyan sh]# cat > for
for f in /proc/sys/net/ipv4/conf/*/arp_accept
do
echo $f
done
[root@250-shiyan sh]#
[root@250-shiyan sh]# bash for
/proc/sys/net/ipv4/conf/all/arp_accept
/proc/sys/net/ipv4/conf/default/arp_accept
/proc/sys/net/ipv4/conf/eth0/arp_accept
/proc/sys/net/ipv4/conf/lo/arp_accept
###      ,   。    echo

[root@250-shiyan sh]# cat > until

f=5

until [ $f -lt 0 ]

do

echo $f

((f--))

done

[root@250-shiyan sh]# bash until

5

4

3

2

1

0
[root@84-monitor test]# bash loop
pass 1 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 2 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 3 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 4 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 5 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

[root@84-monitor test]# cat loop
outer=1
for a in 1 2 3 4 5
do
echo "pass $outer in outer_loop"
echo "-------------------------"
inner=1
for b in 1 2 3 4 5
do
echo "pass $inner in inner_loop"
let "inner+=1"
done
let "outer+=1"
echo
done
[root@84-monitor test]#

 
####     ,           

[root@250-shiyan frag]# cat while1.sh

while [ 1 -gt 0 ]

do

sleep 3

echo used

echo "`free |awk '/Mem/{print $3}'`"

done

[root@250-shiyan frag]# bash while1.sh

used

328200

used

328060

used

328060

####      ,           

while :

do

sleep 2

echo "`df -h`"

done  



####                  ,    

while read line

 do

 echo $line

 done 

< /etc/passwd



####             

min=1

max=100

while [ $min -le $max ]

do

  echo $min

  min=`expr $min + 5`

done



####     ,     ,       C   ,    :i=$(($i+1))

i=1

while(($i<100))

do

 if(($i%4==0))

 then

   echo $i

 fi

 i=$(($i+1))

done


####      while  

                       。



####       while  

              ,                   ,          ,            。

[root@250-shiyan frag]# cat aa.sh

#             while     1~10   

#!/bin/sh

echo "Please input the num (1~~10): "

read num

while [[ $num != 4 ]]

do

if [ $num -lt 4 ]

then

 echo "Too small ,Try again.."

 read num

 elif [ $num -gt 4 ]

 then

 echo "Too big ,Try again.. "

 read num

 else

 exit 0

 fi

 done

 echo "Yes ,you are right !!"

[root@250-shiyan frag]# bash aa.sh

Please input the num (1~~10):

3

Too small ,Try again..

4

Yes ,you are right !!



####     while  

              

[root@250-shiyan frag]# cat a1.sh

#!/bin/sh

echo "Please input the num:"

read num

sum=0

i=1

signal=0

while [[ $signal != 1 ]]

do

if [ $i -eq $num ]

 then

 let "signal=1"

 let "sum+=i"

 echo "1+2、、、+$num=$sum"

 else

  let "sum=sum+i"

  let "i++"

fi

done

[root@250-shiyan frag]# bash a1.sh

Please input the num:

1

1+2、、、+1=1

[root@250-shiyan frag]# bash a1.sh

Please input the num:

3

1+2、、、+3=6



####      while  

[root@250-shiyan frag]# cat aa.sh

#!/bin/sh

echo "Please input arguements is $# "

echo "What you input : "

while [[ $* != "" ]]

do

echo $1

shift

done

[root@250-shiyan frag]# ./aa.sh 34 535 232

Please input arguements is 3

What you input :

34

535

232
 
  

 

case  
[root@localhost script]# cat >yesno
#!/bin/bash
echo "enter [y/n]:"
read a
case $a in
  y|Y|yes|YES)
  echo "you enter $a"
  ;;
  n|N|no|NO)
  echo "you enter $a"
  ;;
  *)
  echo "error"
  ;;
esac

, 。case 。 ,case , 。 C ,case default , shell , *, 。
case 。


select bash , : (
1)、 1,2,3,4 ( echo , ) (2)、 read ( read , ) (3)、 ( , , ) select ,break , select , , select , , select break, , while , 。select , case 。 [root@250-shiyan sh]# cat sel1 #!/bin/bash while echo "display current systeminfo" do select vi in "ifconfig" "date" "uptime" "quit" do case $vi in      case "ifconfig") ifconfig;; "date") date;; "uptime") uptime;; "quit") exit 0;; *) continue;; esac break done done

 
パフォーマンス比較awkのプロセス制御文とshellのプロセス制御文
[chengmo@localhost nginx]# time (awk 'BEGIN{ total=0;for(i=0;i<=10000;i++){total+=i;}print total;}')

50005000



real    0m0.003s

user    0m0.003s

sys     0m0.000s

[chengmo@localhost nginx]# time(total=0;for i in $(seq 10000);do total=$(($total+i));done;echo $total;)

50005000



real    0m0.141s

user    0m0.125s

sys     0m0.008s



      ,    awk      shell 50 !