while do done, until do done (不定循环)

while [ condition ]  <==中括号内的状态就是判断式
do <==do 是回圈的开始!
程序段落
done <==done 是回圈的结束
until [ condition ]
do
程序段落
done
[root@www scripts]# vi sh13.sh
#!/bin/bash
# Program:
# Repeat question until user input correct answer.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[root@www scripts]# vi sh13-2.sh
#!/bin/bash
# Program:
# Repeat question until user input correct answer.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH until [ "$yn" == "yes" -o "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[root@www scripts]# vi sh14.sh
#!/bin/bash
# Program:
# Use loop to calculate "1+2+3+...+100" result.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH s=0 # 这是加总的数值变量
i=0 # 这是累计的数值,亦即是 1, 2, 3....
while [ "$i" != "100" ]
do
i=$(($i+1)) # 每次 i 都会添加 1
s=$(($s+$i)) # 每次都会加总一次!
done
echo "The result of '1+2+3+...+100' is ==> $s"

for...do...done (固定回圈)

for 这种语法,则是『
已经知道要进行几次回圈』的状态!他的语法是:

for var in con1 con2 con3 ...
do
程序段
done

以上面的例子来说,这个 $var 的变量内容在回圈工作时:

  1. 第一次回圈时, $var 的内容为 con1 ;
  2. 第二次回圈时, $var 的内容为 con2 ;
  3. 第三次回圈时, $var 的内容为 con3 ;
  4. ....

我们可以做个简单的练习。假设我有三种动物,分别是 dog, cat, elephant 三种, 我想每一行都输出这样:『There are dogs...』之类的字样,则可以:

[root@www scripts]# vi sh15.sh
#!/bin/bash
# Program:
# Using for .... loop to print 3 animals
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH for animal in dog cat elephant
do
echo "There are ${animal}s.... "
done
[root@www scripts]# vi sh16.sh
#!/bin/bash
# Program
# Use id, finger command to check system account's information.
# History
# 2009/02/18 VBird first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
users=$(cut -d ':' -f1 /etc/passwd) # 撷取帐号名称
for username in $users # 开始回圈进行!
do
id $username
finger $username
done
[root@www scripts]# vi sh17.sh
#!/bin/bash
# Program
# Use ping command to check the network's PC state.
# History
# 2009/02/18 VBird first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
network="192.168.1" # 先定义一个网域的前面部分!
for sitenu in $(seq 1 100) # seq 为 sequence(连续) 的缩写之意
do
# 底下的程序在取得 ping 的回传值是正确的还是失败的!
ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
# 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN)
if [ "$result" == 0 ]; then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done

上面这一串命令运行之后就可以显示出 192.168.1.1~192.168.1.100 共 100 部主机目前是否能与你的机器连通! 如果你的网域与鸟哥所在的位置不同,则直接修改上头那个 network 的变量内容即可!其实这个范例的重点在 $(seq ..) 那个位置!那个 seq 是连续 (sequence) 的缩写之意!代表后面接的两个数值是一直连续的!

