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. 结束码 命令的结束码可以在命令运行完后 ...
随机推荐
- 搭建Android开发环境。
1. 从 http://developer.android.com/intl/zh-cn/sdk/index.html 下载ADK 2. 点击SDK.Manager.exe, 遇到闪退的问题,一开始还 ...
- CnBlog客户端Windows Live Write安装方法
官方帮助http://space.cnblogs.com/forum/topic/8550 注:如果自动配置没有成功,需要手动配置: a) 在"Type of weblog that yo ...
- Exploratory Undersampling for Class-Imbalance Learning
Abstract - Undersampling is a popular method in dealing with class-imbalance problems, which uses on ...
- 解决PHP大文件上传问题
PHP大文件上传问题 今天负责创业计划大赛的老师问我作品上报系统上传不了大文件,我当时纳闷了,做的时候没限制上传文件的大小阿,怎么会传不了呢,自己亲自体验了番,果然不 行,想了好一会儿才有点眉目 ...
- 简单阐述下OC中UIImage三种创建方式~~~
一. 直接使用imageNamed进行创建 UIImage * image = [UIImage imageNamed:@"1.jpg"]; 简单说一下这种方式的优缺点: 优点:代 ...
- html基础01
一.HTML 基础 1.HTML基础标签:<head> <bady> <tittle>XXX<tittle>:为标签 2.HTML标题 <h1&g ...
- docker 源码分析 三(基于1.8.2版本),NewDaemon启动
本文来分析一下New Daemon的启动过程:在daemon/daemon.go文件中: func NewDaemon(config *Config, registryService *registr ...
- JAVA Arrays.binarySearch
转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...