当前位置: 首页 > news >正文

day59-Shell编程(第五部分)

1.每日复盘与今日内容

1.1复盘

  • case语句
  • for循环

1.2今日内容

  • for循环
  • exit、break、continue
  • while循环
  • 函数🍟🍟🍟🍟🍟
  • 数组🍟🍟🍟🍟🍟
  • 经典案例

2.for循环案例

#先安装 yum -y install expect
[root@shell ~]# mkpasswd 
q5ay~YNj8
[root@shell ~]# mkpasswd 
eUc2Mc@w0
[root@shell ~]# mkpasswd 
/K0eRmd4c案例1: 给每个用户设置一个随机密码,并且保留密码
[root@shell day05]# cat user.sh 
#!/bin/bash
for i in {1..3}
douser=oldboy$iuseradd $user pass=`mkpasswd`echo $pass|passwd --stdin $user#将用户写入文件中echo -e "$user\t$pass" >> user.txt
done

3.循环控制语句

1.exit     退出整个脚本
2.break    跳出循环、忽略循环内剩余代码、继续向下执行 break n  # n为数字 跳出的层数
3.continue 忽略本次剩余代码、从头开始执行[root@shell day05]# cat user.sh 
#!/bin/bash
for i in {1..3}	  # 需求oldboy1 oldboy2  oldboy3      目前已经创建oldboy2
douser=oldboy$iid $user &>/dev/nullif [ $? -eq 0 ];thencontinue			# 忽略oldboy2继续进入第三次循环 创建oldboy3elseuseradd $user 		fi
done
echo hehe..............

4.while循环

while [ 条件表达式 ]  # 如果成立则执行、不成立不执行
do可执行命令
done#死循环
[root@shell day05]# cat while.sh
#!/bin/bash
while true
doecho hehe.....sleep 1
done#死循环
#!/bin/bash
while true 或者 [ -f /etc/hosts ] 或者 [ 10 -eq 10 ]
doecho hehe.....sleep 1
done案例1.从1加到100  笔试题
[root@shell day05]# cat while.sh
#!/bin/bash
for i in `seq 100`
dosum=$[$sum+$i]
done
echo $sum
[root@shell day05]# sh while.sh
5050运算过程
#!/bin/bash
for i in `seq 100`
dosum=$[$sum+$i]i=1sum=0+1sum=1i=2sum=1+2sum=3i=3sum=3+3sum=6done
echo $sum#while从1加到100
[root@shell day05]# cat while.sh
#!/bin/bash
i=1
while [ $i -le 100 ]
dosum=$[$sum+$i]let i++
doneecho $sum
[root@shell day05]# sh while.sh
5050#while执行过程
[root@shell day05]# vim while.sh 
#!/bin/bash
i=1
while [ $i -le 100 ]
dosum=$[$sum+$i]sum=0+1sum=1let i++i=2sum=1+2sum=3done  [root@shell day05]# echo {1..100}|sed 's/ /+/g'|bc
5050[root@shell day05]# seq  -s + 100
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[root@shell day05]# seq  -s + 100|bc
5050案例2.while读取文件、一行一行的读取
for循环按照空格读取文件
[root@shell day05]# cat for.sh
#!/bin/bash
for i in `cat /etc/hosts`
doecho $i
done
[root@shell day05]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@shell day05]# sh for.sh 
127.0.0.1
localhost
localhost.localdomain
localhost4
localhost4.localdomain4
::1
localhost
localhost.localdomain
localhost6
localhost6.localdomain6while循环读取文件的方式
[root@shell day05]# cat while.sh
#!/bin/bash
while read line
doecho $line
done</etc/hosts[root@shell day05]# sh while.sh 
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6案例3.使用while将用户和对应的密码创建出来
[root@shell day05]# cat a.txt 
zs  test123.com
ls  oldboy123.com[root@shell day05]# cat while.sh 
#!/bin/bash
while read line
douser=`echo $line|awk '{print $1}'`pass=`echo $line|awk '{print $2}'`useradd $userecho $pass|passwd --stdin $user
done<a.txt

5.函数