[root@www scripts]# vi sh18.sh
#!/bin/bash
# Program:
# User input dir name, I find the permission of files.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH # 1. 先看看这个目录是否存在啊?
read -p "Please input a directory: " dir
if [ "$dir" == "" -o ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit 1
fi # 2. 开始测试文件罗~
filelist=$(ls $dir) # 列出所有在该目录下的文件名称
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done

for...do...done 的数值处理

for (( 初始值; 限制值; 运行步阶 ))
do
程序段
done
  • 初始值:某个变量在回圈当中的起始值,直接以类似 i=1 配置好;
  • 限制值:当变量的值在这个限制值的范围内,就继续进行回圈。例如 i<=100;
  • 运行步阶:每作一次回圈时,变量的变化量。例如 i=i+1。

值得注意的是,在『运行步阶』的配置上,如果每次添加 1 ,则可以使用类似『i++』的方式,亦即是 i 每次回圈都会添加一的意思。

[root@www scripts]# vi sh19.sh
#!/bin/bash
# Program:
# Try do calculate 1+2+....+${your_input}
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "Please input a number, I will count for 1+2+...+your_input: " nu s=0
for (( i=1; i<=$nu; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+$nu' is ==> $s"

 

第十三章、学习 Shell Scripts 循环 (loop)的更多相关文章

  1. 第十三章、学习 Shell Scripts

    什么是 Shell scripts shell script (程序化脚本) :shell script 是针对 shell 所写的『脚本!』 shell script 是利用 shell 的功能所写 ...

  2. 鸟哥的Linux私房菜——第十六章:学习Shell Scripts

    视频链接:http://www.bilibili.com/video/av10565321/ 1. 什么是 Shell Script       (shell写的脚本)1.1 干嘛学习 shell s ...

  3. 鸟哥的Linux私房菜-第10/11/12/13章(vim程序编辑器、学习bash、正则表达式与文件格式化处理、学习Shell Scripts)

    第10章 vim程序编辑器 可以将vim看做vi的进阶版本,vim可以用颜色或底线等方式来显示出一些特殊的信息. 为何要学习vim?因为: a. 所有的 Unix Like 系统都会内建 vi 文书编 ...

  4. 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记

    第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...

  5. 第13章 学习shell script

    由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...

  6. 鸟哥的linux私房菜——第十三章学习(Linux 帐号管理与 ACLL 权限设置)

    第十三章.Linux 帐号管理与 ACLL 权限设置 1.0).使用者识别码: UID 与 GID UID :User ID GID :group ID [root@study ~]# ll -d / ...

  7. 第十三章、学习 Shell Scripts 条件判断式

    利用 if .... then 单层.简单条件判断式 if [ 条件判断式 ]; then 当条件判断式成立时,可以进行的命令工作内容: fi <==将 if 反过来写,就成为 fi !结束 i ...

  8. 第十三章、学习 Shell Scripts 善用判断式

    善用判断式 利用 test 命令的测试功能 我要检查 /dmtsai 是否存在时,使用: [root@www ~]# test -e /dmtsai [root@www ~]# test -e /dm ...

  9. 第十三章、学习 Shell Scripts 简单的 shell script 练习

    简单的 shell script 练习 简单范例 对谈式脚本:变量内容由使用者决定 [root@www scripts]# vi sh02.sh #!/bin/bash # Program: # Us ...

随机推荐

  1. poj 3440 Coin Toss 概率问题

    这题主要是推导数学公式!!! 将概率问题转化为圆心所在的面积! 代码如下: #include<iostream> #include<stdio.h> #include<a ...

  2. linux 磁盘读写性能测试

    1. 测试读取速度 haparm -Tt /dev/xxx 1.1 获取硬盘设备名称: fdisk -l Disk /dev/xvdf: 365.0 GB, 365041287168 bytes 25 ...

  3. ZOJ 2563 Long Dominoes(状态压缩DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1563 题目大意:在h*w的矩阵里铺满1*3的小矩阵,共有多少种方法 ...

  4. Android:控件Spinner实现下拉列表

    在Web开发中,HTML提供了下拉列表的实现,就是使用<select>元素实现一个下拉列表,在其中每个下拉列表项使用<option>表示即可.这是在Web开发中一个必不可少的交 ...

  5. 编程之美:1.9高效率安排见面会 图的m着色问题 回溯法

    原书问题,可以转换为图的m着色问题 ,下面该问题的代码 这里有参考ppt与code,免积分载 http://download.csdn.net/detail/u011467621/6341195 // ...

  6. linux系统灵活运用灯[android课程3]

    1,文件如何生成: ----- ---- (二),把hello例子贴过来后,编译问题: 在编译Android 4.0驱动的时候,使用到了proc_dir_entry结构体中的owner成员,但是编译的 ...

  7. 25.allegro中模块复用[原创]

    一,Module reuse 1,打开原理图 ------------------- --------------------- ctrl+i过滤器 直选part ctrl+e 查看属性 查看: 是否 ...

  8. NFC(8)关于新买的标签的格式化

    有多种方法格式化nfc标签设备. 如搜相关的手机上应用,在应用里选择格式类型 本文是用代码手动格式 public void writeNFCTag(Tag tag) { if (tag == null ...

  9. Java之iterator迭代器和iterable接口

    java.lang.Iterable java.util.Iterator Iterator是迭代器类,而Iterable是接口. 好多类都实现了Iterable接口,这样对象就可以调用iterato ...

  10. UVALive Proving Equivalences (强连通分量,常规)

    题意: 给一个有向图,问添加几条边可以使其强连通. 思路: tarjan算法求强连通分量,然后缩点求各个强连通分量的出入度,答案是max(入度为0的缩点个数,出度为0的缩点个数). #include ...