一、for循环结构 1、for循环语法 for 变量 in 变量取值列表 do 指令 done
提示:在此结构中“in 变量取值列表”可以省略,省略时相当于in “$@”,使用for i就相当于使用for i in “$@”。 2、c语言型for循环语法 for ((exp1;exp2;exp3)) do 指令 done 实例1: [root@node01 day12]# vi for01_sh #!/bin/sh for i in 5 4 3 2 1 do echo $i done [root@node01 day12]# sh for01_sh 5 4 3 2 1 3、使用大括号 [root@node01 day12]# vi for02.sh #!/bin/sh for i in {5..1} do echo $i done [root@node01 day12]# sh for02.sh 5 4 3 2 4、使用seq [root@node01 day12]# vi for03.sh #!/bin/sh for i in `seq 5 -1 1` -1是步长 do echo $i done [root@node01 day12]# sh for03.sh 5 4 3 2 1 5、开机自启动实例 开发脚本仅设置sshd、crond、rsyslog、network自启动,其他
关闭。 [root@node01 day12]# vi auto_start.sh for name in $service #!/bin/sh LANG=en service=`chkconfig --list|grep 3:on|awk '{print $1}'` for name in $service do chkconfig $name off done for name in rsyslog network crond sshd sysstat do chkconfig $name on done [root@node01 day12]# sh auto_start.sh [root@node01 day12]# chkconfig --list|grep 3:on 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 rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 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 也可以一步实现
方法二: for wolf in `chkconig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|sshd|rsyslog|sysstat"`;do chkconfig $wolf off;done chkconfig --list | grep 3:on
方法三: chkconfig --list|grep 3:on|grep -vE "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig " $1 " off"}' |bash 1加到100 [root@node01 day12]# vi for-sum.sh #!/bin/sh for((i=0; i<=100; i++)) do ((j=j+i)) done echo $j [root@node01 day12]# sh for-sum.sh 5050 [root@node01 day12]# vi for_curl.sh #!/bin/sh for((i=0; i<=100; i++)) do curl http://www.baidu.com done 二、四道题 1、使用for循环在/data/wolf目录下批量创建10个
文件,
名称依次为: wolf-1 wolf-2 wolf-3 。。。 wolf-10 例如
随机创建10个
文件,包含wolf字符串,结尾是1-10,扩展名是html。 [root@node01 day12]# cat 01.sh #!/bin/sh [ ! -d /data/wolf ] && mkdir -p /data/wolf for i in `seq 10` do touch /data/wolf/wolf-${i}.html done [root@node01 data]# cd wolf [root@node01 wolf]# ls wolf-10.html wolf-1.html wolf-2.html wolf-3.html wolf-4.html wolf-5.html wolf-6.html wolf-7.html wolf-8.html wolf-9.html 2、将以上
文件名中的wolf全部改成linux(用for循环实现),并且扩展名改成大写,for循环的循环体不能出现wolf [root@node01 day12]# vi 02.sh #!/bin/sh for i in `seq 10` do cd /data/wolf mv wolf-${i}.html linux${i}.HTML done [root@node01 day12]# sh 02.sh [root@node01 day12]# ll /data/wolf/ total 0 -rw-r--r-- 1 root root 0 Aug 6 11:57 linux10.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux1.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux2.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux3.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux4.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux5.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux6.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux7.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux8.HTML -rw-r--r-- 1 root root 0 Aug 6 11:57 linux9.HTML -rw-r--r-- 1 root root 0 Aug 6 11:56 linux.HTML -rw-r--r-- 1 root root 0 Aug 6 11:56 linux{i}.HTML [root@node01 ~]# cd /data/wolf [root@node01 wolf]# ls wolf-10.html wolf-2.html wolf-4.html wolf-6.html wolf-8.html wolf-1.html wolf-3.html wolf-5.html wolf-7.html wolf-9.html [root@node01 wolf]# f=wolf-1.html [root@node01 wolf]# echo $f wolf-1.html [root@node01 wolf]# echo $f|sed 's#wolf#linux#g' linux-1.html 上面这个没对,没改扩展名
方法1、 [root@node01 wolf]# echo $f|sed 's#wolf#linux#g' linux-1.html [root@node01 wolf]# echo $f|sed 's#wolf#linux#g'|sed 's#html#HTML#g' linux-1.HTML [root@node01 wolf]# mv $f `echo $f |sed 's#wolf#linux#g'|sed 's#html#HTML#g'` [root@node01 wolf]# ll total 0 -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-1.HTML [root@node01 day12]# vi 03.sh #!/bin/sh cd /data/wolf for f in `ls *.html` do mv $f `echo $f |sed 's#wolf#linux#g'|sed 's#html#HTML#g'` done [root@node01 day12]# sh 03.sh [root@node01 wolf]# ll total 0 -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-10.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-1.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-2.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-3.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-4.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-5.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-6.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-7.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-8.HTML -rw-r--r-- 1 root root 0 Aug 6 11:58 linux-9.HTM
方法2 #!/bin/sh cd /data/wolf for f in `ls *.html` do #mv $f `echo $f|sed 's#wolf#linux#g'|sed 's#html#HTML#g'` mv $f `echo $f|sed 's#wolf\(.*\).html#linux\1.HTML#g'` done [root@node01 wolf]# ll total 0 -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-10.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-1.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-2.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-3.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-4.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-5.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-6.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-7.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-8.HTML -rw-r--r-- 1 root root 0 Aug 6 13:26 linux-9.HTML rename可以实现 3、批量创建10个系统帐号wolf01-wolf10并设置密码(密码不能相同) [root@node01 day12]# vi 05.sh #!/bin/sh for i in `seq -w 10` do useradd wolf$i&&\ echo "root$i"|passwd --stdin wolf$i done [root@node01 day12]# sh 05.sh Changing password for user wolf01. passwd: all authentication tokens updated successfully. Changing password for user wolf02. passwd: all authentication tokens updated successfully. Changing password for user wolf03. passwd: all authentication tokens updated successfully. Changing password for user wolf04. passwd: all authentication tokens updated successfully. Changing password for user wolf05. passwd: all authentication tokens updated successfully. Changing password for user wolf06. passwd: all authentication tokens updated successfully. Changing password for user wolf07. passwd: all authentication tokens updated successfully. Changing password for user wolf08. passwd: all authentication tokens updated successfully. Changing password for user wolf09. passwd: all authentication tokens updated successfully. Changing password for user wolf10. passwd: all authentication tokens updated successfully. [root@node01 day12]# su - wolf01 welcome to wolf linux [wolf01@node01 ~]$ id uid=
502(wolf01) gid=
502(wolf01) groups=
502(wolf01) [wolf01@node01 ~]$ su - wolf02 Password: welcome to wolf linux [wolf02@node01 ~]$ [wolf02@node01 ~]$ whoami wolf02 技术点 a:00 01 02 03。。。10 b:无交互设置密码 也可以这样 -p,--password PASSWORD The encrypted password,as returned by crypt(3). The default is to disable the password. Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes. You should make sure the password respects the system´s password policy. #!/bin/sh for i in `seq -w 10` do userdel -r wolf$i useradd wolf$i&& -p root$i # echo "root$i"|passwd --stdin wolf$i done 4、批量创建10个系统帐号wolf01-wolf10并设置密码(密码
随机8位字符串) [root@node01 day12]# echo $RANDOM 16803 [root@node01 day12]# echo $((RANDOM+100000000)) 100006409 [root@node01 day12]# echo $RANDOM|md5sum 32位 8ba9344650cf6474a17359692e5932b0 - [root@node01 day12]# echo $RANDOM|md5sum|cut -c 1-8 使用cut取8位 a6b44f06 [root@node01 day12]# echo "`date`$RANDOM"|md5sum|cut -c 1-8 1362138d [root@node01 day12]# echo "`date +%s`$RANDOM"|md5sum|cut -c 1-8 2a4e0da6
删除脚本 [root@node01 day12]# vi rm_user.sh #!/bin/sh for i in `seq -w 10` do userdel -r wolf$i done
随机数用两次的话 需要定义变量 [root@node01 day12]# #!/bin/sh for i in `seq -w 10` do pass="`date +%s`$RANDOM"|md5sum|cut -c 1-8` useradd wolf$i&&\ echo "$pass"|passwd --stdin wolf$i echo -e "wolf $i\t $pass" >>/data/wolf/pass.txt done 标准
方法 [root@node01 day12]# vi 07.sh #!/bin/sh for i in `seq -w 10` do pass=`echo "$RANDOM"|md5sum|cut -c 1-8` useradd wolf$i&&\ echo "$pass"|passwd --stdin wolf$i echo -e "wolf $i\t $pass" >>/data/wolf/pass.txt done [root@node01 day12]# sh 07.sh Changing password for user wolf01. passwd: all authentication tokens updated successfully. Changing password for user wolf02. passwd: all authentication tokens updated successfully. Changing password for user wolf03. passwd: all authentication tokens updated successfully. Changing password for user wolf04. passwd: all authentication tokens updated successfully. Changing password for user wolf05. passwd: all authentication tokens updated successfully. Changing password for user wolf06. passwd: all authentication tokens updated successfully. Changing password for user wolf07. passwd: all authentication tokens updated successfully. Changing password for user wolf08. passwd: all authentication tokens updated successfully. Changing password for user wolf09. passwd: all authentication tokens updated successfully. Changing password for user wolf10. passwd: all authentication tokens updated successfully. [root@node01 day12]# cat /data/wolf/pass.txt wolf 01 87d5ca7c wolf 02 1a394ec2 wolf 03 d9497158 wolf 04 b682731c wolf 05 0463a374 wolf 06 24055dba wolf 07 30a02856 wolf 08 1796c864 wolf 09 0944b988 wolf 10 a42f8a52 [root@node01 day12]# vi 07.sh #!/bin/sh for i in `seq -w 10` do pass=`echo "$RANDOM"|md5sum|cut -c 1-8` useradd wolf$i&&\ echo "$pass"|passwd --stdin wolf$i echo -e "wolf$i\t $pass" >>/data/wolf/pass.txt done wolf01 e6e49473 wolf02 8d986dfd wolf03 b915f9d9 wolf04 7e419d06 wolf05 4c45b423 wolf06 c79eb81b wolf07 7381ad8d wolf08 a9caaa8b wolf09 a599dd26 wolf10 e170f057 ============================================ 1.for … in 语句 for 变量 in seq字符串 do 指令 done 说明:seq字符串 只要用空格字符分割,每次for…in 读取时候,就会按顺序将读到值,给前面的变量 #!/bin/sh for i in $(seq 10); do echo $i; done; 2.for((赋值;条件;运算语句)) for((赋值;条件;运算语句)) do action done; #!/bin/sh for((i=1;i<=10;i++));do echo $i; done; ================================================= 学习资料 第一类:数字性循环 ----------------------------- for1-1.sh #!/bin/bash for((i=1;i<=10;i++)); do echo $(expr $i \* 3 + 1); done ----------------------------- for1-2.sh #!/bin/bash for i in $(seq 1 10) do echo $(expr $i \* 3 + 1); done ----------------------------- for1-3.sh #!/bin/bash for i in {1..10} do echo $(expr $i \* 3 + 1); done ---------------------------- for1-4.sh #!/bin/bash awk 'BEGIN{for(i=1; i<=10; i++) print i}' 第二类:字符性循环 ----------------------------- for2-1.sh #!/bin/bash for i in `ls`; do echo $i is file name\! ; done ----------------------------- for2-2.sh #!/bin/bash for i in $* ; do echo $i is input chart\! ; done ----------------------------- for2-3.sh #!/bin/bash for i in f1 f2 f3 ; do echo $i is appoint ; done ----------------------------- for2-4.sh #!/bin/bash list="rootfs usr data data2" for i in $list; do echo $i is appoint ; done 第三类:路径查找 ----------------------------- for3-1.sh #!/bin/bash for file in /proc/*; do echo $file is file path \! ; done ----------------------------- for3-2.sh #!/bin/bash for file in $(ls *.sh) do echo $file is file path \! ; done # 三.循环控制语句 # break 命令不执行当前循环体内break下面的语句从当前循环
退出. # continue 命令是程序在本循体内忽略下面的语句,从循环头开始执行