shell 练习题 11—20,内附答案。

来源:互联网 发布:mac黑屏能继续下载吗 编辑:程序博客网 时间:2024/05/16 12:31
11、用户输入数字,如果输入的是非数字,提示 “Include nunumbers, retry please!”并结束,如果是纯数字,返回数字结果。
12、按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为2013-09-23.log, 并且把磁盘的使用情况写到到这个文件中。
13、设计一个脚本,监控远程的一台机器(假设ip为123.23.11.21)的存活状态,当发现宕机时发一封邮件给你自己。
14、写一个脚本,查看80端口是否开放,如果开放,什么也不做,如果不开放,重启 httpd 服务,并发送邮件给自己。 建立一个计划任务,每1分钟执行一次。
15、输入一个数字,然后运行对应的一个命令。显示命令如下:*cmd meau** 1---date 2--ls 3--who 4--pwd 当输入1时,会运行date, 输入2时运行ls, 依此类推。
16、添加user_00 - user_09 10个用户,并且给他们设置一个随机密码,密码要求10位包含大小写字母以及数字,注意需要把每个用户的密码记录到一个日志文件里。
17、请详细查看如下几个数字的规律,并使用shell脚本输出后面的十个数字。
10 31 53 77 105 141 .......
10 31 53 77 105 141
  21 22 24 28 36
    1  2  4  8
18、查看Linux系统中是否有自定义用户(普通用户),若是有,一共几个
19、写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期为命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时,发邮件通知你自己。
思路:就是先df -h 然后过滤出已使用的那一列,然后再想办法过滤出百分比的整数部分,然后和85去比较,同理,inode也是一样的思路。发邮件通知你自己,需要你的系统有smtp服务,可以安装 sendmail或者postfix,安装好后不用修改配置,启动服务就可以运行,发邮件使用命令:mail -s "主题" mailer < file.txt (这个文件就是邮件内容)。mail这个命令是安装mailx包得到的。
20、写一个shell脚本来看看你最喜欢敲的命令是哪个?然后列出你最喜欢敲的命令top10。


题目来自阿铭论坛 http://ask.apelearn.com/explore/category-65 
每个题目都有试着自己去写,当然作为新手难免有考虑不周的地方,如有发现,欢迎指正。




11-20 答案

11、
#! /bin/bash
read -p "Please input a number: " n
m=`echo $n| sed 's/[0-9]//g'`
if [ -n "$m" ] -n 表示判断 m 变量是否为空
then
    echo "Include unnumbers, Retry!"
else
    echo $n
fi
read 表示和用户交互,后面的 n 是变量
12、
#! /bin/bash
d=`date +%F`
log=$d.log
df -h > $log

13、
#! /bin/bash
ip=123.23.11.21
maress=test@163.com

while: 
do 
sts=`ping -c 10 #ip |grep 'received' | awk -F ';' '{print $2}' | awk '{print $1}'`
if [ $sts ==0 ] 
then 
    echo "Host $ip is die" | mail -s "mail title"$maress
fi
sleep 30
done

14、
#! /bin/bash
n=`netstat -lnp | awk '{print $4}' | grep ':80'`
if [ -z $n ]
then
   /etc/init.d/httpd start
   mail -s "80 port is down" test@163.com
fi

#! /bin/bash
mail=123@123.com
if netstat -lnp |grep ':80' |grep -q 'LISTEN'; then
    exit
else
    /usr/local/apache2/bin/apachectl restart >/dev/null 2> /dev/null
    echo "The 80 port is down."|mail -s 'check_80' $mail
    n=`ps aux |grep httpd|grep -cv grep`
    if [ $n -eq 0 ]; then
        /usr/local/apache2/bin/apachectl start 2>/tmp/apache_start.err
    fi
    if [ -s /tmp/apache_start.err ]; then
        mail -s 'apache_start_error' $mail < /tmp/apache_start.err
    fi
fi

15、
#! /bin/bash
read -p "Please select comman 1---date 2--ls 3--who 4--pwd: " n
case $n in
    1)
        date
        ;;
    2)
        ls
        ;;
    3)
        who
        ;;
    4)
        pwd
        ;;
    *)
        echo "try agine"
esac

#! /bin/bash
echo "please chose a number"
echo
select command in  date ls who pwd
do
    case $command in
    pwd)
        pwd
        ;;
    who)
        who
        ;;
    ls)
        ls
        ;;
    date)
        date
        ;;
    *)
        echo "retry please"
        ;;
    esac
done

16、
#! /bin/bash
for i in `seq -w 00 09`
do
    useradd user_$i
    passwd=`mkpasswd -l 10 -s 0`
    echo $passwd | passwd --stdin user_$i
    echo "user_$i $passwd" >> /tmp/1.txt
done
17、
#! /bin/bash
a=10
echo $a
for n in `seq 0 9`
do
    b=$[20+(2**$n)]
    a=$[$a+$b]
    echo $a
done

18、自己写的
# cat file.sh
#! /bin/bash
for i in `awk -F ':' '{print $3}' /etc/passwd`
do
    if [ $i -gt 499 ]
    then
        echo $i
    fi
done
# sh filename.sh |wc -l
铭哥答案
#!/bin/bash
n=`awk -F ':' '>=500' /etc/passwd|wc -l`
if [ $n -gt 0 ]
then
    echo "There are $n common users."
else
    echo "No common users."
fi
19、
#! /bin/bash
d=`date +%F`
disklog=$d.disk.log
inodelog=$d.inode.log
for i in `df -h | awk '{print $5}'| grep -v [a-Z] | awk -F '%' '{print $1}'`
do
    echo `df -h` >> /tmp/$disklog
    if [ $i -gt 16 ]
    then
        mail -s "title" test@163.com < /tmp/$disklog
    fi
done

for u in `df -i | awk '{print $5}'| grep -v [a-Z] | awk -F '%' '{print $1}'`
do
    echo `df -i` >> /tmp/$inodelog
    if [ $u -gt 80 ]
    then
        mail -s "title" test@163.com < /tmp/$inodelog
    fi
done

20、 cat ~/.bash_history | sort| uniq -c| sort -rn | head

0 0