shell中4大循環語句:shell
for、while、until、select編程
這裏咱們來學習下while的基本用法bash
1、while語法結構ide
while argument;do
statement
.......
done模塊化
2、常見用法oop
一、無限循環學習
while中無限循環使用 ((1)) 或者[1]
spa
while (());do
command
….ip
done
示例:計算1到10的和rem
#!/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
二、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.
三、經過管道傳遞給{}(一樣適用於其餘語句)
經過管道把命令組丟給{}
上面例子一樣能夠寫成這樣
[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.
3、示例對用戶輸入的數字進行判斷,若是大於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是必不可少的一部分。。。。