1.函数完成特点功能代码块
2.函数和变量类似可以复用
3.函数必须先定义、然后在调用、定义不调用不执行。1.函数的定义方式
[root@shell day05]# cat fun.sh
#!/bin/bash
fun1(){					    # 常用echo "函数第一种定义方式"
}function fun2(){echo "函数第二种定义方式"
}function fun3 {echo "函数第三种定义方式"
}fun1
fun2
fun3
[root@shell day05]# sh fun.sh
函数第一种定义方式
函数第二种定义方式
函数第三种定义方式2.函数的传参
#在函数中无法直接接收脚本的传参
第一种传参方式、在调用函数的后面传参
[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $1 ];thenecho "$1 文件存在"elseecho "$1 文件不存在"fi
}
fun /etc/hosts
[root@shell day05]# sh fun.sh
/etc/hosts 文件存在[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $2 ];thenecho "$2 文件存在"elseecho "$2 文件不存在"fi
}
fun /etc/hosts /etc/passwd
[root@shell day05]# sh fun.sh
/etc/passwd 文件存在[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $1 ];thenecho "$1 文件存在"elseecho "$1 文件不存在"fi
}
fun $1
[root@shell day05]# sh fun.sh /etc/hostssss
/etc/hostssss 文件不存在
[root@shell day05]# sh fun.sh /etc/hosts
/etc/hosts 文件存在[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $1 ];thenecho "$1 文件存在"elseecho "$1 文件不存在"fi
}
fun $2 $1
[root@shell day05]# sh fun.sh /etc/hosts /etc/passwd
/etc/passwd 文件存在[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $2 ];thenecho "$2 文件存在"elseecho "$2 文件不存在"fi
}
fun $2  $1
[root@shell day05]# sh fun.sh /etc/hosts /etc/passwd
/etc/hosts 文件存在第二种传参方式、直接通过赋值传参方式
[root@shell day05]# cat fun.sh
#!/bin/bash
file=$1
fun(){if [ -f $file ];thenecho "$file 文件存在"elseecho "$file 文件不存在"fi
}
fun
[root@shell day05]# sh fun.sh /etc/hosts
/etc/hosts 文件存在
[root@shell day05]# sh fun.sh /etc/hostsssss
/etc/hostsssss 文件不存在3.函数的变量
[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){file=/etc/hosts
}
fun
echo $file
[root@shell day05]# sh fun.sh
/etc/hosts[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){file=/etc/hosts
}
echo $file			# 在未调用函数前无法调用函数中的变量、但是可以调用函数外的变量
fun
[root@shell day05]# sh fun.sh#函数变量注意
[root@shell day05]# cat fun.sh
#!/bin/bash
fun1(){name=oldboy
}
fun2(){name=oldgirl
}
fun1
fun2
echo $name
[root@shell day05]# sh fun.sh
oldgirl#local定义只在函数体内生效
[root@shell day05]# cat fun.sh
#!/bin/bash
name=hehe
fun1(){local  name=oldboyecho $name
}
fun2(){local name=oldgirlecho $name
}
fun1
fun2
echo $name
[root@shell day05]# sh fun.sh
oldboy
oldgirl
hehe4.函数的返回值
[root@shell day05]# cat fun.sh 
#!/bin/bash
fun(){if [ -f $1 ];thenexit 50elseexit 100fi
}
fun $1				# 执行完成后 不管文件存在还是不存在都会退出整个脚本、不会继续向下
if [ $? -eq 50 ];thenecho "$1 存在"
elseecho "$1 不存在"
fi[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $1 ];thenreturn 50elsereturn 100fi
}
fun $1
if [ $? -eq 50 ];thenecho "$1 存在"
elseecho "$1 不存在"
fi
[root@shell day05]# sh fun.sh /etc/hosts
/etc/hosts 存在
[root@shell day05]# sh fun.sh /etc/hostssssssssss
/etc/hostssssssssss 不存在[root@shell day05]# cat fun.sh 
#!/bin/bash
fun(){if [ -f $1 ];thenreturn 50elsereturn 100fi
}
fun $1
re=$?
[ $re -eq 50 ] && echo $1 文件存在
[ $re -eq 100 ] && echo $1 文件不存在#注意返回值的判断方法
[root@shell day05]# cat fun.sh
#!/bin/bash
fun(){if [ -f $1 ];thenreturn 50elsereturn 100fi
}
fun $1
if [ $? -eq 50 ];then			# 不正确的判断方法echo 存在
elif [ $? -eq 100 ];thenecho 不存在
fi#re=$?		 					# 正确的判断方法
#[ $re -eq 50 ] && echo $1 文件存在
#[ $re -eq 100 ] && echo $1 文件不存在

6.数组

