day60-day-shell-while
while
1.while本质上就是循环
只不过while支持条件测试语句
整数比对
字符串比对
正则比对
文件比对
2.while读入文件的方式比较的特殊
while读入文件按行读取 IFS指定分隔符
for读入文件按空格读取 IFS指定分隔符
while read file do echo $file done < /etc/passwd
循环中的控制语句 ----> for while
exit
break
continue
1.exit,退出整个程序。当脚本碰到exit时,直接退出,剩余不管有多少代码都不执行。
2.break,结束当前循环,但会执行循环之后的所有的代码。
3.continue 忽略本次循环剩余的所有代码,直接进行下一次循环,直到循环结束,然后继续循环之后的代码。
需求:循环嵌套continue,打印1-9当数值为5则跳过本次循环,继续下一次循环。请分别使用for和while实现。
需求2:循环嵌套break,打印1-9当数值为5则停止。请分别使用for和while实现。
需求1: 使用while读入文件方式,批量创建用户
[root@manager while]# cat while-12.sh #!/bin/bash # Author: Oldux.com QQ: 552408925 # Date: 2019-11-01 # FileName: while-12.sh # URL: https://www.xuliangwei.com # Description: while read line do id $line &>/dev/null if [ $? -eq 0 ];then continue else useradd $line fi done < user.txt
需求2: 使用while读入文件方式,批量创建用户以及密码 文件格式: username:password
[root@manager while]# cat while-13.sh #!/bin/bash # Author: Oldux.com QQ: 552408925 # Date: 2019-11-01 # FileName: while-13.sh # URL: https://www.xuliangwei.com # Description: while read line do user=$(echo $line | awk -F ":" '{print $1}') pass=$(echo $line | awk -F ":" '{print $2}') id $user &>/dev/null if [ $? -eq 0 ];then continue else useradd $user echo "$pass" | passwd --stdin $user fi done <user.txt
需求3: 猜数字游戏
1)随机输出一个1-100的数字 echo
RANDOM%100+1))
2)要求用户输入的必须是数字(数字处加判断)
3)如果比随机数小则提示比随机数小了 大则提示比随机数大了
4)正确则退出 错误则继续死循环
5)最后统计猜了多少次(猜对了多少次,失败多少次)
[root@manager while]# cat while-14.sh #!/bin/bash # Author: Oldux.com QQ: 552408925 # Date: 2019-11-01 # FileName: while-14.sh # URL: https://www.xuliangwei.com # Description: SJ=$(($RANDOM%100+1)) i=1 while true do read -p "请输入你要猜的数: " Action if [ $Action -eq $SJ ];then echo "恭喜你 gdx...." break elif [ $Action -lt $SJ ];then echo "太小了 gdx...." else echo "太大了 gdx...." fi let i++ done echo "你总共猜了 $i 次, 失败了 $(( $i-1 )) 次"
需求4: 在一个2000多行数据文件,然后有10个新的文件,1-5行数据放到第一个文件里,6-10行数据 放到第二个文件里...46-50数据放到第10个文件里。然后51-55数据在放到第一个文件里。如何实现?【可忽略】
[root@manager while]# cat while-15.sh #!/bin/bash # Author: Oldux.com QQ: 552408925 # Date: 2019-11-01 # FileName: while-15.sh # URL: https://www.xuliangwei.com # Description: while true do for i in $(seq 10) do head -5 file.txt >> file_$i.txt sed -i '1,5d' file.txt if [ ! -s file.txt ];then exit fi done done
#!/bin/bash a=0 b=0 while [ $b -lt 2000 ] do file=num.txt while [ $a -lt 10 ] do a=$[$a+1] b=$[$b+5] echo "$a $b" line=$(awk "NR==$[$b-4],NR==$b" $file) echo "$line">>${a}.txt done a=0 done
1.按照时间生成文件
2018-05-22.log将每天的磁盘使用状态写入到对应日期的文件
2.统计Nginx日志中每个IP的访问量有多少,日志如下:
192.168.56.1 - - [21/May/2018:20:44:06 -0400] "GET /index.html HTTP/1.0" 404 169 "-" "ApacheBench/2.3" "-"/code/index.html
3.写一个脚本计算一下Linux系统所有进程占用内存大小的和。
4.在/backup下创建10个.txt的文件,找到/backup目录下所有后缀名为.txt的文件
[root@manager-61 /work]#vim work4.sh 1 #!/bin/bash 2 #********************* 3 #Author: 流星花雨 4 #QQ: 1679338722 5 #Date: 2019-11-02 6 #FileName: work4.sh 7 #URL: https://www.leitiancheng.cn 8 #Description: The test script 9 #********************* 10 11 while [ $i -le 10 ] 12 do 13 touch /backup/$i.txt 14 #rm -rf /backup/$i.txt 15 let i++ 16 17 done
1.批量修改txt为txt.bak
file=$(find /backup/ -type f -name "*.txt") for i in $file do #echo $i mv $i $i.bak done
2.把所有的.bak文件打包压缩为123.tar.gz
cd /backup/ && tar czf 123.tar.gz ./*.bak rm -rf /backup/*.bak 3.批量还原文件的名字,及把增加的.bak再删除 tar xf /backup/123.tar.gz -C /backup/ Find=$(find /backup/ -type f -name "*.bak") for l in $Find do mv $l ${l%.*} done
5.把一个文本文档的前五行中包含字母的行删除掉,同时把6到10行中的全部字母删除掉
nu=$(wc -l file.txt |awk '{print $1}') for i in $(seq 1 5) do l=$(sed -n ${i}p file.txt) if echo $l |grep -Evq [a-Z];then echo $l fi done for i in $(seq 6 10) do k=$(sed -n ${i}p file.txt) echo $k|sed 's#[a-Z]##g' done for i in $(seq 11 $nu) do sed -n ${i}p file.txt done
7.写个shell,看看你的linux系统中是否有自定义用户(普通用户),若是有,一共有几个?
[root@manager-61 /work]#cat work6.sh #!/bin/bash #********************* #Author: 流星花雨 #QQ: 1679338722 #Date: 2019-11-02 #FileName: work6.sh #URL: https://www.leitiancheng.cn #Description: The test script #********************* Uid=$(awk -F ":" '{print $3}' /etc/passwd) for i in $Uid do if [ $i -ge 1000 ];then echo $i >>user.txt fi done nu=$(wc -l user.txt| awk '{print $1}') echo $nu rm -rf user.txt
8.写一个shell脚本来看看你使用最多的命令是哪些,列出你最常用的命令top10
history |awk '{print $2}'|sort|uniq -c|sort -nr|head
9.中企动力面试题
用shell处理以下内容
1、按单词出现频率降序排序!
declare -A array_txt txt="the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation" txt=$(echo $txt |sed 's#[.,]# #g') for i in $txt do let array_txt[$i]++ done #循环数组 for j in ${!array_txt[@]} do echo 次数是: "${array_txt[$j]}" 索引是: "$j"|sort -t ":" -k2|sort -nr done
2、按字母出现频率降序排序!
the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation
declare -A array_txt te () { txt="the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation" txt=$(echo $txt |grep -o [a-Z]) for i in $txt do let array_txt[$i]++ done #循环数组 for j in ${!array_txt[@]} do echo 次数是: "${array_txt[$j]}" 索引是: "$j"|sort -t ":" -k2 done } te|sort -t ":" -k2 -nr
10.写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些
for i in $(seq 254) do { ping -c1 10.0.0.$i &>/dev/null if [ $? -eq 0 ];then echo 10.0.0.$i 存活 fi }& done wait
函数、
需求3:使用case实现系统管理工具箱。
Command action
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出程序
cat << -EOF h 显示命令帮助 f 显示磁盘分区 d 显示磁盘挂载 m 查看内存使用 u 查看系统负载 q 退出程序 -EOF read -p "请输入相应的指令: [ h | f | d | m | u | q ]" i case $i in h) clear man cat ;; f) clear fdisk -l ;; d) clear df -h ;; m) clear free -m ;; u) clear top ;; q) exit ;; *) echo 回车好玩吗 esac
需求3:使用case、循环、函数、实现JumpServer跳板机功能。
1.用户登陆该服务器则自动执行该脚本。
2.脚本提示可连接主机列表。
3.该脚本无法直接退出。
5.case场景示例,实现多级菜单功能。
[root@manager-61 /work]#cat work12.sh #!/bin/bash #********************* #Author: 流星花雨 #QQ: 1679338722 #Date: 2019-11-03 #FileName: work12.sh #URL: https://www.leitiancheng.cn #Description: The test script #********************* while true do cat << -EOF 1 nginx 2 php 3 httpd 4 nfsd 5 vsftpd 6 quit -EOF read -p "请输入要操作的服务 [ 1 | 2 | 3 | 4 | 5 | 6 ]" server case $server in 1|nginx|Nginx|NGINX) clear cat << EOF --------------- 1 nginx-1.14 2 nginx-1.12 3 nginx-1.10 4 返回上一层 -------------- EOF read -p "请输入你要安装的大版本号 [ 1 | 2 | 3 | 4 ]" b case $b in 1) echo nginx-1.14 已安装 ;; 2) echo nginx-1.12已安装 ;; 3) echo nginx-10已安装 ;; 4) #cat << -EOF #1 nginx #2 php #3 httpd #4 nfsd #5 vsftpd #6 quit -EOF #read -p "请输入要操作的服务 [ 1 | 2 | 3 | 4 | 5 | 6 ]" server continue ;; *) exit esac ;; 2|php|Php|PHP) cat <<EOF 1 php-7.1 2 php-7.2 3 返回上一级 EOF read -p "请输入要安装的版本 [ 1 | 2 | 3 ]" c case $c in 1) echo php-7.1已安装 ;; 2) echo php-7.2已安装 ;; 3) continue ;; *) eixt esac ;; 3|httpd|Httpd|HTTPD) cat << EOF 1 http-1.0 2 http-2.0 3 返回上一级 EOF read -p "请输入版本号 [ 1 | 2 | 3 ]" d case $d in 1) echo php-1.0已安装 ;; 2) echo php-2.0已安装 ;; 3) continue ;; *) exit esac ;; *) exit esac done
- 点赞
- 收藏
- 分享
- 文章举报
- Shell笔记7——while和until循环的应用实践
- shell 关于 while 循环中赋值的问题
- Shell学习(while,break,continue,exit)-2018.12.26
- bash shell while语法
- shell脚本 for循环 while循环 case分支结构
- shell之 while read a b的意思
- bash shell while语法
- 转载]Linux 编写shell脚本(2)if for while
- shell脚本之循环语句for,while,until用法的详解
- shell编程之case语句、while以及until循环
- linux 秒级别定时任务 crond shell while
- shell中while+for的使用方法
- shell脚本--if,for,while循环
- Linux之shell编程条件判断-if,while,for,case
- Spark-shell启动的时候报Error while instantiating ‘org.apache.spark.sql.hive.HiveSessionStateBuilder’错误
- tar ,cpio打包解压. shell脚本for,while,until循环. rpm包管理,select循环菜单,函数function,yum
- SHELL脚本语句 if、for、while、case 图示
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select .
- shell-3:while循环中break和continue的用法
- linux自学_shell脚本while详解