码迷,mamicode.com
首页 > 系统相关 > 详细

linux编程基础(三)

时间:2018-04-17 22:59:36      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:linux 编程

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 "linux的blog:"

for i

do

echo "$i"

done   

脚本执行结果

[root@kai for]# sh for2.sh http://blog.george.comlinux的blog:

http://blog.george.com

 技术分享图片

③   类似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 = 0for i in 1...100{

x += i

}

print(x)


//

5050for _ 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循环在/kai目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串kai,名称示例如下:[root@znix C19]# ls /kai

apquvdpqbk_kai.html mpyogpsmwj_kai.html txynzwofgg_kai.html

bmqiwhfpgv_kai.html udrzobsprf_kai.html vjxmlflawa_kai.html

jhjdcjnjxc_kai.html qeztkkmewn_kai.html jpvirsnjld_kai.html

ruscyxwxai_kai.html

脚本内容

方法一:

生成全字母随机数的方法:uuidgen|tr '0-9-' 'a-z'

[ -d /kai ] || mkdir -p /kai

cd /kai && \

for i in {1..10}  

do    

suiji=`uuidgen|tr '0-9-' 'a-z'|cut -c 1-10`    

touch ${suiji}_kai.html

done


方法二:

12 [ -d /kai ] || mkdir -p /kai

13 rpm -qa |grep pwgen &>/dev/null14 if [ $? -eq 1 ]

15 then

16 yum install pwgen -y &>/dev/null17 fi18

19 cd /kai &&\

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}_kai.html

25 done

脚本执行结果

[root@kai for]# ls -l /kai

-rw-r--r-- 1 root root 0 12月 8 19:41 aegheiriek_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 aifohxique_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 caexahween_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 ciefaivaib_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 eixongooph_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 foozaivedo_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 ireeteethu_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 ohmeebivae_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 oiceehahth_kai.html

-rw-r--r-- 1 root root 0 12月 8 19:41 sheewaehoo_kai.html

1.2.2 【练习题2】批量改名特殊案例

【练习题1】中结果文件名中的kai字符串全部改成znix(最好用for循环实现),并且将扩展名html全部改成大写。

jpvirsnjld_kai.html ===> jpvirsnjld_znix.HTML

脚本内容:技术分享图片

1 [root@kai for2]# cat rename_file.sh

2 #!/bin/bash

3 #############################################################

4 # File Name: rename_file.sh 

5 # Version: V1.0 

6 # Author: kai

7 # Organization: http://blog.george.com 

8 # Created Time : 2017-12-08 11:31:56 

9 # Description:

10 #############################################################

11

12 cd /kai &&\

13 File_name=`ls |sed -r 's#(.*)_kai.html#\1#g'`

14

15 for i in $File_name

16 do

17 if [ -f ${i}_kai.html ]

18 then

19 mv ${i}_kai.html ${i}_znix.HTML

20 else

21 echo "文件修改完成."

22 exit

23 fi

24 done


方法二:

切随机字符串:echo ls |cut -c 1-10

查看结果

[root@kai for2]# ls /kai/

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 *
rename html html-$(date +%F) *

#批量改文件名  

非 for 循环方式批量改名(使用sed命令进行拼接,然后交给bash执行)

ls *jpg|sed -r 's#(.*).jpg#mv & \1.mp4#'|bash

1.2.3 【练习题3】批量创建特殊要求用户案例

  批量创建10个系统帐号kai01-kai10并设置密码(密码为随机数,要求字符和数字等混合)。

脚本内容:技术分享图片

1 [root@kai for2]# cat add_user.sh

2 #!/bin/bash

3 #############################################################

4 # File Name: add_user.sh 5 # Version: V1.0 6 # Author: kai

7 # Organization: http://blog.george.com 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 kai{01..10}

17 do

18 userdel -r "$i" &>/dev/null

19 id $i &>/dev/null

20 if [ $? -ne 0 ]

21 then22 useradd $i

23 PassWd=`uuidgen`

24 echo $PassWd |passwd --stdin $i &>/dev/null25 echo "用户名:$i 密码:$PassWd" >>$Passwd_File

26 echo -e "\033[32m $i 用户创建成功!\033[0m"27 else28 echo "$i 用户已存在"29 fi30 if [ "$i" == "kai10" ]