变量: 一个名称对应一个值  类似一个箱子里只能装一个水果
A箱子=苹果
B箱子=香蕉
数组: 一个名称对应多个值  类似一个箱子中装了很多小盒子 然后小盒子中装了不同的水果
A箱子[盒子1]=苹果
A箱子[盒子2]=香蕉数组的分类:普通数组   只能以数字作为索引关联数组   也可以用字符串作为索引
语法:数组名称[索引]=值数组名称[下标]=值数组名称[元素名]=值第一种定义方法、使用索引名称定义
[root@shell day05]# array[1]=shell
[root@shell day05]# array[2]=mysql
[root@shell day05]# array[3]=docker查看数组
[root@shell day05]# array[1]=shell
[root@shell day05]# array[2]=mysql
[root@shell day05]# array[3]=docker
[root@shell day05]# echo $array#查看数组中指定的索引
[root@shell day05]# echo ${array[2]}
mysql
[root@shell day05]# echo ${array[3]}
docker
[root@shell day05]# echo ${array[1]}
shell
#查看数组中所有的值
[root@shell day05]# echo ${array[*]}
shell mysql docker
[root@shell day05]# echo ${array[@]}
shell mysql docker
#查看索引(箱子中小盒子的编号)
[root@shell day05]# echo ${!array[*]}
1 2 3
#查看当前定义的数组
[root@shell day05]# declare -a|grep array
declare -a array=([1]="shell" [2]="mysql" [3]="docker")
#使用unset取消变量或者数组的定义
[root@shell day05]# echo $name
oldboy
[root@shell day05]# unset name
[root@shell day05]# echo $name第二种定义方法、直接定义
[root@shell day05]# array=(10.0.0.1 10.0.0.2 10.0.0.7 10.0.0.8 10.0.0.71)
[root@shell day05]# echo ${array[*]}
10.0.0.1 10.0.0.2 10.0.0.7 10.0.0.8 10.0.0.71
[root@shell day05]# echo ${!array[*]}
0 1 2 3 4第三种定义方式、混合定义
[root@shell day05]# unset array
[root@shell day05]# array=([5]=shell mysql [10]=nginx docker)
[root@shell day05]# echo ${array[*]}
shell mysql nginx docker
[root@shell day05]# echo ${!array[*]}
5 6 10 11第四种定义方式、支持命令
[root@shell day05]# unset array
[root@shell day05]# array=(`ls`)
[root@shell day05]# ls
a.txt  for.sh  fun.sh  pass.sh  test.sh  user.sh  user.txt  while.sh
[root@shell day05]# echo ${array[*]}
a.txt for.sh fun.sh pass.sh test.sh user.sh user.txt while.sh案例.判断IP地址是否在线
10.0.0.1
10.0.0.2
10.0.0.7
10.0.0.8
10.0.0.71
www.baidu.com
www.hehehehehehe.com[root@shell day05]# cat ping.sh
#!/bin/bash
#定义数组
ip=(
10.0.0.1
10.0.0.2
10.0.0.7
10.0.0.8
10.0.0.71
www.baidu.com
www.hehehehehehe.com
)#数组遍历
for i in ${ip[*]}
doping -c1 -W1 $i &>/dev/nullif [ $? -eq 0 ];thenecho "$i 在线....."elseecho "$i 不在线....."fi
done[root@shell day05]# sh ping.sh
10.0.0.1 在线.....
10.0.0.2 在线.....
10.0.0.7 不在线.....
10.0.0.8 不在线.....
10.0.0.71 在线.....
www.baidu.com 在线.....
www.hehehehehehe.com 不在线.....第二种数组遍历方式
[root@shell day05]# cat ping.sh
#!/bin/bash
ip=(
10.0.0.1
10.0.0.2
10.0.0.7
10.0.0.8
10.0.0.71
www.baidu.com
www.hehehehehehe.com
)for i in ${!ip[*]} 
doping -c1 -W1 ${ip[$i]} &>/dev/nullif [ $? -eq 0 ];thenecho "${ip[$i]} 在线....."elseecho "${ip[$i]} 不在线....."fi
done
[root@shell day05]# sh ping.sh
10.0.0.1 在线.....
10.0.0.2 在线.....
10.0.0.7 不在线.....
10.0.0.8 不在线.....
10.0.0.71 在线.....
www.baidu.com 在线.....
www.hehehehehehe.com 不在线.....#关联数组、定义方式和普通数组相同唯一的区别是可以用字符串作为索引
#声明array是关联数组
[root@shell day05]# declare -A array
[root@shell day05]# array[index1]=shell
[root@shell day05]# array[index2]=docker
[root@shell day05]# array[index3]=nginx
[root@shell day05]# echo ${array[*]}
shell nginx docker
[root@shell day05]# echo ${!array[*]}
index1 index3 index2案例1.统计文件中的性别出现的次数#数组中运算自增的执行过程
#!/bin/bash
m
f
m
m
f
x
while read line
dodeclare -A sumlet sum[$line]++  let i++= i=i+1   sum[line]=line+1let sum[m]=m+1sum[m]=1let sum[f]=f++sum[f]=1let sum[m]=m+1sum[m]=2done<s.txt
-----------------[root@shell day05]# cat sex.sh
#!/bin/bash
while read line
dodeclare -A sumlet sum[$line]++
done<s.txt#数组遍历
for i in ${!sum[*]}
doecho $i 出现了 ${sum[$i]} 次
done
[root@shell day05]# sh sex.sh
x 出现了 1 次
m 出现了 3 次
f 出现了 2 次#案例2.统计解释器的出现次数
[root@shell day05]# cat sex.sh 
#!/bin/bash
while read line
dodeclare -A sumlet sum[`echo $line|awk -F: '{print $NF}'`]++
done</etc/passwd#数组遍历
for i in ${!sum[*]}
doecho $i 出现了 ${sum[$i]} 次
done或者先将索引定义给一个变量
[root@shell day05]# cat sex.sh 
#!/bin/bash
while read line
dodeclare -A sumba=`echo $line|awk -F: '{print $NF}'`let sum[$ba]++
done</etc/passwd#数组遍历
for i in ${!sum[*]}
doecho $i 出现了 ${sum[$i]} 次
done

