shell中4大循环语句:

for、while、until、select

这里我们来学习下while的基本用法

一、while语法结构

while argument;do
    statement
    .......
done

二、常见用法

1、无限循环

while中无限循环使用 ((1)) 或者[1]


while (());do
        command
        ….

done
示例:计算1到10的和

#!/bin/bash
#Version:0.1
#Author:lovelace
#calculaion and from 1 to 10
#difine two variable
declare -i i=1
declare -i sum=0
#use while to loop
while ((i<=10));do
let sum+=i
let ++i
done
#print the result
echo $sum

 

结果如下:

[root@lovelace while]# ./while1.sh
55

2、while 读取文件

经典的用法是搭配转向输入,读取文件的内容
打印出使用bash的用户名和bash

#!/bin/bash
#Vsersion:0,1
#Author:lovelace
#Pragram:This pragram is show user who use bash
#use while read files
while read line;do
#filter out the user who use bash
Bashuser=`echo $line | awk -F: '{print $1,$NF}' | grep 'bash' | awk '{print $1}'`
#jugement Bashuser is null or not and print the user who use bash shell
if [ ! -z $Bashuser ];then
echo "$Bashuser use bash shell."
fi
done < "/etc/passwd"

 

执行结果

[root@lovelace while]# ./readpasswd.sh
root use bash shell.
nick use bash shell.
kale use bash shell.
user2 use bash shell.
user3 use bash shell.
user4 use bash shell.
user5 use bash shell.
user6 use bash shell.
user7 use bash shell.
user8 use bash shell.
user9 use bash shell.
user10 use bash shell.
mark use bash shell.
lovelace use bash shell.
lovetest use bash shell.

3、通过管道传递给{}(同样适用于其他语句)

通过管道把命令组丢给{}

上面例子同样可以写成这样

[root@lovelace while]# cat catwhile.sh
#!/bin/bash
#Version:0.1
#Author:lovelace
#Pragram:This pragram is show user who use bash
#use pipe transparent data to {}
cat /etc/passwd | {
while read line;do
#use if statement jugement bash shell user  and print it
if [ "`echo $line | awk -F: '{print $NF}'`" == "/bin/bash" ];then
Bashuser=`echo $line |     awk -F: '{print $1}'`
echo "$Bashuser use bash shell."
fi
done
}

 

执行结果:

[root@lovelace while]# ./catwhile.sh
root use bash shell.
nick use bash shell.
kale use bash shell.
user2 use bash shell.
user3 use bash shell.
user4 use bash shell.
user5 use bash shell.
user6 use bash shell.
user7 use bash shell.
user8 use bash shell.
user9 use bash shell.
user10 use bash shell.
mark use bash shell.
lovelace use bash shell.
lovetest use bash shell.

三、示例对用户输入的数字进行判断,如果大于2,则打印出改数字的次方,如果小于2,则一直进行循环)Note:该脚本有bug,不能对非数字字符进行判断。。。。

#!/bin/bash
#Pragram:This pragram is check the number you input
#Set the variable is not defined are not allowed to shopt -s -o nounset
#usedifine variale
declare -i num
declare -i i
declare -i x
while [[ $num -lt 2 ]]
do
read -p "please input a number greater than 2:" num
done
i=2
echo -n $num '='
while ((num>=i))
do
x=0
tmp=num%i
while [[ $tmp -eq 0 ]]
do
((num/=i))
((x++))
tmp=num%i
done
if [[ $x -gt 0 ]];then
echo -n $i
[ $x -gt 1 ] && echo -n '^'$x
[ $num -gt 1 ] && echo -n ' * '
fi
((i>=3?i+=2:i++))
done
echo

执行结果:

[root@lovelace while]# ./prem.sh
please input a number big 2:3
3 =3
[root@lovelace while]# ./prem.sh
please input a number big 2:-23
please input a number big 2:nick
./prem.sh: line 11: nick: unbound variable
[root@lovelace while]# ./prem.sh
please input a number big 2:4
4 =2^2
[root@lovelace while]# ./prem.sh
please input a number big 2:9
9 =3^2

 

总结:while作为shell编程中的循环控制语句的其中之一,有着非常强悍的功能,这里仅仅是列出了其中while基本的一些功能,我们的最终目标是把shell脚本模块化,而想要达到这一步,学习while是必不可少的一部分。。。。j_0015.gifj_0022.gif