脚本基础3
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.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
脚本内容
[root@clsn for]# cat make_file.sh
[ -d /clsn ] || mkdir -p /clsn
rpm -qa |grep pwgen &>/dev/null
if [ $? -eq 1 ]
then
yum install pwgen -y &>/dev/null
fi
cd /clsn &&\
for i in {1..10}
do
#File_Name=`uuidgen |tr "0-9-" "a-z"|cut -c 1-10`
File_Name2=`pwgen -1A0 10`
touch ${File_Name2}_clsn.html
done
View Code 批量生成随机字符文件名
脚本执行结果
[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.3 while循环语句
在编程语言中,while循环(英语:while loop)是一种控制流程的陈述。利用一个返回结果为布林值(Boolean)的表达式作为循环条件,当这个表达式的返回值为“真”(true)时,则反复执行循环体内的程式码;若表达式的返回值为“假”(false),则不再执行循环体内的代码,继续执行循环体下面的代码。
因为while循环在区块内代码被执行之前,先检查陈述是否成立,因此这种控制流程通常被称为是一种前测试循环(pre-test loop)。相对而言do while循环,是在循环区块执行结束之后,再去检查陈述是否成立,被称为是后测试循环。
1.3.1 shell中while语法
while 条件
do
命令
done
1.3.2 while 使用场景
多用于创建守护进程
【示例1】:while实现web服务器搭建
脚本代码
[root@clsn scripts]# vim web_view.sh
#!/bin/bash
while true
do
echo "ok" | nc -l 81
done
客户端进行访问测试
[root@clsn html]# curl 10.0.0.180:81
ok
服务端显示结果:
[root@clsn scripts]# sh web_view.sh
GET / HTTP/1.1
User-Agent: curl/7.29.0
Host: 10.0.0.180:81
Accept: */*
1.3.3 while 作用
补充定时任务功能,执行小于1秒的定时任务
循环执行某些操作,
1.5 break continue exit return
条件与循环控制及程序返回值命令表
命令 | 说明 |
---|---|
break n | 如果省略n,则表示跳出整个循环,n表示跳出循环的层数 |
continue n | 如果省略n,则表示跳过本次循环,忽略本次循环的剩余代码,进人循环的下一次循环。 n表示退到第n层继续循环 |
exit n | 退出当前Shell程序,n为上一次程序执行的状态返回值。n也可以省略,在下一个Shell里可通过"$?"接收exit n的n值 |
return n | 用于在函数里作为函数的返回值,以判断函数执行是否正确。在下一个Shell里可通过"$?"接收exit n的n值 |
简单来说即:
break 跳出循环
continue 跳出本次循环
exit 退出脚本
return 与 exit 相同,在函数中使用
1.5.1 break命令说明
[root@clsn scripts]# help break
break: break [n]
退出 for、while、或 until 循环
退出一个 FOR、WHILE 或 UNTIL 循环。如果指定了N,则打破N重
循环
退出状态:
退出状态为0除非 N 不大于或等于 1。
1.5.2 continue命令说明
[root@clsn scripts]# help continue
continue: continue [n]
继续 for、while、或 until 循环。
继续当前 FOR、WHILE 或 UNTIL 循环的下一步。
如果指定了 N, 则继续当前的第 N 重循环。
退出状态:
退出状态为 0 除非 N 不大于或等于1。
1.5.3 exit命令说明
[root@clsn scripts]# help exit
exit: exit [n]
退出shell。
以状态 N 退出 shell。 如果 N 被省略,则退出状态
为最后一个执行的命令的退出状态。
1.5.4 return命令说明
[root@clsn tuichu]# help return
return: return [n]
从一个 shell 函数返回。
使一个函数或者被引用的脚本以指定的返回值 N 退出。
如果 N 被省略,则返回状态就是
函数或脚本中的最后一个执行的命令的状态。
退出状态:
返回 N,或者如果 shell 不在执行一个函数或引用脚本时,失败。
1.6 shell中的数组
1.6.1 为什么会产生Shell数组
通常在开发Shell脚本时,定义变量采用的形式为“a=l;b=2;C=3”,可如果有多个 变量呢?这时再逐个地定义就会很费劲,并且要是有多个不确定的变量内容,也会难以 进行变量定义,此外,快速读取不同变量的值也是一件很痛苦的事情,于是数组就诞生 了,它就是为了解决上述问题而出现的。
1.6.2 什么是Shell数组
Shell的数组就是一个元素集合,它把有限个元素(变量或字符内容)用一个名字来 命名,然后用编号对它们进行区分。这个名字就称为数组名,用于区分不同内容的编 号就称为数组下标。组成数组的各个元素(变量)称为数组的元素,有时也称为下标变量。
1.6.3 数组中的增删改查
数组的定义
# 定义数组
[root@clsn scripts]# stu=(001 002 003)
# 打印数组
[root@clsn scripts]# echo ${stu[@]}
001 002 003
# 显示数组长度
[root@clsn scripts]# echo ${#stu}
3
查: 遍历数组的内容
#打印数组内容(通过数组下标或索引)
[root@clsn scripts]# echo ${stu[0]}
001
[root@clsn scripts]# echo ${stu[1]}
002
[root@clsn scripts]# echo ${stu[2]}
003
[root@clsn scripts]# echo ${stu[3]}
遍历数组
#方法一
[root@clsn scripts]# for i in ${stu[@]};do echo $i ;done
001
002
003
# 方法二
[root@clsn scripts]# array=(1 2 3 4 5)
[root@clsn scripts]# for i in `eval echo {1..${#array[@]}}`;do echo ${array[i-1]};done
1
2
3
4
5
增:数组添加
[root@clsn scripts]# stu[3]=004
[root@clsn scripts]# echo ${stu[@]}
001 002 003 004
改:数组修改
[root@clsn scripts]# stu[2]=000
[root@clsn scripts]# echo ${stu[@]}
001 002 000 004
删:数组删除
[root@clsn scripts]# unset stu[2]
[root@clsn scripts]# echo ${#stu[@]}
3
[root@clsn scripts]# echo ${stu[@]}
001 002 004
1.6.4 将命令的结果赋值给数组
dir=(`ls`)
dir=($(ls))
命令定义数组
[root@clsn scripts]# COM=(`ls`)
[root@clsn scripts]# echo ${COM[@]}
bianliang.sh case cfb.sh chanshu.sh check check_url.sh
clsn.sh clsn_test.sh daojishi.sh ddos_check.sh echo.sh for for2 fruits.sh
function fz.sh html jingdutiao.sh jishuanqi2.sh jishuanqi.sh lamp.sh lnmp.sh
memcache_check.sh menu.sh nginx.sh panduan panduan1 play quanju.sh rsync_check.sh
rsyncd system6 tuichu web_check.sh web_view.sh while xiugaizhuji.sh yhk.sh yunshuan.sh
zhuajiu.sh
1.6.1 数组定义格式
[root@clsn scripts]# a=(1 2 3 )
[root@clsn scripts]# b=(
> 2
> 3
> 4
> )
> [root@clsn scripts]# echo ${a[@]}
> 1 2 3
> [root@clsn scripts]# echo ${b[@]}
> 1 2 3 4
1.6.2 数组的本质就是一个变量,只是这个变量存了多个值
在shell 常用的功能是查
array=(valuel value2 value3 ...)
打印数组格式
${array[@]} 所有元素
${#array[@]} 数组长度
${array[i]} 单个元素,i是下标
1.7 Shell 函数
shell一个非常重要的特性是它可作为一种编程语言来使用。因为shell是一个解释器,所以它不能对为它编写的程序进行编译,而是在每次从磁盘加载这些程序时对它们进行解释。而程序的加载和解释都是非常耗时的。
针对此问题,许多shell(如BourneAgainShell)都包含shell函数,shell把这些函数放在内存中,这样每次需要执行它们时就不必再从磁盘读入。shell还以一种内部格式来存放这些函数,这样就不必耗费大量的时间来解释它们。
函数的作用就是把程序里多次调用相同代码的部分定义成一份,然后起个名字,所有的调用都 只用这名字就可以了,修改代码时,只需要改变函数体内的代码即可。
1.7.1 使用函数的优势
🐟 把相同的程序段定义成函数,可以减少代码量。
🐟 增加程序的可读、易读性
🐟 实现程序功能的模块化
1.7.2 定义函数
function clsn(){
echo "http://blog.znix.top"
}
znix(){
echo "http://www.znix.top "
}
说明:
1、可以带function clsn() 定义,也可以直接clsn() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)
3、执行函数就是将函数名放到定义的函数之后即可
将函数加载到当前窗口执行:
[root@clsn function]# . fun1.sh
[root@clsn function]# zn
znew znix
[root@clsn function]# znix
test
[root@clsn function]# clsn
http://blog.znix.top
1.7.3 引用函数
脚本内容
[root@clsn function]# cat fun2.sh
#!/bin/bash
Fun_File=/server/scripts/function/fun1.sh
[ -f $Fun_File ] && . $Fun_File
clsn
脚本执行结果
[root@clsn function]# sh fun2.sh
http://blog.znix.top
1.7.4 函数传参
脚本内容:
[root@clsn function]# cat fun3.sh
#!/bin/bash
function clsn(){
echo "Hi "
}
CLSN(){
echo "Hello "
echo $0
echo $1
echo $2
}
clsn
CLSN xi xi
脚本执行结果
[root@clsn function]# sh fun3.sh
Hi
Hello
fun3.sh
xi
xi
1.7.5 $0 永远的脚本名称
function clsn(){
echo "http://blog.znix.top $1 $2"
echo $0
}
znix(){
echo "test"
}
clsn $1 $2
执行结果
[root@clsn function]# sh fun1.sh
http://blog.znix.top
fun1.sh
1.7.6 函数中return的用法
脚本内容:
[root@clsn function]# cat fun3.sh
#!/bin/bash
function clsn(){
echo "Hi "
}
CLSN(){
echo "Hello "
echo $0
echo $1
echo $2
return 4
echo "xxixiixxiix"
}
clsn
CLSN xi xi
echo $?
脚本执行结果
[root@clsn function]# sh fun3.sh
Hi
Hello
fun3.sh
xi
xi
4
return
命令说明:
[root@clsn function]# help return
return: return [n]
从一个 shell 函数返回。
使一个函数或者被引用的脚本以指定的返回值 N 退出。
如果 N 被省略,则返回状态就是
函数或脚本中的最后一个执行的命令的状态。
退出状态:
返回 N,或者如果 shell 不在执行一个函数或引用脚本时,失败。