for循环结构
语法1、
for 变量名 in 变量取值列表
do
指令集
done
提示:在此结构中“in 变量取值列表 ”可省略,省略时相当于in “[email protected]” 使用for i 就相当于使用 for i in "[email protected]"
=========================
语法2、 类似C语言
for ((表达式1;表达式2;表达式3))
do
指令集
done
其中表达式1是初始变量
表达式2是条件,如果这个条件成立就,执行循环指令集
表达式3是对表达式2的变量等进行操作,如自增,自减等
=========================
范例1. 轮询打印下面名称
[[email protected] 03]# cat for.sh |||
#!/bin/sh |||
for i in lvnian01 lvnian02 lvnian03 lvnian04 |||
do |||
echo $i |||
done |||
[[email protected] 03]# sh for.sh |||
lvnian01 |||
lvnian02 |||
lvnian03 |||
lvnian04 |||
[[email protected] 03]# |||
=========================== |||
[[email protected] 03]# cat for.sh
#!/bin/sh
for i in `seq 10 20 100`
do
echo $i
done
##seq 上面的 第一个数字是初始值,第二个是间隔,第三个最大值,意思是从10开始输出,输出间隔为20,输出的最大值不能超过100
[[email protected] 03]# sh for.sh
10
30
50
70
90
[[email protected] 03]#
##################################################
范例2.开发脚本实现进设置开机自启动sshd rsyslog crond network sysstat等服务。
[[email protected] 03]# cat for.sh
#!/bin/sh
for a in `chkconfig --list|awk ‘{print $1}‘`
do
/sbin/chkconfig $a off
/sbin/chkconfig --list $a
done
for i in sshd rsyslog crond network sysstat
do
/sbin/chkconfig $i on
/sbin/chkconfig --list $i
done
[[email protected] 03]# sh for.sh
abrt-ccpp 0:off 1:off 2:off 3:off 4:off 5:off 6:off
abrt-oops 0:off 1:off 2:off 3:off 4:off 5:off 6:off
abrtd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
acpid 0:off 1:off 2:off 3:off 4:off 5:off 6:off
......
......
sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
svnserve 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sysstat 0:off 1:on 2:off 3:off 4:off 5:off 6:off
udev-post 0:off 1:on 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[[email protected] 03]#
#######################################
范例3
求1+2+3+..+100和1+2+3+..+n之和
[[email protected] 03]# cat for.sh
#!/bin/sh
for i in `seq 100`
do
let sum=sum+i
done
echo $sum
echo "=================="
for ((i=1;i<=$1;i++))
do
let sum1=sum1+i
done
echo $sum1
[[email protected] 03]# sh for.sh 10
5050
==================
55
[[email protected] 03]#
#######################################
范例4
批量在data目录下处理10个文件 touch1-10
[[email protected] 03]# cat for.sh
#!/bin/sh
[ ! -d ./data ] && mkdir ./data -p && cd ./data
for i in `seq 10`
do
touch lvnian$i
done
[[email protected] 03]#
====
[[email protected] 03]# ls
for.sh for01.sh for02.sh for03.sh for04.sh ran.sh ziqidong.sh
[[email protected] 03]# sh for.sh
[[email protected] 03]# ls
data for.sh for01.sh for02.sh for03.sh for04.sh ran.sh ziqidong.sh
[[email protected] 03]# ls data/
lvnian1 lvnian10 lvnian2 lvnian3 lvnian4 lvnian5 lvnian6 lvnian7 lvnian8 lvnian9
[[email protected] 03]#
#######################################
范例5
把上面的文件名lvnian全部修改成xiaogao
[[email protected] 03]# cat for.sh
#!/bin/sh
cd ./data
for i in `ls ./`
do
mv $i `echo $i | sed ‘s/lvnian/xiaogao/‘`
done
[[email protected] 03]# ls data/
lvnian1 lvnian10 lvnian2 lvnian3 lvnian4 lvnian5 lvnian6 lvnian7 lvnian8 lvnian9
[[email protected] 03]# sh for.sh
[[email protected] 03]# ls data/
xiaogao1 xiaogao10 xiaogao2 xiaogao3 xiaogao4 xiaogao5 xiaogao6 xiaogao7 xiaogao8 xiaogao9
[[email protected] 03]#
#######################################
范例6
批量创建N个系统用户用户为user1-N,N是传参,你需要多少个用户N就是多少。密码是随机的10位任意字符。把账号和密码放在user.log文件中
[[email protected] 03]# cat for.sh
#!/bin/sh
num=$1
for n in `seq $num`
do
useradd user${n}
lvnian=`cat /dev/urandom |head -1|md5sum |cut -c 11-20 `
echo lvnian | passwd --stdin user${n}
echo "user${n} : --------------> $lvnian" >> user.log
done
[[email protected] 03]#
[[email protected] 03]# cat user.log
user1 : --------------> a1c15b266d
user2 : --------------> c4fc6b0c66
user3 : --------------> 22bd804b11
user4 : --------------> 8f1313bfa8
user5 : --------------> 0f14a7eba1
user6 : --------------> 1aa00f757f
user7 : --------------> eb7feb075e
user8 : --------------> fc8336e658
user9 : --------------> 24748ac3bf
user10 : --------------> 3b7f6ca3f0
#######################################