美文网首页
shell(操作题)

shell(操作题)

作者: 1220 | 来源:发表于2019-06-12 10:57 被阅读0次

    考试题 5:

    通过脚本传参的方式,检查 Web 网站 URL 是否正常(要求主体使用函数)。

    #!/bin/bash
    . /etc/init.d/functions
    check_url(){
     wget -q -o /dev/null -T 2 --tries=1 --spider $1
     if [ $? -eq 0 ]
     then
     action "$1 is ok" /bin/true
     else
     action "$1 is no" /bin/false 
     fi
    }
    usage(){
     echo "Usage: $0 url"
    }
    main(){
     if [ $# -ne 1 ]
     then
     usage
     fi
     check_url $1
    }
    main $*
    

    考试题 6:

    开发 Nginx 服务(编译安装)启动停止脚本,并分别通过 chkconfig(CentOS6)
    和 systemctl(CentOS7)实现开机自启动。

    #!/bin/bash
    # chkconfig: 2345 66 88
    ##############################################################
    # File Name: 6.1.sh
    # Version: V1.0
    # Author: oldboy
    # Organization: www.oldboyedu.com
    ##############################################################
    . /etc/init.d/functions
    usage(){
     echo "Usage: $0 {start|stop|restart}"
    }
    start(){
     /application/nginx/sbin/nginx &>/dev/null
     if [ `netstat -lntup |grep nginx|wc -l` -eq 0 ]
     then
     action "nginx 开启失败" /bin/false
     else
     action "nginx 开启成功" /bin/true
     fi
    }
    stop(){
     /application/nginx/sbin/nginx -s stop &>/dev/null
     if [ `netstat -lntup |grep nginx|wc -l` -eq 0 ]
     then
     action "nginx 关闭成功" /bin/true
     else
     action "nginx 关闭失败" /bin/false
     fi
    }
    restart(){
     /application/nginx/sbin/nginx -s stop &>/dev/null
     /application/nginx/sbin/nginx &>/dev/null
     if [ `netstat -lntup |grep nginx|wc -l` -eq 0 ]
     then
     action "nginx 重启失败" /bin/false
     else
     action "nginx 重启成功" /bin/true
     fi
    }
    case $1 in
     start)
     start
     ;;
     stop)
     stop
     ;;
     restart)
     restart
     ;;
     *)
     usage
     ;;
    esac
    ==================================================================================
    ```[root@web01 /etc/systemd/system]# cat nginxd2.service``` 
    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    ExecStart=/etc/init.d/nginxd2 start
    ExecReload=/etc/init.d/nginxd2 restart
    ExecStop=/etc/init.d/nginxd2 stop
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    

    考试题 7:

    猜数字游戏。首先让系统随机生成一个数字,给这个数字定一个范围(1-60),
    让用户输入猜的数字,对输入进行判断,如果不符合要求,就给予高或低的提示,猜对后则
    给出猜对用的次数。

    sum=$((RANDOM%61))
    cishu=0
    echo "猜数字游戏,猜对有奖励"
    function duru(){
     read -p "请输入你心中想的数字" a
     if [[ ! $a =~ [0-9]+ ]]
     then
     echo "别闹,请输入数字,再来一次吧"
     sleep 1
     duru
     fi
    }
    function cai(){
     ((cishu++))
     if [ $a -eq $sum ]
     then
     echo "yes , this is $sum"
     if [ $cishu -le 3 ]
     then
     echo "真棒,您一共猜了${cishu}次,打败全球 99%的用户"
     elif [ $cishu -le 6 -a $cishu -gt 3 ]
     then
     echo "还行,您一共猜了${cishu}次,打败全球 66%的用户"
     else
     echo "您一共猜了${cishu}次,可不中啊"
     fi
     exit 0
     elif [ $a -gt $sum ]
     then
     echo "太大了,再来一次吧"
     sleep 1
     duru
     elif [ $a -lt $sum ]
     then
     echo "太小了,再来一次吧"
     sleep 1
     duru
     fi
    }
    function main(){
    duru
    while true
    do
     cai
    done
    }
    main
    

    考试题 8:

    计算从 1 加到 100 之和(要求用 for 和 while,至少给出两种方法)。

    #!/bin/bash
    ##############################################################
    # File Name: 8.1.sh
    # Version: V1.0
    # Author: oldboy
    # Organization: www.oldboyedu.com
    ##############################################################
    sum=0
    for i in `seq 1 100`
    do
     sum=$[$sum+$i]
    done
    echo $sum
    #!/bin/bash
    ##############################################################
    # File Name: 8.2.sh
    # Version: V1.0
    # Author: oldboy
    # Organization: www.oldboyedu.com
    ##############################################################
    j=0
    for((i=0;i<=100;i++))
    do
     ((j=j+i))
    done
    echo $j
    #!/bin/bash
    ##############################################################
    # File Name: 8.3.sh
    # Version: V1.0
    # Author: oldboy
    # Organization: www.oldboyedu.com
    ##############################################################
    i=0
    n=1
    while [ $n -lt 101 ]
    do
     i=$[$n+$i]
     n=$[$n+1]
    done
    echo $i
    #!/bin/bash
    ##############################################################
    # File Name: 8.4.sh
    # Version: V1.0
    # Author: oldboy
    # Organization: www.oldboyedu.com
    ##############################################################
    i=0
    n=100
    until [ $n -le 0 ]
    do
     i=$[$n+$i]
     n=$[$n-1]
    done
    echo $i
    

    考试题 9:

    利用 bash for 循环打印下面这句话中字母数不大于 6 的单词(某企业面试真
    题)。I am oldboy teacher welcome to oldboy training class

    a=(I am oldboy teacher welcome to oldboy training class)
    for n in ${a[*]}
    do
     if [ ${#n} -le 6 ]
     then 
     echo "${n}"
     fi
    done
    

    考试题 10:

    使用 read 读入方式比较两个整数大小,要求对用户输入的内容严格判断是否
    为整数,是否输入了两个数字。

    read -p "pls input two number:" nmb1 nmb2
    expr $nmb1 + 1 &>/dev/null
    retval1=$?
    expr $nmb2 + 1 &>/dev/null
    retval2=$?
    if [ -z "$nmb1" -o -z "$nmb2" -o $retval1 -ne 0 -o $retval2 -ne 0 ]
    then
     echo "error must input two number"
     exit 1
    fi
    if [ $nmb1 -gt $nmb2 ]
    then
     echo "$nmb1>$nmb2"
    elif [ $nmb1 -lt $nmb2 ]
    then
     echo "$nmb1<$nmb2"
    else
     echo "$nmb1=$nmb2"
    fi
    

    考试题 11:

    批量生成随机字符文件名案例
    使用 for 循环在/oldboy 目录下批量创建 10 个 html 文件,其中每个文件需要包含 oldboy
    固定字符串加 10 个随机小写字母,名称示例如下:
    [root@oldboy scripts]# ls /oldboy
    oldboy_amaeeurmja.html oldboy_jmtiqwhinw.html oldboy_rnbwmjkjkh.html oldboy_yhufdiceox.html
    oldboy_antodvkjcf.html oldboy_ncivkckvok.html oldboy_tyqfhfxvup.html
    oldboy_epwmszmttm.html oldboy_osjmrurgsx.html oldboy_vpgslhajdn.html

    Path=/oldboy
    [ -d $Path ] || mkdir $Path
    for n in {1..10}
    do
     rand=`openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 1-10`
     touch $Path/oldboy_${rand}.html
    done
    

    考试题 12:

    批量改名特殊案例
    将上题 11 中结果文件名中的 oldboy 字符串全部改成 oldgirl,并且将扩展名 html 全部改成
    大写 HTML(不低于 2 种方法)。

    rename oldboy oldgirl *.html && rename .html .HTML *.html
    ls *.html |awk -F "[._]" '{print "mv",$0,"oldgirl_"$2".HTML" }'|bash
    

    考试题 13:

    批量创建特殊要求用户案例
    批量创建 10 个系统帐号 oldboy01-oldboy10 并设置密码(密码为随机 8 位数)。
    不用 for 循环的实现思路:http://user.qzone.qq.com/49000448/blog/1422183723

    for user in {01..10}
    do
    pass=`echo "$RANDOM"|md5sum|cut -c 1-8`
    useradd oldboy$user &>/dev/null
    echo "$pass"|passwd --stdin oldboy$user &>/dev/null
    echo "oldboy$user $pass" >>/tmp/pass.log
    done
    

    考试题 14:

    解决 DOS 攻击生产案例
    写一个 Shell 脚本解决 DOS 攻击生产案例。
    请根据 web 日志,监控当某个 IP 短时内 PV 达到 100(读者根据实际情况设定),即调用
    防火墙命令封掉对应的 IP。防火墙命令为:iptables -I INPUT -s IP -j DROP(IP 为要封的
    地址)。

    awk '{print $1}' $1|grep -v "^$"|sort|uniq -c >/tmp/tmp.log
    exec </tmp/tmp.log
    while read line
    do
     ip=`echo $line|awk '{print $2}'`
     count=`echo $line|awk '{print $1}'`
     if [ $count -ge 30 ] && [ `iptables -L -n|grep $ip|wc -l` -lt 1 ]
     then
     iptables -I INPUT -s $ip -j DROP
     echo "$ip is droped" >>/tmp/droplits_$(date +%F).log
     fi
    done 
    执行方式 sh 14.sh access_2010-12-8.log
    执行方式 sh 14.sh access_2010-12-8.log
    

    考试题 15:

    菜单自动化软件部署经典案例
    综合实例:打印选择菜单,按照选择一键安装不同的 Web 服务。
    示例菜单:
    [root@oldboy scripts]# sh menu.sh
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
    要求:
    1、当用户输入 1 时,输出“start installing lamp.提示”然后执行/server/scripts/lamp.sh,
    脚本内容输出"lamp is installed"后退出脚本,工作中就是正式 lamp 一键安装脚本;
    2、当用户输入 2 时,输出“start installing lnmp.提示” 然后执行/server/scripts/lnmp.sh
    输出"lnmp is installed"后退出脚本,工作中就是正式 lnmp 一键安装脚本;
    3、当输入 3 时,退出当前菜单及脚本;
    4、当输入任何其它字符,给出提示“Input error”后退出脚本;
    5、要对执行的脚本进行相关的条件判断,例如:脚本文件是否存在,是否可执行等判断,
    尽量用上前面讲解的知识点。

    shu(){
     cat <<EOF
     1、install lamp
     2、install lnmp
     3、exit
    EOF
     read -p "请输入数字安装程序" sum
     expr $sum + 1 &>/dev/null
     if [ $? -ne 0 ]
     then
     echo 请输入数字 1、2、3
     shu
     elif [ ${#sum} -lt 1 ]
     then
     echo 您未输入数字
     shu
     elif [ $sum -eq 1 ]
     then
     lamp
     elif [ $sum -eq 2 ]
     then
     lnmp
     elif [ $sum -eq 3 ]
     then
     tui
     fi
    }
    lamp(){
     if [ -x /server/scripts/lamp.sh ] && [ -f /server/scripts/lamp.sh ]
     then
     . /server/scripts/lamp.sh
     echo "start install lamp"
     shu
     else
     echo "您的脚本有异常"
     shu
     fi
    }
    lnmp(){
     if [ -x /server/scripts/lnmp.sh ] && [ -f /server/scripts/lnmp.sh ]
     then
     . /server/scripts/lnmp.sh
     echo "start install lnmp"
     shu
     else
     echo "您的脚本有异常"
     shu
     fi
    }
    tui(){
     exit 0
    }
    main(){
     while true
     do
     shu
     lamp
     lnmp
     tui
     done
    }
    main
    

    考试题 16:

    批量检查多个网站地址是否正常
    企业面试题:批量检查多个网站地址是否正常
    要求:
    1、使用 shell 数组方法实现,检测策略尽量模拟用户访问。
    2、每 10 秒钟做一次所有的检测,无法访问的输出报警。
    3、待检测的地址如下
    http://blog.oldboyedu.com
    http://www.baidu.com
    http://oldboy.blog.51cto.com
    http://10.0.0.7

    . /etc/init.d/functions
    url_list=(
    http://blog.oldboyedu.com
    http://www.baidu.com
    http://oldboy.blog.51cto.com
    http://10.0.0.7
    )
    url_ar(){
     for ((i=0;i<${#url_list[*]};i++))
     do
     wget -q -o /dev/null -T 10 --tries=1 --spider ${url_list[i]}
     if [ $? -eq 0 ]
     then
     action "${url_list[i]}" /bin/true
     else
     action "${url_list[i]}" /bin/false
     fi 
     done
    }
    main(){
    while true
    do
     url_ar
     sleep 10
    done
    }
    main
    

    考试题 17-18:

    单词及字母去重排序案例
    用 shell 处理以下内容
    1、按单词出现频率降序排序(不低于 3 种方法)
    2、按字母出现频率降序排序(不低于 3 种方法)
    the squid project provides a number of resources to assist users
    design,implement and support squid installations. Please browse the
    documentation and support sections for more infomation,by oldboy training.

    [root@web01 scripts]# tr ",. " "\n" <17.txt |sort |uniq -c|sort -nr |head -5
     2 the
     2 support
     2 squid
     2 and
     2 
    [root@web01 scripts]# tr ",. " "\n" <17.txt |awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -
    nr|head -5
    2 the
    2 support
    2 squid
    2 and
    2 
    [root@web01 scripts]# awk -F"[,. ]+" '{for(i=1;i<=NF;i++) S[$i]++}END{for(k in S) print S[k],k}' 
    17.txt|sort -nr |head -5
    2 the
    2 support
    2 squid
    2 and
    1 users
    [root@web01 scripts]# sed 's# #\n#g;s#,#\n#g' 17.txt |sort |uniq -c|sort -nr |head -5
     2 the
     2 support
     2 squid
     2 and
     1 users
    [root@web01 scripts]# sed 's# #\n#g;s#,#\n#g' 17.txt |awk '{S[$1]++}END{for(k in S) print 
    S[k],k}'|sort -nr|head -5
    2 the
    2 support
    2 squid
    2 and
    1 users
    [root@web01 scripts]# egrep -o "[a-Z]+" 17.txt |sort |uniq -c|sort -nr |head -5
     2 the
     2 support
     2 squid
     2 and
     1 users
    [root@web01 scripts]# egrep -o "[a-Z]+" 17.txt |awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -
    nr|head -5
    2 the
    2 support
    2 squid
    2 and
    1 users
    [root@web01 scripts]# awk -vRS="[^a-zA-Z]" '{S[$1]++}END{for(k in S) print S[k],k}' 17.txt|sort -
    nr|head -5
    2 the
    2 support
    2 squid
    2 and
    2
    arr=(the squid project provides a number of resources to assist usersdesign,implement and 
    support squ
    id installations. Please browse the documentation and support sections for more infomation,by 
    oldboy 
    training)
    echo ===============NO.1================
    for i in ${arr[*]}
    do
     echo $i|sed 's#[^a-Z]##g'|grep -o '[^ ]' >>/tmp/1.txt
    done
    sort /tmp/1.txt|uniq -c|sort -nr
    echo ===============NO.2================
    tr ",. " "\n" <17.txt|grep -o '[^ ]'|sort|uniq -c|sort -nr 
    echo ===============NO.3================
    tr "., " "\n" <17.txt|awk -F "" '{for(i=1;i<=NF;i++)S[$i]++}END{for(k in S)print S[k],k|"sort -nr"}' 
    ===============NO.1================
     19 s
     18 o
     17 e
     15 t
     14 n
     14 i
     12 r
     10 a
     8 u
     8 d
     7 p
     6 m
     5 l
     4 c
     4 b
     3 f
     2 y
     2 q
     2 h
     2 g
     1 w
     1 v
     1 P
     1 j
    ===============NO.2================
     19 s
     18 o
     17 e
     15 t
     14 n
     14 i
     12 r
     10 a
     8 u
     8 d
     7 p
     6 m
     5 l
     4 c
     4 b
     3 f
     2 y
     2 q
     2 h
     2 g
     1 w
     1 v
     1 P
     1 j
    ===============NO.3================
    19 s
    18 o
    17 e
    15 t
    14 n
    14 i
    12 r
     a
     u
    8 d
    7 p
    6 m
    5 l
    4 c
    4 b
    3 f
    2 y
    2 q
    2 h
    2 g
    1 w
    1 v
    1 P
    1 j
    

    考试题 19:

    已知:/etc/hosts 的内容为
    192.168.1.11 oldboy11
    192.168.1.21 oldboy21
    192.168.1.31 oldboy31
    请用 shell 脚本实现,怎么才能在输入 IP 后找到/etc/hosts 里对应的唯一的 hostname?
    解答:

    #!/bin/bash
    ##############################################################
    # File Name: 19.sh
    # Version: V1.0
    # Author: oldboy
    # Organization: www.oldboyedu.com
    ##############################################################
    if [ $# -ne 1 ]
    then
     echo "Usage: $0 + IP"
     exit 1
    else
     hostname=`awk -F "[ ]+" -vn=$1 '$1==n{print $2}' /etc/hosts`
     if [ ${#hostname} -eq 0 ]
     then
     echo "not found $1 in /etc/hosts"
     else
     echo $hostname
     fi
    fi
    

    考试题 20:

    老男孩交流群某人发问如下
    上海@Danny(122504707)21:54:37
    请教一个问题
    cat oldboy.txt
    192.168.1.1 /hello1/b.do?bb=4
    192.168.1.2 /hello2/a.do?ha=3
    192.168.1.3 /hello3/r.do?ha=4
    如何显示成以下效果
    192.168.1.1 b.do
    192.168.1.2 a.do
    192.168.1.3 r.do

    array=(
    "192.168.1.1 /hello1/b.do?bb=4"
    "192.168.1.2 /hello2/a.do?ha=3"
    "192.168.1.3 /hello3/r.do?ha=4"
    )
    for n in "${array[@]}"
    do
     echo $n|awk -F "[ /?]" '{print $1,$4}'
    done
    while read line
    do
     echo $line|awk -F "[ /?]" '{print $1,$4}'
    done < 20.txt
    

    相关文章

      网友评论

          本文标题:shell(操作题)

          本文链接:https://www.haomeiwen.com/subject/ikfhfctx.html