shell脚本练习题->1

猜随机数的大小

描述:

写一个猜数字脚本,当用户输入的数字和预设数字(随机生成一个0-100的数字)一样时,直接退出,否则让用户一直输入:并且提示用户输入的数字比预设数字大或者小

分析:

1:随机数字是如何生成的

2:这是一个死循环,直到猜对了才能退出

3:需要判断大小

脚本实现:

[root@jumpserver-70 scripts]# cat num_random.sh 
#!/bin/bash

num=$(echo $(($RANDOM%100+1)))
i=0

while true
do
    read -p "请猜一下这个数是多少:" number
    let i++
    if [[ ! $number =~ ^[0-9]+$ ]];then
        echo "请检查输入的是否为数字"
    elif [ $number -gt $num ];then
        echo "你输入的数大了!"
    elif [ $number -lt $num ];then
        echo "你输入的数小了- -"
    else
        echo "恭喜你!都会抢答了。"
        echo "你一共猜了 $i 次"
        exit 1
    fi
done

实现的效果:

[root@jumpserver-70 scripts]# sh num_random.sh 
请猜一下这个数是多少:10
你输入的数小了- -
请猜一下这个数是多少:50
你输入的数小了- -
请猜一下这个数是多少:90
你输入的数大了!
请猜一下这个数是多少:80
你输入的数小了- -
请猜一下这个数是多少:85
你输入的数大了!
请猜一下这个数是多少:83
你输入的数小了- -
请猜一下这个数是多少:84
恭喜你!都会抢答了。
你一共猜了 7 次

文件中带随机数并修改

描述:

请写出:使用for循环在/opt目录下通过随机小写10个字母加固定字符串test批量创建10个html文件,创建完成后,讲test全部改为test_done(for循环实现),并html为大写HTML

分析:

1:随机是个小写字母 file=$(uuidgen | sed -r 's#[0-9-]+##g'|cut -c 1-10)

2:创建的文件名字放到一个文件中,再取出文件名

3:用mv改名用替换方式

脚本实现:

[root@jumpserver-70 scripts]# cat make_file.sh 
#!/bin/bash

for i in {1..10}
do
    file=$(uuidgen | sed -r 's#[0-9-]+##g'|cut -c 1-10)
    [ -f test_$file.html ]    
    if [ $? -eq 0 ];then
        echo "这个文件已经存在。。"
    else
        touch /opt/test_${file}.html &>/dev/null
        echo  test_${file}.html >>/opt/test1.txt
        echo "你已经创建了 test_$file.html 文件"
    fi
done

echo "------------正在执行操作------------"

for n in {1..10}
do    
name=$(sed -rn ${n}p /opt/test1.txt | sed -r 's#.*_(.*).html#\1#g')
name_new=test_done_${name}.HTML
        mv /opt/test_${name}.html /opt/$name_new 
        echo "文件 $name_new 修改成功"
done     

> /opt/test1.txt

第二种方法:

[root@web03 ~]# cat for_file2.sh 
#!/bin/bash

#1.循环创建10个随机文件名的html文件
for i in {1..10}
do
File_Name=$(openssl rand -base64 40 |sed 's#[^a-z]##g'|cut -c1-10)
touch test_${File_Name}.html
done

#2.查找当前目录下.html结尾的文件,写入一个文件中
find ./ -type f -name "*.html" > 3.txt
for i in $(cat 3.txt)
do
#4.将带html修改为HTML ${i/html/HTML} 将文件名后缀是html替换为HTML
mv  $i ${i/html/HTML}
done

实现效果:

[root@jumpserver-70 scripts]# sh make_file.sh 
你已经创建了 test_fababba.html 文件
你已经创建了 test_cdedacafaf.html 文件
你已经创建了 test_eedbacbafc.html 文件
你已经创建了 test_adbfdeb.html 文件
你已经创建了 test_afdddcdebe.html 文件
你已经创建了 test_bacfaeeacf.html 文件
你已经创建了 test_bcfadfbcdd.html 文件
你已经创建了 test_faeebfcdc.html 文件
你已经创建了 test_eebecffac.html 文件
你已经创建了 test_eefca.html 文件
------------正在执行操作------------
文件 test_done_fababba.HTML 修改成功
文件 test_done_cdedacafaf.HTML 修改成功
文件 test_done_eedbacbafc.HTML 修改成功
文件 test_done_adbfdeb.HTML 修改成功
文件 test_done_afdddcdebe.HTML 修改成功
文件 test_done_bacfaeeacf.HTML 修改成功
文件 test_done_bcfadfbcdd.HTML 修改成功
文件 test_done_faeebfcdc.HTML 修改成功
文件 test_done_eebecffac.HTML 修改成功
文件 test_done_eefca.HTML 修改成功
[root@jumpserver-70 opt]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 20 21:00 test1.txt
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_aadacfbdea.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_badbcedabf.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_ccdbddfaff.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_daafffacfb.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_dbdecdabdc.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_dceafb.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_dcedbcbfa.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_eacfaabcff.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_ecbceabcbd.HTML
-rw-r--r-- 1 root root 0 Sep 20 21:00 test_done_feceeecbdd.HTML