7.案例

案例1
1.抓阄 1-100之前抓阄
2.输入用户 zhnagsan 从1-100之间生成随机数
3.随机数不能重复
4.退出脚本最终进行排序
zhangsan 66
不能退出脚本继续抓阄
laowang  99
所有的用户输入完毕 结束抓阄
对数字进行虚拟排序
laowang 99
zhangsan 66
laoliu 55案例2.中午吃什么
中午可以吃的菜单是
1.包子
2.馒头
3.米饭
4.炒菜
5.汉堡
6.水饺
请稍后正在随机指定中午吃啥...
随机生成1-10之间的一个数字
如果生成的是5 则输出 中午你要吃的是汉堡案例3.双色球
红球1-33 之间随机生成6个数字  
篮球1-16 之间随机生成1个数字
完成后进行排序
请选择随机生成多少注: 
买多少钱的: 100
生成50注
02 03 05 15 20 33  4案例4.反向破解
$((RANDOM))  # 区间范围 1-32767
c953f543
7b0b5693
6d8a1bc0
0513d6f0
d4470cc
caae07f

8.今日总结

  • for循环
  • exit、break、continue
  • while循环
  • 函数🍟🍟🍟🍟🍟
  • 数组🍟🍟🍟🍟🍟
  • 经典案例

http://www.dtcms.com/a/553523.html

相关文章:

  • 网站建设客户需求分析调查表江苏国泰做的网站案例
  • VUE3+element plus el-table上下拖拽
  • 【模拟面试|豆包模拟面试-1 Java基础】
  • AI优化亚马逊广告:DeepBI智能化托管让广告运营从负担变轻松,ACOS优化至23%以下
  • 关键词解释:梯度消失(Vanishing Gradient)与 梯度爆炸(Exploding Gradient)
  • 天津网站建设如何最近火爆的新闻
  • Maven内核探秘:从启动到构建全流程
  • CNCC 2025|开源AI基础设施论坛成功举办
  • 开源可信MCP,AICC机密计算新升级!
  • 混元图像3.0开源原生多模态生图新篇章
  • 环境搭建与第一个程序:Hello, Rust!
  • [论文阅读] AI | 大语言模型服务系统服务级目标和系统级指标优化研究
  • 帝国网站管理系统视频教程asp网站开发
  • 自己做的网站申请软著物联网是干嘛的
  • 企业形象破局指南——缺乏专业线上展示?官网SEO优化重构品牌信任
  • webgl 变换矩阵:旋转、平移、缩放
  • 怎么做婚介网站襄阳php网站开发
  • 网站建设规划书案例济南做网站互联网公司有哪些
  • float为什么会丢失精度?
  • 网站产品后台界面怎么做微信朋友圈广告推广
  • 香港科技大学广州|可持续能源与环境学域博士招生宣讲会—吉林大学专场
  • LaTeX 重点表格文字对不齐(有些列文字和其他列差一行才显示)的原因和解决办法
  • 网站推广必做百度云打开的wordpress
  • soular零基础学习,如何统一管理TikLab帐号体系
  • kanass零基础学习:创建第一个项目
  • 【C语言实战(66)】筑牢防线:C语言安全编码之输入与错误处理
  • 【机器学习11】决策树进阶、随机森林、XGBoost、模型对比
  • 唯品会 一家专门做特卖的网站做振动盘的企业网站
  • 我的WordPress网站锦州网站建设市场
  • Spring Boot 3.3新特性全解析