码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本常用脚本:for循环

时间:2018-03-18 12:06:10      阅读:1517      评论:0      收藏:0      [点我收藏+]

标签:linux   shell   for循环   批量创建账号   批量创建文件   

 shell脚本常用脚本:for循环

wheil 很多循环可以用for循环替换

 

for循环语法结构

 

for 变量名 in 变量取值列表

         do

                  指令

done

 

for ((exp1;exp2;exp3))

         do

                  指令

Done

 

脚本实例:for  99乘法表

#!/bin/bash

#Date       :2016-11-22 15:04:12 ##date "+%Y-%m-%d %H:%M:%S"

#Author   :jorbabe

#Mail       :jorbabe@163.com

#Function        :99乘法表

#Version  :版本 V1.1

#Update           :2016-11-22 15:04:12

 

for a in `seq 1 9`

do

        for b in `seq 1 9`

        do

                if [ $a -ge $b ];then

                echo -en "\t$a x $b" = $(expr  $a \* $b)

                fi

        done

echo "  "

done

[root@centos-b for]# ./for02.sh   

        1 x 1 = 1 

        2 x 1 = 2       2 x 2 = 4 

        3 x 1 = 3       3 x 2 = 6       3 x 3 = 9 

        4 x 1 = 4       4 x 2 = 8       4 x 3 = 12      4 x 4 = 16 

        5 x 1 = 5       5 x 2 = 10      5 x 3 = 15      5 x 4 = 20      5 x 5 = 25 

        6 x 1 = 6       6 x 2 = 12      6 x 3 = 18      6 x 4 = 24      6 x 5 = 30      6 x 6 = 36 

        7 x 1 = 7       7 x 2 = 14      7 x 3 = 21      7 x 4 = 28      7 x 5 = 35      7 x 6 = 42      7 x 7 = 49 

        8 x 1 = 8       8 x 2 = 16      8 x 3 = 24      8 x 4 = 32      8 x 5 = 40      8 x 6 = 48      8 x 7 = 56      8 x 8 = 64 

        9 x 1 = 9       9 x 2 = 18      9 x 3 = 27      9 x 4 = 36      9 x 5 = 45      9 x 6 = 54      9 x 7 = 63      9 x 8 = 72      9 x 9 = 81 

 

 

  脚本实例:for  1-100求和

 

#!/bin/bash

#Date       :2016-11-22 15:04:12 ##date "+%Y-%m-%d %H:%M:%S"

#Author   :jorbabe

#Mail       :jorbabe@163.com

#Function        :1-100求和

#Version  :版本 V1.1

#Update           :2016-11-22 15:04:12

 

for ((i=0; i<=100; i++))

        do

        ((j=j+i))

done

#echo $j

[ -n "$j" ] && printf "totalsum is:$j\n"

 

 

[root@centos-b for]# ./for03.sh  

totalsum is:5050

 

 

脚本实例:批量创建文件

 

#!/bin/bash

#Date       :2016-11-22 15:04:12 ##date "+%Y-%m-%d %H:%M:%S"

#Author   :jorbabe

#Mail       :jorbabe@163.com

#Function        :批量创建文件

#Version  :版本 V1.1

#Update           :2016-11-22 15:04:12

#递归创建文件,并进入目录

mkdir -p /tmp/shell/for/test/ && cd /tmp/shell/for/test

for filenum in `seq 10`

do

        touch jorbabe-$filenum

done

 

 

[root@centos-b for]# ll test/    

total 0

-rw-r--r--. 1 root root 0 Nov 22 10:36 jorbabe-1

-rw-r--r--. 1 root root 0 Nov 22 10:36 jorbabe-10

-rw-r--r--. 1 root root 0 Nov 22 10:36 jorbabe-2

.............

 

 脚本实例:批量创建用户设置密码

#!/bin/bash

#Date       :2016-11-22 15:04:12 ##date "+%Y-%m-%d %H:%M:%S"

#Author   :jorbabe

#Mail       :jorbabe@163.com

#Function        :批量创建用户并设置随机密码

#Version  :版本 V1.1

#Update           :2016-11-22 15:04:12

#调用系统库

. /etc/init.d/functions

#>/tmp/shell/for/test/user.log

#>/tmp/shell/for/test/fail_user.log

#数值01-10

for n in $(seq -w 10)

do

#时间格式

        tim=`date "+%Y_%m_%d %H:%M:%S"`

                  #随机密码

        passwd=`echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9`

                  #创建用户账号

        useradd jorbabe-$n >&/dev/null && user_status=$?

                  #设置用户密码

        echo "$passwd"|passwd --stdin jorbabe-$n >&/dev/null && pass_status=$?

                 

                  #检查用户创建于设置情况

        if  [ $user_status -eq 0 -a $pass_status -eq 0 ] ; then

                action "adduser jorbabe-$n" /bin/true

                  #创建成功,生成账户信息写到log

                echo -e "$tim\tuser: jorbabe-$n\tpass: $passwd" >>/tmp/shell/for/test/user.log

        else

                action "adduser jorbabe-$n" /bin/false

                  #创建失败,生成账户信息写到log

                echo -e "$tim\tuser:\tjorbabe-$n pass:\t$passwd" >>/tmp/shell/for/test/fail_user.log

        fi

done

 

[root@centos-b for]# ./for05.sh       

adduser jorbabe-01                                         [  OK  ]

adduser jorbabe-02                                         [  OK  ]

adduser jorbabe-03                                         [  OK  ]

adduser jorbabe-04                                         [  OK  ]

adduser jorbabe-05                                         [  OK  ]

adduser jorbabe-06                                         [  OK  ]

adduser jorbabe-07                                         [  OK  ]

adduser jorbabe-08                                         [  OK  ]

adduser jorbabe-09                                         [  OK  ]

adduser jorbabe-10                                         [  OK  ]

[root@centos-b for]# cat test/user.log

2016_11_22 12:08:43     user: jorbabe-01        pass: 84327849

2016_11_22 12:08:43     user: jorbabe-02        pass: cf11a1ee

2016_11_22 12:08:43     user: jorbabe-03        pass: 2a60233d

2016_11_22 12:08:43     user: jorbabe-04        pass: 2d181418

2016_11_22 12:08:43     user: jorbabe-05        pass: 641f69d6

2016_11_22 12:08:43     user: jorbabe-06        pass: b8d57d2a

2016_11_22 12:08:43     user: jorbabe-07        pass: ae123cb3

2016_11_22 12:08:44     user: jorbabe-08        pass: db1f73c1

2016_11_22 12:08:44     user: jorbabe-09        pass: a6bc81e1

2016_11_22 12:08:44     user: jorbabe-10        pass: 22aac1f2

[root@centos-b for]#

删除脚本创建的用户账号

[root@centos-b for]# for n in $(seq -w 10);do userdel -r jorbabe-$n;done


shell脚本常用脚本:for循环

标签:linux   shell   for循环   批量创建账号   批量创建文件   

原文地址:http://blog.51cto.com/xianlei/2088011

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!