shell脚本常用脚本:for循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
[[email protected] 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"
[[email protected] 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
[[email protected] 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
[[email protected] 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 ]
[[email protected] 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
[[email protected] for]#
删除脚本创建的用户账号
[[email protected] for]# for n in $(seq -w 10);do userdel -r jorbabe-$n;done
以上是关于shell脚本常用脚本:for循环的主要内容,如果未能解决你的问题,请参考以下文章
不会做 还不来看?! for/while常用循环例题 shell脚本