1. 程式人生 > >shell腳本常用腳本:for循環

shell腳本常用腳本:for循環

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 :[email protected]

#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 :[email protected]

#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 :[email protected]

#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 :[email protected]

#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循環