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: " wordwhile [[ "$word" != "quit" ]]; do echo $word | tr 'a-z' 'A-Z' read -p -t 5 "Enter a Word again: " worddone |
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) QuitEOFread -p "Enter your option: " optionoption=`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..EOFread -p "Enter Your userName: " userNameuserName=`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: " userNameuserName=`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) " userNamedone |
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=0declare -i i=1while [ $i -le 100 ];do let sum+=$i let i++doneecho $sum#! /bin/bash#declare -i sum=0declare -i i=1until [ $i -gt 100 ]; do let sum+=$i let i++doneecho $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=0declare -i i=1while [ $i -le 100 ]; do if [ $[$i%2] -eq 0 ]; then let evensum+=$i fi let i++doneecho $evensum#! /bin/bash#declare -i sum=0declare -i i=0while [[ $i -le 100 ]]; do let sum+=$i let i+=2doneecho $sum |
6.用until求100以内整数之和;
|
1
2
3
4
5
6
7
8
9
10
11
|
#! /bin/bash#declare -i sum=0declare -i i=1until [ $i -gt 100 ]; do let sum+=$i let i++doneecho $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: " userNameuntil [ -n "$userName" ] && id $userName &> /dev/null ; do read -t 5 -p "Enter Your userName again: " userNamedoneuntil who | grep "^$userName" &> /dev/null; do sleep 5doneecho "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: -f1done < /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 fidone < /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 fidone < /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 fidone < /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 fidone < /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. 结束码 命令的结束码可以在命令运行完后 ...
随机推荐
- Informatica相同环境与不同环境的导入导出( Repository Name,Integration Service Name,Folder Name是否相同):
Informatica相同环境与不同环境的导入导出( Repository Name,Integration Service Name,Folder Name是否相同): 1.repository N ...
- 火狐和IE浏览器的兼容问题汇总
1.window.event code=(navigator.appName="Netscape")?event.which:event.keycode; 2.event.x mx ...
- web前端基础篇⑨
1.web端.app端 目前实现响应式布局,主要用以下两种方式.CSS原生代码响应式布局 @media screen.bootstrap移动设备优先.自带框架. 兼容性 用原生代码的话 根据不同的屏幕 ...
- 访问本地Access 数据出错
访问本地的access数据库时,出现了OleDbException 10Aug2015 晚: 好像找到了问题所在, 虽然不知道背后深层次的原因 改用了C#, 然后command 语句里也update了 ...
- css cursor 的可选值(鼠标的各种样式)
crosshair; 十字准心 The cursor render as a crosshair游标表现为十字准线 cursor: pointer; cursor: hand;写两个是为了照顾IE5, ...
- Apple Pay的快速实现
一.在Apple开发者中心配置 AppleID 和 Merchant IDs 二.配置好证书后在Xcode中开启Apple Pay 三.代码实现 3.1 判断是否支持Apple Pay,如果支持又将支 ...
- LeetCode Reorder List
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution ...
- sql语句的匹配
like 的通配符有两种 %(百分号):代表零个.一个或者多个字符. _(下划线):代表一个数字或者字符. 1. name以"李"开头 where name like '李%' 2 ...
- RCurl网络数据抓取
观察基础信息(服务器信息和提交给服务器的信息) d=debugGatherer()xpath="http://123.sogou.com/"url=getURL(xpath,deb ...
- xml配置文件的读写
using System.Xml; //----------------------------------------------读出XML文件中的节点值 XmlDocument xmlDoc = ...