shell 学习之while语句

简介:

shell中4大循环语句:

for、while、until、select

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

一、while语法结构

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

二、常见用法

1、无限循环

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


while (());do
        command
        ….

done
示例:计算1到10的和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/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

 

结果如下:

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

2、while 读取文件

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

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/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"

 

执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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、通过管道传递给{}(同样适用于其他语句)

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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
}

 

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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,不能对非数字字符进行判断。。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/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

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
[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



本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1211927,如需转载请自行联系原作者

相关文章
|
8天前
|
存储 运维 Shell
shell中for while until 三种循环的用法
shell编程中,有几种常见的循环结构,包括for循环、while循环和until循环,总的来说,循环shell编程中扮演着至关重要的角色,它们使得自动化任务变得更加容易,提高了效率,并且可以处理各种各样的编程需求。
shell中for while until 三种循环的用法
|
3月前
|
Shell 开发工具
学习简单的shell script
【1月更文挑战第3天】学习简单的shell script。
47 3
|
4月前
|
Unix Shell Linux
shell语言的学习
shell语言的学习
58 3
|
3月前
|
Java 大数据 Shell
Shell基础学习---2、运算符、条件判断、流程控制(第一天学习)
大数据开发学习 Shell基础学习---2、运算符、条件判断、流程控制
37 1
|
6月前
|
Shell
shell里的while循环详解
shell里的while循环详解
84 0
|
3月前
|
监控 Linux Shell
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
在线上排查问题时,查询日志、查看系统配置和分析操作系统信息是至关重要的。这些操作可以帮助我们深入了解软件和服务的兼容性,并解决潜在的问题。在本次学习中,我们将介绍并深入学习一些我在处理类似问题时常用的指令。通过掌握这些指令,你将能够更加高效地定位和解决线上问题,提高系统的稳定性和性能。让我们一同进入这个学习过程吧!
43 0
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
|
6月前
|
Shell Linux
shell脚本学习-进阶
shell脚本学习-进阶
31 0
|
2月前
|
监控 Shell Linux
Linux如何系统的学习shell方法
Linux如何系统的学习shell方法
28 0
|
3月前
|
关系型数据库 Linux Shell
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(文件处理指令-上)
在当今的数字化时代,Linux已成为服务器、云计算、物联网等众多领域的核心操作系统。对于技术从业者、开发者以及系统管理员来说,掌握Linux指令不仅是一项基本技能,更是打开专业领域大门的关键。
49 3
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(文件处理指令-上)
|
3月前
|
Shell Linux Perl
Shell基础学习---3、Read读取控制台输入、函数、综合应用案例:归档文件、正则表达式入门(第二天学习)
Shell基础学习---3、Read读取控制台输入、函数、综合应用案例:归档文件、正则表达式入门
54 1