1.1 for循环语句
在计算机科学中,for循环(英语:for loop)是一种编程语言的迭代陈述,能够让程式码反复的执行。 它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或是循环变数。这使得for循环能够知道在迭代过程中的执行顺序。 1.1.1 shell中的for循环shell中的for 循环与在c中不同,它包含三种形式:第一种结构是列表for 循环;第二种结构就是不带列表的for循环;第三种就类似于C语言。 ① 列表for循环(常用) #!/bin/bash for i in 取值列表 do 循环主体/命令 done
② 不带列表for循环(示例) #!/bin/absh echo "惨绿少年的博客是:" for i do echo "$i" done 脚本执行结果 [root@clsn for]# sh for2.sh http://blog.znix.top 惨绿少年的博客是: http://blog.znix.top
|
③ 类似C语言的风格(这种用法常在C语语言中使用)
for((exp1;exp2;exp3)) do 指令... done
编写类似C语言风格脚本
for((i=0;i<=3;i++)) do echo $i done
脚本执行过程
1.1.2 不同语言的For循环
Shell中的两种样式
# 样式一: for i in 1 2 3 do echo $i done # 样式二: for i in 1 2 3;do echo $i;done
JAVA
for(int i = 0; i < 5; i++){ //循环语句; }
PHP
for ($i = 0; $i < 5; $i++) { # statements; }
VB
For i = 1 To 5 ===PASCAL=== for not i=1 do begin i=0; writeln('Go on!'); end. '循环语句 Next i
swift
var x = 0 for i in 1...100{ x += i } print(x) //5050 for _ in 1...100{ x += 1 } print(x) // 100 var box = [1,2,3,4,5] for i in box{ print(i) } /* 1 2 3 4 5 */ ---
1.2 for循环相关练习题
1.2.1 【练习题1】批量生成随机字符文件名案例
使用for循环在/clsn目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串clsn,名称示例如下:
[root@znix C19]# ls /clsn apquvdpqbk_clsn.html mpyogpsmwj_clsn.html txynzwofgg_clsn.html bmqiwhfpgv_clsn.html udrzobsprf_clsn.html vjxmlflawa_clsn.html jhjdcjnjxc_clsn.html qeztkkmewn_clsn.html jpvirsnjld_clsn.html ruscyxwxai_clsn.html
脚本内容
1 [root@clsn for]# cat make_file.sh 2 #!/bin/bash 3 ############################################################# 4 # File Name: make_file.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-08 11:01:19 9 # Description: 10 ############################################################# 11 12 [ -d /clsn ] || mkdir -p /clsn 13 rpm -qa |grep pwgen &>/dev/null 14 if [ $? -eq 1 ] 15 then 16 yum install pwgen -y &>/dev/null 17 fi 18 19 cd /clsn &&\ 20 for i in { 1..10} 21 do 22 #File_Name=`uuidgen |tr "0-9-" "a-z"|cut -c 1-10` 23 File_Name2=`pwgen -1A0 10` 24 touch ${File_Name2}_clsn.html 25 done
脚本执行结果
[root@clsn for]# ls -l /clsn -rw-r--r-- 1 root root 0 12月 8 19:41 aegheiriek_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 aifohxique_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 caexahween_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 ciefaivaib_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 eixongooph_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 foozaivedo_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 ireeteethu_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 ohmeebivae_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 oiceehahth_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 sheewaehoo_clsn.html
1.2.2 【练习题2】批量改名特殊案例
【练习题1】中结果文件名中的clsn字符串全部改成znix(最好用for循环实现),并且将扩展名html全部改成大写。
jpvirsnjld_clsn.html ===> jpvirsnjld_znix.HTML
脚本内容:
1 [root@clsn for2]# cat rename_file.sh 2 #!/bin/bash 3 ############################################################# 4 # File Name: rename_file.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-08 11:31:56 9 # Description: 10 ############################################################# 11 12 cd /clsn &&\ 13 File_name=`ls |sed -r 's#(.*)_clsn.html#\1#g'` 14 15 for i in $File_name 16 do 17 if [ -f ${i}_clsn.html ] 18 then 19 mv ${i}_clsn.html ${i}_znix.HTML 20 else 21 echo "文件修改完成." 22 exit 23 fi 24 done
查看结果
[root@clsn for2]# ls /clsn/ aeyaesughi_znix.HTML caireasipi_znix.HTML uahahnieli_znix.HTML aifaepheeb_znix.HTML eathixoong_znix.HTML zalipageen_znix.HTML akuipheeye_znix.HTML ietoothaik_znix.HTML apheikieno_znix.HTML lachohtaif_znix.HTML
1.2.2.1 批量改名其他方式
rename 方式(最方便,专业改名)
rename txt jpg *
非 for 循环方式批量改名(使用sed命令进行拼接,然后交给bash执行)
ls *jpg|sed -r 's#(.*).jpg#mv & \1.mp4#'|bash
1.2.3 【练习题3】批量创建特殊要求用户案例
批量创建10个系统帐号clsn01-clsn10并设置密码(密码为随机数,要求字符和数字等混合)。
脚本内容:
1 [root@clsn for2]# cat add_user.sh 2 #!/bin/bash 3 ############################################################# 4 # File Name: add_user.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-08 11:52:21 9 # Description: 10 ############################################################# 11 12 Passwd_File=/tmp/`uuidgen`.txt 13 >$Passwd_File 14 chmod 400 $Passwd_File 15 16 for i in clsn{ 01..10} 17 do 18 userdel -r "$i" &>/dev/null 19 id $i &>/dev/null 20 if [ $? -ne 0 ] 21 then 22 useradd $i 23 PassWd=`uuidgen` 24 echo $PassWd |passwd --stdin $i &>/dev/null 25 echo "用户名:$i 密码:$PassWd" >>$Passwd_File 26 echo -e "\033[32m $i 用户创建成功!\033[0m" 27 else 28 echo "$i 用户已存在" 29 fi 30 if [ "$i" == "clsn10" ] 31 then 32 echo "用户密码请查看文件 $Passwd_File" 33 fi 34 done
查看成的密码文件
[root@clsn for2]# cat /tmp/3e5c18d9-f878-4d06-931e-5bbcc810c3dc.txt 用户名:clsn01 密码:3d4644d0-9cf4-49db-8928-1a8346972c32 用户名:clsn02 密码:70798c3a-c8e3-42a0-9942-d4011ce4b4b3 用户名:clsn03 密码:db2a0f1d-2e49-44f5-a5b2-69b352b30120 用户名:clsn04 密码:62d2e0c6-b755-4b00-ad2d-c98f9ca9f258 用户名:clsn05 密码:eaa3471b-d04f-4d7c-8b7e-3d75172a483b 用户名:clsn06 密码:f