查看访问日志访问最高的ip

分析:

awk方法取到access.log的第一列ip地址。然后使用sort uniq 进行排列

命令实现:

 awk '{print $1}' access.log-20180929 | sort |uniq -c |sort -rn | head -1 

把磁盘状态写进文件

分析:

1:磁盘状态查看  df -h

2:   把状态写进文件 df -h >$(date +%F).log

脚本实现:

#!/bin/bash

time = $(date + %F)
df -h > $time.log

查找文件->打包->还原

分析:

1:在/backup目录下创建.txt 文件 ,并筛选出来

2:批量把txt为结尾的文件改为txt.bak

3:   把所有的.bak文件打包压缩为123.tar.gz

4: 批量还原文件的名字,及把增加的.back再删除

脚本实现:

[root@web03 ~]# cat file.sh 

#1.修改名称
find /backup/ -type f -name "*.txt" > /tmp/1.txt

for i in $(cat /tmp/1.txt)
do
mv $i ${i}.bak
done

#2.重新查找,bak文件,然后进行打包
cd /backup && tar czf 123.tar.gz $(find /backup/ -type f -name "*.bak")

#3.将.bak还原.txt
find /backup/ -type f -name "*.bak" >/tmp/2.txt

for i in $(cat /tmp/2.txt)
do
mv $i ${i%.*}
done

实现效果:

监控80端口是否开启

描述:

写一个脚本,判断本机的80端口(加入是httpd)是否开启,如果开启则什么都不用干,如果发现端口不存在,那么重启一下http服务,并发送邮件通知自己,脚本写好后每分钟执行一次,也可以写一个死循环

分析:

1:检测80端口是否正常  netstat -lntp |grep ":80"

2:   如果不正常重启nginx

3: 如果进程是启动的则重启,否则直接启动

脚本实现:

[root@web03 ~]# cat status.sh 
#!/usr/bin/bash
while true
do
#1.检查Nginx是否存或,让wc统计行数,方便整数比对
Nginx_status=$(netstat -lntp|awk '{print $4}'|grep :80$|wc -l)

#2.检查Nginx的值是1还是0,如果是0则代表没启动Nginx,如果是1则不做处理
if [ $Nginx_status -eq 0 ];then
echo  "正在尝试重启..."
systemctl restart nginx &>/dev/null
if [ $? -eq 0 ];then
echo "重启成功..."
else
echo "重启失败"
fi
fi
#3.判断结束后,休息5s
sleep 5
done

网页出现502错误重启php

描述:

现在有一个lnmp环境, 经常出现502错误, 只要一重启php-fpm即可解决,如果不重启则会持续非常长的一段时间,所有有必要写一个监控脚本,监控访问日志的状态码,一旦发生502,则自动重启一下php-fpm

分析:

1.动态的监控日志尾部最后300行,统计502出现的总50次数
2.精准判断是否是502错误,不是则不处理,是则重启php-fpm

脚本实现:

#1.准备对应的Nginx环境
[root@web03 ~]# cat /etc/nginx/conf.d/discuz.conf 
server {
        listen 80;
        root /code;
        index  index.html;
server_name _;

location / {
proxy_pass http://127.0.0.1:8888;

}
location /test {
root /code;
index inde.html;
}
}

2.编写对应的环境
[root@web03 ~]# cat s.sh 
#!/usr/bin/bash
while true
do

#1.实时的抓取日志尾部的信息,筛选出502,进行统计
LogFile=$(tail -n300 /var/log/nginx/access.log|grep 502|wc -l)

#2.使用if进行判断
if [ $LogFile -gt 50 ];then
echo "正常重启php-fpm"
pkill php-fpm && systemctl start php-fpm &>/dev/null
if [ $? -eq 0 ];then
echo "重启成功"
else
echo "重启失败"
fi
else
echo "实时监控日志没有达到300行502"
fi
sleep 5
done

使用ab工具进行压力测试:

故障的 url: ab -n 1000 -c 200 http://10.0.0.9/
恢复的 url: ab -n 1000 -c 200 http://10.0.0.9/test/index.html

取出给定单词的长度

描述:

用shell打印下面这句话中字母数小于6个的单词Bash also interprets a number of multi-user optios

分析:

1.使用循环遍历
2.怎么统计单词数值 wc -c

脚本实现:

[root@web03 ~]# cat num.sh 
#!/usr/bin/bash

for i in Bash also interprets a number of multi-user optios
do
#1.传统获取变量的个数
#re=$(echo -n $i|wc -c)

#2.获取变量的个数
re=$(echo ${#i})
#3.进行数字比对
if [ $re -lt 6 ];then
echo $i
fi
done

猜你喜欢

转载自www.cnblogs.com/tim1blog/p/9706519.html