31 then32 echo "用户密码请查看文件 $Passwd_File"33 fi34 done

查看成的密码文件

[root@kai for2]# cat /tmp/3e5c18d9-f878-4d06-931e-

5bbcc810c3dc.txt

用户名:kai01 密码:3d4644d0-9cf4-49db-8928-1a8346972c32

用户名:kai02 密码:70798c3a-c8e3-42a0-9942-d4011ce4b4b3

用户名:kai03 密码:db2a0f1d-2e49-44f5-a5b2-69b352b30120

用户名:kai04 密码:62d2e0c6-b755-4b00-ad2d-c98f9ca9f258

用户名:kai05 密码:eaa3471b-d04f-4d7c-8b7e-3d75172a483b

用户名:kai06 密码:fb260a11-cd47-4b97-ab49-0cae7a755262

用户名:kai07 密码:16ee7a1f-8aac-4537-b1aa-7fc75beb8754

用户名:kai08 密码:0dde8823-b97d-4c88-9258-3a68a3b53eba

用户名:kai09 密码:daf14ec4-ba9f-4593-9773-1557fdf605dc

用户名:kai10 密码:6f1b452c-00b2-44a1-9f43-5f658d3a9124

注:修改字符集:localerctl set-locale.utf8


方法二:技术分享图片

技术分享图片

1.2.3.1  批量创建用户并设置随机密码(不使用shell循环)

  方法一

echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& echo 

\1 >>/tmp/passwd.txt \&\& echo $RANDOM |md5sum |cut -c 1-

5>>/tmp/passwd.txt \&\& echo `tail -1 /tmp/passwd.txt`|passwd 

--stdin \1#g'|bash  

方法二

echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& 

pass=`echo $RANDOM |md5sum |cut -c 1-5` \&\& echo $pass 

|passwd --stdin \1 \&\& echo \1 

$pass>>/tmp/user_passwd.txt#g'|bash  

方法三

echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& 

pass=`echo $RANDOM |md5sum |cut -c 1-5` \&\& echo 

\1:$pass>>/tmp/user_passwd.txt \&\& 

chpasswd</tmp/user_passwd.txt#g'|bash

1.2.4 【练习题4】扫描网络内存活主机案例

  写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?

脚本内容:技术分享图片

1 [root@kai for]# cat scan_ip2.sh

2 #!/bin/bash

3 #############################################################

4 # File Name: scan_ip.sh 

5 # Version: V1.0 

6 # Author: kai

7 # Organization: http://blog.george.com 

8 # Created Time : 2017-12-07 21:58:47 

9 # Description:

10 #############################################################

11

12 Ip_File=/tmp/scan_ip.txt

13 >$Ip_File

14

15 for i in 10.0.0.{1..254}

16 do

17 ping -c 1 -W 1 $i &>/dev/null && \

18 if [ $? -eq 0 ] ;then

19 echo "存活主机: $i" &>>$Ip_File

20 fi &

21 done

22 echo "使用 cat $Ip_File 查看扫描结果"

脚本执行结果

[root@kai for]# time sh scan_ip2.sh

使用 cat /tmp/scan_ip.txt 查看扫描结果


real 0m0.290s

user 0m0.001

ssys 0m0.039s

[root@kai for]# cat /tmp/scan_ip.txt

存活主机: 10.0.0.180

存活主机: 10.0.0.254

方法二:nmap(推荐使用)

snmp -sT 10.0.0.254    #扫描10.0.0.254网段存活的主机及打开的端口

方法三:shell并发(不建议用,消耗系统资源很大)

{ ... } &:将{ }中的所有命令都放到后台

for i in 10.0.0.{1..254}  

do    

{ ping -c 1 -W 1 $i &>/dev/null    

if [ $? -eq 0 ];then        

echo "survavil_host: $i"        

sleep 10
    fi } &

done


小伙伴们可以关注我的微信公众号:linux运维菜鸟之旅

技术分享图片

关注“中国电信天津网厅”公众号,首次绑定可免费领2G流量,为你的学习提供流量! 

技术分享图片



linux编程基础(三)

标签:linux 编程

原文地址:http://blog.51cto.com/13055758/2104619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!