1.for循环
1.1 列表for循环
格式:
for variable in {list}
do
command
command
...
done
例1:
#!/bin/bash
for variable in 1 2 3 4 5
do
echo "Hello,welcome $variable times "
done
例2:
#!/bin/bash
for variable in {1..5}
do
echo "Hello,welcome $variable times "
done
例3:
#/bin/bash
sum=0
for i in {1..100..2} #step=2
do
let "sum+=i"
done
echo "sum=$sum"
例4:
#!/bin/bash
sum=0
for i in $( seq 1 2 100 ) #按2递增来计算1~100
do
let "sum+=i"
done
echo "sum=$sum"
例5:
#!/bin/bash
for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do
echo "$day"
done
例6:
#!/bin/bash
#通过命令ls显示当前目录下的所有文件,然后通过不断地循环赋值给file,将其对应的文件名显示给用户。
for file in $( ls )
do
echo "file: $i"
done
例7:
#!/bin/bash
#同样可以通过通配符(*)产生文件名扩展,其中通配符可以匹配当前目录下的所有文件
for file in $( * )
do
echo "file: $i"
done
例8:
#!bin/bash
#通过命令行传递脚本中foru循环列表参数
echo "number of arguments is $#"
echo "What you input is:"
for argument in "$*"
do
echo "$argument"
done
1.2 不带列表for循环
格式:
for variable
do
command
command
...
done
其中do和done之间的命令称为循环体,shell会自动将命令行键入的所有参数依次组织起成列表,每次将一个命令行键入的参数显示给用户,直至所有的命令行参数都显示给现实给用户。这种结构的for循环和下面带列表的for循环的结构功能完全一致:
for variable in "$@"
do
command
command
...
done
例:
#!/bin/bash
echo "number of arguments is $#"
echo "What you input is: "
for argument
do
echo "$argument"
done
1.3 类C风格的for循环
格式:
for(( expr1; expr2; expr3 ))
do
command
command
...
done
例1:
#!/bin/bash
for(( integer = 1; integer <= 5; integer++ ))
do
echo "$integer"
done
例2:
#!/bin/bash
for(( ; ;))
do
echo "hello world"
done
例3:
#!/bin/bash
sum=0
for(( i = 1; i <= 100; i = i+2 ))
do
let "sum+= i"
done
echo "sum=$sum"
例4:
#!/bin/bash
LIMIT=5
for(( a=1,b=5; a <+ LIMIT; a++,b--))
do
let "temp=a-b"
echo "$a-$b=$temp"
done
2. while循环
格式:
while expression
do
command
command
...
done
2.1 计数器控制的while循环
例1:
#!/bin/bash
int = 1
while(( "$int" <= 5 ))
do
echo "$int"
let "int++"
done
例2:
#!/bin/bash
sum=0
i=1
while(( i <= 100 ))
do
let "sum+=i"
let "i += 2"
done
echo "sum=$sum"
2.2 结束标记控制的while循环
例1:
#!/bin/bash
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 high.Try again"
read num
else
exit 0
fi
done
echo "Congratulation, you are right"
例2:
#!/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"
2.3 命令行控制的while循环
形式:
while [[ "$*" != "" ]]
do
echo "$1"
shift
done
其中循环条件可以改写成
while [[ "$#" -ne 0 ]]
例:
#!/bin/bash
echo "number of arguments is $#"
echo "What you input is: "
while [[ "$*" != "" ]]
do
echo "$1"
shift
done
3. until循环
直到until后expression为真,结束循环
和while循环类似,当首次测试expression的推出状态为0,将一次也不执行循环体。
格式:
until expression
do
command
command
...
done
例:
#!/bin/bash
i=0
until [[ "$i" -gt 5 ]]
do
let "square=i*i"
echo "$i * $i = $square"
let "i++"
done
4. 嵌套循环
一个循环体内又包含另一个完整的循环结构。
例1:
#!/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 ""
done
例2:
#!/bin/bash
for(( i=1;i<=8;i++ ))
do
for(( j=1;j<=8;j++ ))
do
#下面两行用于不重叠显示黑白格
total=$(( $i+$j ))
tmp=$(( $total % 2 ))
if [ $tmp -eq 0 ]
then
echo -e -n "\033[47m "
else
echo -e -n "\033[40m"
fi
done
echo "" #换行
done
例3:
#!/bin/bash
i=0
while(( "$i" <=9 ))
do
j=1
while (( "$j" <= "$i" ))
do
echo -n "*"
let "j++"
done
let "i++"
echo ""
done
5. 循环控制符
5.1 break
例:
#!/bin/bash
sum=0
for(( i=1;i<=100;i++ ))
do
let "sum+=i"
if [ "$sum" -gt 1000 ]
then
echo "1+2+...+$i=$sum"
break
fi
done
5.2 continue
例:
#!/bin/bash
m=1
for(( i=1;i<100;i++ ))
do
let "temp1=i%7"
if [ "$temp1" -ne 0 ]
then
continue
fi
echo -n "$i"
let "temp2=m%7"
if [ "$temp2" -eq 0 ]
then
echo ""
fi
let "m++"
done
5.3 select
select 是bash的扩展结构,但其交互性要比case好得多。使用select可以提供选项供用户选择。
格式:
select variable in {list}
do
command
command
...
break
done
例1:
#!/bin/bash
echo "What is your favorite color? "
select color in "red" "blue" "green" "white" "black"
do
break;
done
echo
"you have selected $color"
select还有一种格式,不带参数列表,该结构通过命令行来传递参数列表,由用户自己设定参数列表。
例2:
#!/bin/bash
echo "What is your favorite color? "
select color
do
break
done
echo "you hava selected $color "