分类:shell习题
shell习题-成员分组
需求是,把所有的成员平均得分成若干个小组。这里,我会提供一个人员列表,比如成员有50人,需要分成7个小组,要求随机性,每次和每次分组的结构应该不一致。
参考答案
假设成员列表文件为members.txt
#!/bin/bash f=members.txt n=`wc -l $f|awk '{print $1}'` get_n() { l=`echo $1|wc -c` n1=$RANDOM n2=$[$n1+$l] g_id=$[$n1%7] if [ $g_id -eq 0 ] then g_id=7 fi echo $g_id } for i in `seq 1 7` do [ -f n_$i.txt ] && rm -f n_$i.txt done for i in `seq 1 $n` do name=`sed -n "$i"p $f` g=`get_n $name` echo $name >> n_$g.txt done nu(){ wc -l $1|awk '{print $1}' } max(){ ma=0 for i in `seq 1 7` do n=`nu n_$i.txt` if [ $n -gt $ma ] then ma=$n fi done echo $ma } min(){ mi=50 for i in `seq 1 7` do n=`nu n_$i.txt` if [ $n -lt $mi ] then mi=$n fi done echo $mi } ini_min=1 while [ $ini_min -le 7 ] do m1=`max` m2=`min` ini_min=m2 for i in `seq 1 7` do n=`nu n_$i.txt` if [ $n -eq $m1 ] then f1=n_$i.txt elif [ $n -eq $m2 ] then f2=n_$i.txt fi done name=`tail -n1 $f1` echo $name >> $f2 sed -i "/$name/d" $f1 ini_min=$[$ini_min+1] done for i in `seq 1 7` do echo "$i 组成员有:" cat n_$i.txt echo done
shell习题-计算单词重复次数
将文件内所有的单词的重复次数计算出来,只需要列出重复次数最多的10个单词。
参考答案:
假设文档名字叫做a.txt sed 's/[^a-zA-Z]/ /g' a.txt|xargs -n1 |sort |uniq -c |sort -nr |head
shell习题-备份etc下面文件
设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名为如下形式”yymmdd_etc.tar.gz”,yy为年,mm为月,dd为日。
参考答案:
#!/bin/sh if [ ! -d /root/bak ] then mkdir /root/bak fi prefix=`date +%y%m%d` d=`date +%d` if [ $d == "01" ] then cd /etc/ tar czf /root/bak/$prefix_etc.tar.gz ./ fi
shell习题-给文档增加内容
在文本文档1.txt第5行(假设文件行数大于5)后面增加如下内容:
# This is a test file.
# Test insert line into this file.
参考答案:
sed -i "5a # This is a test file.\n# Test insert line into this file." 1.txt
shell习题-打印数字
写一个shell脚本。提示你输入一个暂停的数字,然后从1打印到该数字。然后询问是否继续。继续的话在输入个在数字 接着打印。不继续退出。
例:如果输入的是5,打印1 2 3 4 5 然后继续 输入15 然后打印 6 7 …14 15 依此类推。
参考答案:
#!/bin/bash read -p "请输入您想要暂停的数字:" number_1 for i in `seq 1 $number_1`; do echo $i done read -p "是否继续输入数字?" a if [ $a == "yes" ];then read -p "请继续输入您想要暂停的数字:" number_2 number_3=$[$number_1+1] if [ $number_2 -gt $number_1 ];then for h in `seq $number_3 $number_2`; do echo $h done else echo "输入数字错误,请输入大于的数字!" fi else exit fi
shell习题-统计分析日志
已知nginx访问的日志文件在/usr/local/nginx/logs/access.log内
请统计下早上10点到12点 来访ip最多的是哪个?
日志样例:
111.199.186.68 – [15/Sep/2017:09:58:37 +0800] “//plugin.php?id=security:job” 200 “POST //plugin.php?id=security:job HTTP/1.1″”http://a.lishiming.net/forum.php?mod=viewthread&tid=11338&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3141.7 Safari/537.36” “0.516”
203.208.60.208 – [15/Sep/2017:09:58:46 +0800] “/misc.php?mod=patch&action=ipnotice&_r=0.05560809863330207&inajax=1&ajaxtarget=ip_notice” 200 “GET /misc.php?mod=patch&action=ipnotice&_r=0.05560809863330207&inajax=1&ajaxtarget=ip_notice HTTP/1.1″”http://a.lishiming.net/forum.php?mod=forumdisplay&fid=65&filter=author&orderby=dateline” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3141.7 Safari/537.36” “0.065”
参考答案:
grep '15/Sep/2017:1[0-2]:[0-5][0-9]:' /usr/local/nginx/logs/access.log|awk '{print $1}'|sort -n|uniq -c |sort -n|tail -n1
shell习题-端口解封
一个同学提到一个问题,他不小心用iptables规则把sshd端口22给封掉了,结果不能远程登陆,要想解决这问题,还要去机房,登陆真机去删除这规则。 问题来了,要写个监控脚本,监控iptables规则是否封掉了22端口,如果封掉了,给打开。 写好脚本,放到任务计划里,每分钟执行一次。
参考答案:
#!/bin/bash # check sshd port drop /sbin/iptables -nvL --line-number|grep "dpt:22"|awk -F ' ' '{print $4}' > /tmp/drop.txt i=`cat /tmp/drop.txt|head -n 1|egrep -iE "DROP|REJECT"|wc -l` if [ $i -gt 0 ] then /sbin/iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT fi
shell习题-文件打包
需求:将用户家目录(考虑到执行脚本的用户可能是普通用户也可能是root)下面小于5KB的文件打包成tar.gz的压缩包,并以当前日期为文件名前缀,例如今天打包的文件为2017-09-15.tar.gz。
参考答案:
#!/bin/bash t=`date +%F` cd $HOME tar czf $t.tar.gz `find . -type f -size -5k`
shell习题-监控web可用性
写一个shell脚本,通过curl -I 返回的状态码来判定所访问的网站是否正常。比如,当状态码为200时,才算正常。
参考答案:
#/bin/bash url="http://www.apelearn.com/index.php" sta=`curl -I $url 2>/dev/null |head -1 |awk '{print $2}'` if [ $sta != "200" ] then python /usr/local/sbin/mail.py xxx@qq.com "$url down." "$url down" fi
shell习题-shell中的小数
2017年9月27日
shell习题
No Comments
aming
题目如下:
a=0.5 b=3 c=a*b 求c的值
参考答案