关于循环的总结

/ Shell / 没有评论 / 761浏览

for

数字型

#!/bin/bash
for((i=1;i<=10;i++));
do 
echo $(expr $i \* 3 + 1);
done
#!/bin/bash
for i in $(seq 1 10)
do 
echo $(expr $i \* 3 + 1);
done
#!/bin/bash
for i in {1..10}
do
echo $(expr $i \* 3 + 1);
done
#!/bin/bash
for((integer = 1; integer <= 5; integer++))
do
    echo "$integer"
done
#!/bin/bash
sum=0
for(( i = 1; i <= 100; i = i + 2 ))
do
     let "sum += i"
done
echo "sum=$sum"

字符型

#!/bin/bash
for i in `ls`;
do 
echo $i is file name\! ;
done
#!/bin/bash
for i in $* ;
do
echo $i is input chart\! ;
done
#!/bin/bash
for i in f1 f2 f3 ;
do
echo $i is appoint ;
done
#!/bin/bash
list="rootfs usr data data2"
for i in $list;
do
echo $i is appoint ;
done

路径型

#!/bin/bash
for file in /proc/*;
do
echo $file is file path \! ;
done
#!/bin/bash
for file in $(ls *.sh)
do
echo $file is file path \! ;
done

awk

#!/bin/bash
awk 'BEGIN{for(i=1; i<=10; i++) print i}'

while

单行

 while : ;do  netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}'  ; sleep 1; done; 
#!/bin/bash
echo "Please input the num "
read num
factorial=1
while [ "$num" -gt 0 ]
do
    let "factorial= factorial*num"
    let "num--"
done
echo "The factorial is $factorial"

#!/bin/bash
for (( i = 1; i <=9; i++ ))
do
    
    for (( j=1; j <= i; j++ ))
    do
        let "temp = i * j"     
        echo -n "$i*$j=$temp  "
     done 
     
     echo ""   #output newline
done