bash while/until循环学习
while循环:条件满足,则循环;失败,则退出
如何退出?
必须有时刻,条件测试不成功
? :条件控制变量
while 条件测试:do
循环体
done
until循环;条件不满足,则循环;否则,退出
until 测试条件;do
循环体
done
bash编程之组合测试条件
逻辑与:多个条件同时满足
[ CONDITION1 ] && [ CONDITION2 ]
[ CONDITION1 -a CONDITION2 ]
[[ CONDITION1 && CONDITION2 ]]
注意:前两个使用单双中括号都可,但&&不允许用于单个中括号中,所有第三者只能用于双中括号中
逻辑或:多个条件满足一个
[ CONDITION1 ] || [ CONDITION2 ]
[ CONDITION1 -o CONDITION2 ]
[[ CONDITION1 || CONDITION2 ]]
注意 || 不允许出现在单中括号中
得摩根定律
!(条件1或者 条件2) = !条件1 并且!条件2
!(条件1且条件2)=!条件1 或者 !条件2
练习:
1.:通过键盘提示用户输入字符,将用户输入的小写字母转换为大写,转换一次之后,在此提醒,再输入再转换,直到输入quit退出;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash # read -p -t 5 "Enter a Word: " word while [[ "$word" != "quit" ]]; do echo $word | tr 'a-z' 'A-Z' read -p -t 5 "Enter a Word again: " word done |
2.写一个脚本,实现如下功能;
1、显示如下菜单:
CPU) show cpu info;
men) show memory info;
disk) show disk info;
quit) quit
Enter your option:
2、根据用户的选择输出相应信息,每次执行后,不退出,而由用户咋此指定新的选项
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
|
#! /bin/bash # cat <<EOF cpu) print cpu infomation men) print memory infomation disk) print disk infomation quit) Quit EOF read -p "Enter your option: " option option=` echo $option | tr 'a-z' 'A-Z' ` while [[ "$option" != "QUIT" ]]; do if [[ "$option" == "CPU" ]]; then cat /proc/cpuinfo elif [[ "$option" == "MEM" ]]; then free -m elif [[ "$option" == "DISK" ]]; then df -Th else echo "Wrong Option..." fi read -p "Enter your option: " option option=` echo $option | tr 'a-z' 'A-Z' ` done |
3.提示用户输入一个用户名,显示用户名UID和SHELl信息,否则,则显示无此用户,显示完成后,提示用户再次输入,如果quit则退出;
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
32
33
34
35
36
37
38
39
40
|
#! /bin/bash # cat <<EOF Username)Enter your Username.. quit)quit.. EOF read -p "Enter Your userName: " userName userName=` echo $userName | tr 'A-Z' 'a-z' ` while [[ "$userName" != "QUIT" ]]; do sysUser=` cat /etc/passwd | grep ^$userName | cut -d: -f1 | tr 'A-Z' 'a-z' ` if [[ "$userName" == "$sysUser" ]]; then echo "This $userName `cat /etc/passwd | grep -i ^$userName | cut -d: -f3,7`" else echo "No Such $userName.." fi read -p "Enter Your userName: " userName userName=` echo $userName | tr 'A-Z' 'a-z' ` done #! /bin/bash # read -t 2 -p "Enter a user name: " userName userName=` echo $userName | tr 'A-Z' 'a-z' ` UID=` grep "^$userName\>" /etc/passwd | cut -d: -f3` SH=` grep "^$userName\>" /etc/passwd | cut -d: -f7` while [[ "$userName" != "quit" ]]; do if [ -z "$userName" ]; then echo "Username null...." elif id $userName &> /dev/null ; then echo "$userName uid: $UID" echo "$userName Shell: $SH" else echo "No such user...." fi read -t 2 -p "Enter a user name again(quit to exit) " userName done |
4.求100以内所有正整数的和;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#! /bin/bash # declare -i sum =0 declare -i i=1 while [ $i - le 100 ]; do let sum +=$i let i++ done echo $ sum #! /bin/bash # declare -i sum =0 declare -i i=1 until [ $i -gt 100 ]; do let sum +=$i let i++ done echo $ sum |
5.求100以内所有偶数之和;
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
|
#! /bin/bash # declare -i evensum=0 declare -i i=1 while [ $i - le 100 ]; do if [ $[$i%2] - eq 0 ]; then let evensum+=$i fi let i++ done echo $evensum #! /bin/bash # declare -i sum =0 declare -i i=0 while [[ $i - le 100 ]]; do let sum +=$i let i+=2 done echo $ sum |
6.用until求100以内整数之和;
1
2
3
4
5
6
7
8
9
10
11
|
#! /bin/bash # declare -i sum =0 declare -i i=1 until [ $i -gt 100 ]; do let sum +=$i let i++ done echo $ sum |
7.提供一个用户名,判断用户是否登陆当前系统;
1.如果没有登陆,则停止5秒之后再次判断,直到用户登录系统,显示用户登录,而后退出
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#! /bin/bash # read -t 5 -p "Enter Your userName: " userName userName=` echo $userName | tr 'A-Z' 'a-z' ` who | grep "$userName" &> /dev/null retVal=$? while [ $retVal - ne 0 ]; do sleep 5 read -t 5 -p "Enter Your userName: " userName userName=` echo $userName | tr 'A-Z' 'a-z' ` done echo "Welcome $userName login System..." #! /bin/bash # read -t 5 -p "Enter Your userName: " userName while ! id $userName &> /dev/null ; do read -t 5 -p "Enter Your userName: " userName done who | grep "^$userName" &> /dev/null retVal=$? while [ $retVal - ne 0 ]; do sleep 5 who | grep "$userName" &> /dev/null retVal=$? done echo "Welcome $userName login System..." #! /bin/bash # read -t 5 -p "Enter Your userName: " userName while ! id $userName &> /dev/null ; do read -t 5 -p "Enter Your userName again: " userName done while ! who | grep "^$userName" &> /dev/null ; do sleep 5 done echo "Welcome $userName login System..." #! /bin/bash # read -t 5 -p "Enter Your userName: " userName until [ -n "$userName" ] && id $userName &> /dev/null ; do read -t 5 -p "Enter Your userName again: " userName done until who | grep "^$userName" &> /dev/null ; do sleep 5 done echo "Welcome $userName login System..." |
8.取出当前系统上,默认shell为bash的用户;
1
2
3
4
5
|
#! /bin/bash # while read line; do [[ ` echo $line | cut -d: -f7` == "/bin/bash" ]] && echo $line | cut -d: -f1 done < /etc/passwd |
9.显示其ID号为偶数的用户;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash # while read line; do userID=` echo $line | cut -d: -f3` if [ $[$userID%2] - eq 0 ]; then echo -n "$userID: " echo $line | cut -d: -f1 fi done < /etc/passwd |
10.显示/etc/rc.d/rc.sysinit文件中,其总字符个数大于30的行;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash # while read line; do charCounts=` echo $line | wc -c` if [ $charCounts -gt 30 ]; then echo -n "$charCounts: " echo $line fi done < /etc/rc .d /rc .sysinit |
11.显示用户其UID和GID均为偶数的用户;
1
2
3
4
5
6
7
8
9
10
|
#! /bin/bash # while read line; do userID=` echo $line | cut -d: -f3` groupID` echo $line | cut -d: -f4` if [ $[$userID%2] - eq 0 -a $[$groupID%2] - eq 0 ]; then echo -n "$userID,$groupID: " echo $line | cut -d: -f1 fi done < /etc/passwd |
12.显示/etc/rc.d/rc.sysinit文件中,其总字符个数大于30且以非#开头的行;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash # while read line; do charCounts=` echo $line | wc -c` if [ $charCounts -gt 30 ] && [[ "$line" =~ ^[^ #] ]]; then echo -n "$charCounts: " echo $line fi done < /etc/rc .d /rc .sysinit |
13.写一个脚本,完成如下任务;
1.提示用户输入一个磁盘设备文件路径不存在或不是一个块设备,则提示用户重新输入,知道输入正确为止,或者输入quit以9为退出码结束脚本
2.提示用户"下面的操作会清空磁盘的数据,并提问是否继续"
如果用户给出字符y或yes,则继续,否则,则提供以8为退出码结束脚本
3.将用户指定的磁盘上的分区清空,而后创建两个分区,大小分别为100M和512M
4.格式化这两个分区
5.将第一个分区挂载至/mnt/boot目录,第二个分区挂载至/mnt/sysroot目录
bash while/until循环学习的更多相关文章
- bash的for循环从命令读取值
bash的for循环可以很方便地从命令读取值,还可以指定分割值 下面的程序可以打印文件的内容,前面加上行号 #!/bin/bash # 打印每一行的内容,前面加行号 filename="/h ...
- Bash On Windows的学习
Bash On Windows的学习 Bash On Windows的卸载 删除软件和设置:在 cmd 运行lxrun /uninstall 删除所有文件:在cmd中运行lxrun /uninstal ...
- bash脚本编程---循环
bash为过程式编程语言 代码执行顺序: 1.顺序执行:逐条执行 2.选择执行:代码有一个分支,条件满足时才会执行 两个或以上的分支,只会执行其中一个满足条 ...
- Python for循环学习总结笔记
循环是任何语⾔的⼀个必备要素.同样地,for循环就是Python的⼀个重要组成部分.然而还有⼀些内容是初学者常常忽视的.下面是Python for循环学习总结笔记,一起来查漏补缺吧! ...
- Shell/Bash 变量/variable 循环/loop
如何在bash脚本里面进行循环 #!/bin/bash n=9999 for(( i =1; i<=100;i++)) do /root/testProgram $n sleep 5 n=$(( ...
- bash shell for循环1到100 .
前言 用bash shell写程序时,经常会用到for循环,特别是从1到100这种需求,这里记录几种shell中从1到100的循环方法 方法 类c语言 for ((i=1; i<=100; ...
- bash 编程中循环语句用法
1.if 是单分支语句,使用格式如下: if condition ; then statement ….. fi 2.if … else 是双分支语句,使用格式如下: if condition ; t ...
- Bash Shell 快捷键的学习使用
原文地址: http://dbanotes.net/tech-memo/shell_shortcut.html 这篇 Bash Shell Shortcuts 的快捷键总结的非常好.值得学习.下面内容 ...
- Bash编程(2) 循环与分支
Shell中有三种类型的循环:for, until, while,具有3种类型的条件语句:if, case, 条件操作符(&&, ||). 1. 结束码 命令的结束码可以在命令运行完后 ...
随机推荐
- 查找n个数字中的最大值
闲来无事,试试用arg_list查找n个数字中的最大者. 又因为本人喜欢模板, 所以就早早的写了以下代码, 没有经过严格测试. /*********************************** ...
- CMT learning
一个 GMT 命令由"gmt + 模块 + 选项 + 参数"构成,写成如下形式: gmt module -Axx+bxxxx -Bxx+axxxx • gmt 是 GMT 中&qu ...
- mysql,node.js
var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost', user: 'root', passwor ...
- Entity Framework Linq 动态组合where条件
public static class PredicateExtensions { public static Expression<Func<T, bool>> True&l ...
- 初探NIOS II之hello_world
平台背景: 操作系统:win7 64bit 开发板:DE2-115 Quartus ii:15.0及配套的NIOS ii开发平台 一.硬件系统的建立 1.在Quartus里新建工程,这是很基本的就不 ...
- 简单理解Struts2中拦截器与过滤器的区别及执行顺序
简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...
- 宁波uber优歩司机注册教程 UBER宁波司机注册指南!
自2012年Uber开始向全球进军以来,目前已进入全球56个国家和地区的市场,在全球超过270个城市提供服务, 而Uber公司的估值已高达412亿美元. [目前开通Uber优步叫车服务的中国城市] ...
- 深入理解JavaScript系列:各种上下文中的this
开头闲扯几句.上篇写对象原型的文章获得了1K多的阅读和几条评论,心里还是蛮欣喜的.那种写出来然后有人跟你讨论的感觉很不错. 公告里已经有写,自己开这个博客以及为什么要写文章的原因就是为了能把自己所思所 ...
- BZOJ 1858 线段树
标记会重叠需要判断. #include <bits/stdc++.h> using namespace std; inline int Max(int x,int y) {return x ...
- RabbitMQ - TcpConnection析构引发的一次handshake_timeout
使用RabbitMQ时,连接rabbit-server一直连接失败,代码没有任何错误提示.但是通过rabbitmqctl始终查询不到连接以及创建的queue等信息. 官方的文件demo里面也没有Tcp ...