[17]shell中for-loop[案例有文档扫描、备份文件、打印棋盘]

一, For Loop
1, 循环的定义:计算机可以重复一而再,再而三的执行特定的指令,直到给定的条件满足为止,重复执行的这一组指令就叫做loop
2, Bash中支持
for loop
while loop
注:每个循环使用时,首先,用在循环中的变量必须要在循环前初始化。对每次循环都需要对条件做测试。在循环体 中执行一次需要对循环测试条件值修改。
3, for loop
语法
for  变量名  in 列表
do
循环体
done
4, 列表就是一个集合,集合又空格间隔,可以是以下的几种:
数字/字符集合
1  2  5 7 9
{1..254}
文件集合
/etc/*.conf
指令结果的集合
$(  find /etc/ -perm -003  )
5,
[root@rhel rc3.d]# echo "file "{1,2,3,4,5,6}
file 1 file 2 file 3 file 4 file 5 file 6
[root@rhel rc3.d]#
6, for loop 实例一
[root@rhel helinbash]# touch file{1,2,3,4,5}
[root@rhel helinbash]# ls
99           file2  file5       menu2.sh     testparam.sh   welcome.sh
changdir.sh  file3  forloop.sh  menu.sh      testserver.sh
file1        file4  input.sh    readable.sh  truefalse.sh
[root@rhel helinbash]# ls file*
file1  file2  file3  file4  file5
[root@rhel helinbash]# chmod a+x forloop.sh
[root@rhel helinbash]# vim forloop.sh
[root@rhel helinbash]# ./forloop.sh            
You are going to remove the file1
You are going to remove the file2
You are going to remove the file3
You are going to remove the file5
[root@rhel helinbash]# ls file*
file4
[root@rhel helinbash]# cat forloop.sh
#!/bin/bash
7, for FILENAME  in file1 file2 file3 file5
do

          echo "You are going to remove the $FILENAME"
          rm  $FILENAME -f

done
[root@rhel helinbash]#
8, for loop实例二
[root@rhel helinbash]# echo 172.24.0.{1..10}
172.24.0.1 172.24.0.2 172.24.0.3 172.24.0.4 172.24.0.5 172.24.0.6 172.24.0.7 172.24.0.8 172.24.0.9 172.24.0.10
[root@rhel helinbash]#
[root@rhel helinbash]# ./firewall.sh           
[root@rhel helinbash]# cat /root/setfirewall.sh
#!/bin/bash
iptables -I INPUT -p tcp -s 172.24.0.1 -j ACCEPT
iptables -I INPUT -p tcp -s 172.24.0.2 -j ACCEPT
iptables -I INPUT -p tcp -s 172.24.0.3 -j ACCEPT
iptables -I INPUT -p tcp -s 172.24.0.4 -j ACCEPT
iptables -I INPUT -p tcp -s 172.24.0.5 -j ACCEPT
iptables -I INPUT -p tcp -s 172.24.0.6 -j ACCEPT
iptables -I INPUT -p tcp -s 172.24.0.7 -j ACCEPT

[root@rhel helinbash]# cat firewall.sh
#!/bin/bash
DST_FILENAME=/root/setfirewall.sh
echo "#!/bin/bash" > $DST_FILENAME
for IP  in  {1..254}
do
      echo "iptables -I INPUT -p tcp -s 172.24.0.$IP -j ACCEPT " >> $DST_FILENAME
done
chmod a+x $DST_FILENAME
[root@rhel helinbash]#
dirname是文件之前的所有文件夹。
[root@rhel ~]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
[root@rhel ~]#
basename得到是文件的最后的文件名的一部分
[root@rhel ~]# basename /etc/sysconfig/network-scripts/ifcfg-eth0   
ifcfg-eth0
[root@rhel ~]#
9, 备份文件
(1),
[root@rhel helinshell]# ./backup_conf.sh
backing up the file ----&gt /etc/autofs_ldap_auth.conf
`/etc/autofs_ldap_auth.conf' -> `/var/tmp/autofs_ldap_auth.conf.2014-05-24'
Finished backup ----&gt /etc/autofs_ldap_auth.conf
backing up the file ----&gt /etc/capi.conf
`/etc/capi.conf' -> `/var/tmp/capi.conf.2014-05-24'
Finished backup ----&gt /etc/capi.conf
backing up the file ----&gt /etc/cdrecord.conf
`/etc/cdrecord.conf' -> `/var/tmp/cdrecord.conf.2014-05-24'
Finished backup ----&gt /etc/cdrecord.conf
[root@rhel helinshell]# cat backup_conf.sh
#!/bin/bash
DST_DIR=/var/tmp
DATE_SUFFIX=$(date +%F)
for FILENAME in /etc/*.conf
do
              echo "backing up the file ----&gt $FILENAME"
              TMPFILE=$(basename $FILENAME)
              cp -v $FILENAME $DST_DIR/$TMPFILE.$DATE_SUFFIX
              echo "Finished backup ----&gt $FILENAME"
done
[root@rhel helinshell]#
(2), 测试
[root@rhel tmp]# ls
autofs_ldap_auth.conf.2014-05-24  modprobe.conf.2014-05-24
capi.conf.2014-05-24              mtools.conf.2014-05-24
cdrecord.conf.2014-05-24          multipath.conf.2014-05-24
conman.conf.2014-05-24            named.caching-nameserver.conf.2014-05-24
dhcp6c.conf.2014-05-24            nscd.conf.2014-05-24
dnsmasq.conf.2014-05-24           nsswitch.conf.2014-05-24
esd.conf.2014-05-24               ntp.conf.2014-05-24
gpm-root.conf.2014-05-24          pam_smb.conf.2014-05-24
grub.conf.2014-05-24              prelink.conf.2014-05-24
gssapi_mech.conf.2014-05-24       reader.conf.2014-05-24
host.conf.2014-05-24              resolv.conf.2014-05-24
10, 实例四
(1), 配置文件
[root@rhel helinshell]# vim /etc/testservers.conf
[root@rhel helinshell]# cat /etc/testservers.conf
# This file is the host
#172.24.254.254
192.168.1.1
# FTP server
192.168.2.2
127.0.0.1
[root@rhel helinshell]#
(2),
[root@rhel tmp]# cat /etc/testservers.conf | grep -Ev '^(#)'


192.168.1.1

192.168.2.2

127.0.0.1

You have new mail in /var/spool/mail/root
[root@rhel tmp]#
(3),
[root@rhel tmp]# cat /etc/testservers.conf | grep -Ev '^(#|$)'
192.168.1.1
192.168.2.2
127.0.0.1
[root@rhel tmp]#

[root@rhel helinshell]# ./testservers.sh  
192.168.1.1 is down
192.168.2.2 is down
127.0.0.1 is online
[root@rhel helinshell]#
[root@rhel helinshell]# cat testservers.sh   
#!/bin/bash

for IP in $(cat /etc/testservers.conf | grep -Ev '^(#|$)')
do
######################### test a host #########################
       ##################################################
       #IP=$1
       ping $IP -c2 &> /dev/null
       RES=$?
       if [ $RES -eq 0 ] ; then
           echo "$IP is online"
       else

           echo "$IP is down"
       fi
       #####################################################

done
[root@rhel helinshell]#
二, 文档全文搜索
1,
[root@rhel helinshell]# vim fullsearch.sh
[root@rhel helinshell]# chmod  a+x fullsearch.sher
chmod: cannot access `fullsearch.sher': No such file or directory
[root@rhel helinshell]# chmod  a+x fullsearch.sh
[root@rhel helinshell]# ./fullsearch.sh
Usage: ./fullsearch.sh
[root@rhel helinshell]# ./fullsearch.sh root /etc
find: root: No such file or directory
[root@rhel helinshell]# ./fullsearch.sh  /etc root
The file ---&gt /etc/ldap.conf has the keyword
The file ---&gt /etc/vsftpd/vsftpd.conf has the keyword
The file ---&gt /etc/vsftpd/user_list has the keyword
The file ---&gt /etc/vsftpd/ftpusers has the keyword
The file ---&gt /etc/dnsmasq.conf has the keyword
The file ---&gt /etc/rpm/macros.jpackage has the keyword
The file ---&gt /etc/xinetd.d/kshell has the keyword
The file ---&gt /etc/xinetd.d/eklogin has the keyword
The file ---&gt /etc/xinetd.d/krb5-telnet has the keyword
The file ---&gt /etc/xinetd.d/klogin has the keyword
The file ---&gt /etc/xinetd.d/swat has the keyword
The file ---&gt /etc/xinetd.d/rsync has the keyword
The file ---&gt /etc/xinetd.d/gssftp has the keyword
The file ---&gt /etc/xinetd.d/ekrb5-telnet has the keyword
The file ---&gt /etc/xinetd.d/tcpmux-server has the keyword
The file ---&gt /etc/xinetd.d/tftp has the keyword
The file ---&gt /etc/xinetd.d/rmcp has the keyword
The file ---&gt /etc/dbus-1/system.d/wpa_supplicant.conf has the keyword
The file ---&gt /etc/dbus-1/system.d/nm-system-settings.conf has the keyword
The file ---&gt /etc/dbus-1/system.d/bluez-hcid.conf has the keyword
The file ---&gt /etc/dbus-1/system.d/hal.conf has the keyword
2,
[root@rhel helinshell]# cat fullsearch.sh
#!/bin/bash

if [ $# -eq 2  ] ; then


     DIR=$1
     KEYWORD=$2

     for FILENAME  in $(find $DIR -type f)
     do
          if grep $KEYWORD $FILENAME &> /dev/null ; then
          echo "The file ---&gt $FILENAME has the keyword"
          #else
          fi
     done   
else

    echo  "Usage: $0

fi
[root@rhel helinshell]#
三, 九九乘法表
[root@rhel helinshell]# vim 9X9.sh
[root@rhel helinshell]# ./9X9.sh
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
3 * 1 = 3
[root@rhel helinshell]# cat 9X9.sh
#!/bin/bash

for A in {1..9}

do

##################################
for  B   in  {1..9}

do
        RES=$(expr $A \* $B)
        echo "$A * $B = $RES"

done

##################################

done
[root@rhel helinshell]#
四, 打印棋盘

[root@rhel helinshell]# cat chess.sh
#!/bin/bash

for ROW  in {1..9}

do
##########################

  for COL in {1..9}

  do

    POS=$(expr $ROW + $COL)    

    RES=$(expr $POS % 2)
    if [ $RES -eq 0 ] ; then
 
         ##################### white block ############
         echo -en "\033[30;40m \033[0m"  
     else
         ##################### black block ############
         echo -en "\033[37;47m \033[0m"
      fi
  done

      ###################### new line ##############
      echo ""

done

[root@rhel helinshell]#

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29611940/viewspace-1181675/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29611940/viewspace-1181675/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值