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

除了 if...then...fi 这种条件判断式之外,回圈可能是程序当中最重要的一环了~回圈可以不断的运行某个程序段落,直到使用者配置的条件达成为止。 所以,重点是那个『条件的达成』是什么。除了这种依据判断式达成与否的不定回圈之外, 还有另外一种已经固定要跑多少次的回圈形态,可称为固定回圈的形态呢!上一小节我们已经谈过不定回圈,下面我们来谈谈固定回圈的形态:

相对于while, until 的回圈方式是必须要『符合某个条件』的状态, 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...』之类的字样,则可以:

#!/bin/bash
#Use for ...loop to print 3 animals
for animal in dog cat elephant
do
echo "There are ${animal}s..."
done

让我们想像另外一种状况,由於系统上面的各种帐号都是写在 /etc/passwd 内的第一个栏位,你能不能透过管线命令的cut 捉出单纯的帐号名称后,以idfinger 分别检查使用者的识别码与特殊参数呢?由於不同的 Linux 系统上面的帐号都不一样!此时实际去捉 /etc/passwd 并使用回圈处理,就是一个可行的方案了!程序可以如下:

#!/bin/bash
#Use id,finger command to check system account's information.
users=$(cut -d: -f1 /etc/passwd)
for username in $users
do
id $username
finger $username
done

假如,我想要利用 ping 这个可以判断网络状态的命令, 来进行网络状态的实际侦测时,我想要侦测的网域是本机所在的 192.168.1.1~192.168.1.100,由于有 100 台主机, 总不会要我在 for 后面输入 1 到 100 吧?此时你可以这样做喔!
#!/bin/bash
#use ping command to check the network's PC state.
network="192.168.1"
for sitenum in $(seq 1 100) #seq为sequence(连续)的缩写之意
do
ping -c 1 -w 1 ${network}.${sitenum} &> /dev/null && result=0 || result=1
if [ "$result" == 0 ]; then
echo "Server ${network}.${sitenum} is UP"
else
echo "Server ${network}.${sitenum} is DOWN"
fi
done

看看很简单吧,我们来看一下ping的用法:

-c count
Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.


-n Numeric output only. No attempt will be made to lookup symbolic names for host addresses.


-w deadline
Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not stop after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.

-W timeout
Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.

我想要让使用者输入某个目录档名, 然后我找出某目录内的档名的权限,该如何是好?呵呵!可以这样做啦~

#!/bin/bash
#User input dir name, I find the permission of files.
read -p "Please input a directory:" dir
if [ "$dir" == "" -o ! -d "$dir" ]; then
echo "$dir doesn't exist in your system!"
exit 1
fi

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

执行结果如下:

[oracle@99bill-as9 loop]$ sh permission.sh
Please input a directory:/home/oracle/zy
The file /home/oracle/zy/20110901's permission is readable writable executable
The file /home/oracle/zy/20110902's permission is readable writable executable
The file /home/oracle/zy/20110903's permission is readable writable executable
The file /home/oracle/zy/20110904's permission is readable writable executable
The file /home/oracle/zy/20110906's permission is readable writable executable
The file /home/oracle/zy/20110907's permission is readable writable executable
The file /home/oracle/zy/20110908's permission is readable writable executable
The file /home/oracle/zy/ages.sh's permission is readable writable
The file /home/oracle/zy/case.sh's permission is readable writable
The file /home/oracle/zy/datefile.sh's permission is readable writable
The file /home/oracle/zy/date.sh's permission is readable writable
The file /home/oracle/zy/function2.sh's permission is readable writable
The file /home/oracle/zy/function.sh's permission is readable writable
The file /home/oracle/zy/loop's permission is readable writable executable
The file /home/oracle/zy/monitor.sh's permission is readable writable
The file /home/oracle/zy/para.sh's permission is readable writable
The file /home/oracle/zy/t.sh's permission is readable writable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值