shell企业面试题

shell企业面试题

1.利用bash for循环打印下面这句话中字母数不大于6的单词

I am oldboy teacher welcome to oldboy training class

[root@ci-node1 mst]# cat 1.sh 
#!/bin/bash
##############################################################
# File Name: 1.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-15 22:28:35
# Description:
##############################################################
#数组实现
arrary=(I am oldboy teacher welcome to oldboy training class)
for ((i=0;i<${#arrary[*]};i++))
do
    if [ ${#arrary[i]} -le 6 ]
    then
        echo ${arrary[i]}
    fi
done
echo ---------------------------
#使用for循环实现
for i in I am oldboy teacher welcome to oldboy training class
do
    if [ ${#i} -le 6 ]
    then
        echo $i
    fi
done
#使用awk循环实现
[root@ci-node1 mst]# chars="I am oldboy teacher welcome to oldboy training class"
[root@ci-node1 mst]# echo $chars
I am oldboy teacher welcome to oldboy training class
[root@ci-node1 mst]# echo $chars|awk '{for(i=0;i<=NF;i++) if(length($i)<=6)print $i}'

2. 批量随机生成字符文件案例

使用for循环在/oldboy目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字每加固定oldboy

随机生成字符串的方法
1.echo $RANDOM 取值范围是0-32767
2.openssl rand -base64 100
3.date +%s%N
4.head /dev/urandom |cksum 
5.uuidgen
6.cat /proc/sys/kernel/random/uuid 
7.mkpasswd(需先安装yum -y expect)
[root@ci-node1 mst]# mkpasswd -l 20 -d 10 -c 5 -C 3 -s 2
-l:指定长度
-d:指定数字
-c:指定小写
-C:指定大写
-s:指定特殊符号
cat: 1.: No such file or directory
[root@web01 scripts]# cat 1.sh 
#!/bin/bash
##############################################################
# File Name: 1.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-15 19:04:12
# Description:
##############################################################
[ -d /oldboy ] || mkdir /oldboy
for name in {1..10}
do
    cd /oldboy
    rand=`echo "$RANDOM"|md5sum |tr "[0-9]" "[a-z]"|cut -c 2-10` 
    touch ${rand}_oldboy.html
done
echo "文件已创建完成"

2.批量改名特殊案例

将上面题1中的结果文件名中的oldboy字符串全部改成oldgirl(最好使用for循环实现),并且将扩展名改为HTML大写

方法一:使用awk进行批量改名
[root@web01 scripts]# ls /oldboy/*.html|awk -F "oldboy.html" '{print "mv",$0,$1".oldgril.HTML"}'|bash
方法二:使用for循环
[root@web01 scripts]# cat 2.sh 
#!/bin/bash
##############################################################
# File Name: 2.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-15 19:55:32
# Description:
##############################################################
file_path=`ls /oldboy`
for i in $file_path
do
    cd /oldboy
    mv $i `echo ${i/oldgril.HTML/oldboy.html}`
done
方法三:使用rename
语法:rename 找查的内容 替换的内空  文件名
[root@web01 oldboy]# rename ".oldgril.HTML" "oldboy.html" *.HTML

3.批量创建特殊要求用户案例

批量创建10个系统账号oldboy01-oldboy10,并设置密码为随机数,要求字符和数字混合)

[root@web01 scripts]# vim useradd.sh 

#!/bin/bash
##############################################################
# File Name: useradd.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 09:29:02
# Description:
##############################################################
. /etc/init.d/functions
#方法一:使用拼接方式创建
#user=`seq -w 11 15`
#for i in $user
#do
#    passwd=`openssl rand -base64 5`
#    useradd oldboy$i 
#    echo $passwd|passwd --stdin oldboy$i
#    echo -e "oldboy$i\t$passwd" >> /tmp/useradd.log
#done
#方法二:使用chpasswd方法创建,此方法只能使用这一种格式:用户名:密码
for i in {22..25}
do
    passwd=`openssl rand -base64 5`
    grep "oldboy$i" /etc/passwd &>/dev/null
    if [ $? -ne 0 ]
    then
        useradd oldboy$i &>/dev/null
        echo "oldboy$i:$passwd" >> /tmp/user.log
        action "oldboy$i is successful" /bin/true
    else
        action "oldboy$i is exists" /bin/false
     fi
done

4.扫描网络内存活主机案例

写一个shell脚本,判断10.0.0.0/24网段中,当前在线的ip的址有多少

方法一:使用脚步进行检测
[root@web01 scripts]# cat ping.sh 
#!/bin/bash
##############################################################
# File Name: ping.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 11:18:42
# Description:
##############################################################
. /etc/init.d/functions
for i in {1..254}
do
    {
    if `ping -c 1 -w 3  172.16.1.$i >&/dev/null` 
    then
        echo "172.16.1.$i is up" 
    else
        echo "172.16.1.$i is down"
    fi
} &
done
方法二:使用命令
[root@web01 scripts]# nmap -sP 10.0.0.0/24|awk '/Nmap scan report for/{print $NF}'

5.mysql数据库分库备份

实现对mysql数据库进行分库备份,使用脚本

使用命令查询数据库信息
mysql -uroot -poldboy -e "show database;"
[root@web01 scripts]# cat mysql.sh 
#!/bin/bash
##############################################################
# File Name: mysql.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 14:43:23
# Description:
##############################################################
. /etc/init.d/functions
user="root"
password="oldboy"
path=/backup/mysql
mysql_name=`mysql -u$user -p$password -e "show databases;" 2>/dev/null|grep -v _schema|sed 1d`
[ -d $path ] || mkdir -p $path
for name in $mysql_name
do
    mysqldump -u$user -p$password -B $name &>/dev/null|gzip >$path/${name}_$(date +%F).sql.gz 
    if [ $? -eq 0 ]
    then
        action "$name backcup is success" /bin/true
    else
        action "$name backup is fail" /bin/false
    fi
done

6.mysql数库分库分表备份

如何实现对mysql数据库进行分库加分表备份

使用命令查询分表信息
[root@web01 scripts]# mysql -uroot -poldboy -e "show tables from wordpress;"
    前提:需在/etc/my.cnf配置文件中添加mysql用户名与密码
[client]
user="root"
password="oldboy"
[root@web01 scripts]# cat mysql_1.sh 
#!/bin/bash
##############################################################
# File Name: mysql.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 14:43:23
# Description:
##############################################################
. /etc/init.d/functions
path=/backup/mysql
mysql_name=`mysql -e "show databases;" |grep -v _schema|sed 1d`
[ -d $path ] || mkdir -p $path
for name in $mysql_name
do
    mysql_table=`mysql -e "show tables from $name;"|sed 1d`
    for tname in ${mysql_table}
    do
        if [ "$name" = "mysql" ]
        then
            mysqldump  --skip-lock-tables  $name $tname |gzip >$path/${name}_${tname}_$(date +%F).s
ql.gz             if [ $? -eq 0 ]
            then
                action "$name backcup is success" /bin/true
            else
                action "$name backup is fail" /bin/false
            fi
        else
            mysqldump  $name $tname |gzip >$path/${name}_${tname}_$(date +%F).sql.gz            
            if [ $? -eq 0 ]
            then
                action "$name backcup is success" /bin/true
            else
                action "$name backup is fail" /bin/false
            fi
        fi
    done
done

7.SSH免密钥批量分发文件专业脚本案例

有3台机器,m01,backup,nfs01,采用ssh免密钥实现从m01到其它两台机器无密码登陆后,批量分发任意文件到其它两台机器的任意目录下

#分发免密钥脚本
[root@m01 scripts]# cat ansible.sh 
#!/bin/bash
ip='7 31'
key='dsa'
if [ -f "/root/.ssh/id_$key" ]
then
    rm -f /root/.ssh/id_$key
fi
ssh-keygen -t $key -f /root/.ssh/id_dsa -N ""
for ip in $ip
do
sshpass -pliuyang ssh-copy-id -i /root/.ssh/id_$key.pub "-o StrictHostKeyChecking=no 172.16.1.$ip -
p10000"done
分发脚本
    [root@m01 scripts]# cat 2.sh 
#!/bin/bash
. /etc/init.d/functions
ip='7 31'
if [ $# -ne 2 ]
then
    echo "Uasge:$0 localdir remotedir"
    exit 1
fi
for i in $ip
do
    echo =====172.16.1.$i========
    scp -rP10000 $1 172.16.1.$i:$2 &>/dev/null
    if [ $? -eq 0 ]
    then 
       action "$1 is successful" /bin/true
    else
       action "$1 is fail" /bin/false
    fi
done 

8.破解RANDOM随机数案例

已知下面的字符串是RANDOM通过md5sum后,再截取一部分连续字符串的结果,请破解这些字符串对应的使用的md5sum处理前RANDOM的数字
21029299
00205d1c
a3da1677
1f6d12dd
890684b

[root@web01 scripts]# cat md5.sh 
#!/bin/bash
##############################################################
# File Name: md5.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 16:43:18
# Description:
##############################################################
md5=(
21029299
00205d1c
a3da1677
1f6d12dd
890684b
)
generate_md5(){
    for num in {0..32767}
    do  
        echo -e "$num\t `echo $num|md5sum`" >> /tmp/md5.log
    done

}
check_md5(){
    for i in ${md5[*]}
    do
        if [ `grep $i /tmp/md5.log|wc -l` -eq 1 ]
        then
            grep $i /tmp/md5.log
        fi
    done
}
main(){
    path=/tmp/md5.log
    if [ ! -s  "$path" ]
    then
        generate_md5
    fi
    echo ======数据解密码中======
    check_md5
    echo ======数据解密完成======
}
main

9.批量检查多个网站地址是否正常

要求:
1.使用shell数组方法实现,检测策略尽量模用户访问
2.每10秒钟做一次检测,无法访问的输出报警
3.等检测的地址如下
http://blog.oldboyedu.com
http://blog.etiantian.org
http://10.0.0.7

[root@web01 scripts]# cat web.sh 
#!/bin/bash
##############################################################
# File Name: web.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 18:03:53
# Description:
##############################################################
. /etc/init.d/functions
url=(
http://blog.oldboyedu.com
http://blog.etiantian.org
http://10.0.0.7
http://172.16.1.7
http://www.baidu.com
)
check_url(){
    for ((i=0;i<${#url[*]};i++))
    do
        wget --spider -T 5 -t 2 -q -o /dev/null ${url[$i]}
        if [ $? -eq 0 ]
        then
            action "${url[$i]} is runing" /bin/true
        else
            action "${url[$i]} in not runing" /bin/false
        fi
    done
}
main(){
    while true
    do
        echo -------------
        check_url
        sleep 10
    done

}
main

10.解决DOS攻击生产案例

请根据web日志或者网络连接数,监控某一个IP并发连接数或都 短时间pv达到100,即调用防火墙命令封掉对应的IP,防火墙命令为iptables -I INPUT -s ip地址 -j DROP
web访问日志监控脚本:

[root@web01 scripts]# cat iptables.sh 
#!/bin/bash  ##############################################################
# File Name: iptables.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-16 19:52:29
# Description:
##############################################################
path=/server/scripts/access_2010-12-8.log
while true
do
    awk '{print $1}' ${path}|sort -nr|uniq -c  >/tmp/web.log
    while read line
    do
        count=`echo $line|awk '{print $1}'`
        ip=`echo $line|awk '{print $2}'`
        drop_ip=`iptables -nL|awk '/DROP/{print $4}'|grep "$ip"`
        if [ $count -ge 50 ]
        then
            if [ "$ip" != "$drop_ip" ]
            then
                iptables -I INPUT -s $ip -j DROP
            fi
        fi
    done</tmp/web.log
done

根据连接数禁止ip访问

path=/server/scripts/netstat.log
awk -F "[ :]+" '/ESTABLISHED/{s[$6]++}END{for(k in s) print s[k],k}' $path|sort -nr >/tmp/web01.log
while read line
do
    count=`echo $line|awk '{print $1}'`
    ip=`echo $line|awk '{print $2}'`
    drop_ip=`iptables -nL|awk '/DROP/{print $4}'|grep "$ip"`
    if [ $count -ge 100 -a "$ip" != "$drop_ip" ]
    then
        iptables -I INPUT -s $ip -j DROP
    fi
done</tmp/web01.log

11.开发mysql服务启动脚本

[root@web01 scripts]# cat mysqld.sh 
#!/bin/bash
##############################################################
# File Name: mysqld.sh
# Version: V1.0
# Author: liu
# Organization: 
# Created Time : 2019-04-17 11:37:51
# Description:
##############################################################
. /etc/init.d/functions
mysql_pid=/application/mysql/data/`uname -n`.pid
lockfile=/var/lock/subsys/mysqld
datadir=/application/mysql/data
mysqld_safe=/application/mysql/bin/mysqld_safe
start(){
    /bin/sh $mysqld_safe  --datadir=$datadir --pid-file=$mysql_pid &>/dev/null &
    retval=$?
    sleep 2
    if [ $retval -eq 0 ]
    then
        action  "Mysql start is  successful..."  /bin/true
        touch $lockfile
        return $retval
    else
        action "Mysql start is fail... " /bin/false
        return $retval
    fi
}
stop(){
    if [ -s "$mysql_pid" ]
    then
        mysql_pid=`cat $mysql_pid`
        if (kill -0 $mysql_pid &>/dev/null)
        then
            kill $mysql_pid
            retval=$?
            sleep 2
            if [ $retval -eq 0 ]
            then
                action "Mysql stop is successful" /bin/true
                rm -f $lockfile
                return $retval
            else
                action "Mysql stop is fail" /bin/false
                return $retval
            fi
        fi
    fi
}
case $1 in
    start)
        start
        retval=$?
        ;;
    stop)
        stop
        retval=$?
        ;;
    restart)
        stop
        retval=$?
        sleep 2
        start
        retval=$?
        ;;
    *)
        echo "Usage:$0{start|stop|restart|}"
esac
exit $retval

12.单词及字母去重排序案例

用shell处理以下内容
1.按单词出现频率降序排序
2.按字母现现频率降序排序
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
1.按单词出现频率降序排序
方法一:

cat 12.log|tr " , ." "\n"|sort|uniq -c|sort -nr

方法二:

cat 12.log|tr " .," "\n"|awk  '{s[$1]++}END{for(k in s) print s[k],k}'|sort -nr

方法三:RS表示分割符

awk 'BEGIN{RS="[ ,.]"}{s[$1]++}END{for(k in s) print s[k],k}' 12.log |sort -nr

2.按字母现现频率降序排序
方法一:

grep -o "[a-z]" 12.log |sort|uniq -c|sort -nr

方法二:

grep -o "[a-z]" 12.log |awk '{s[$1]++}END{for(k in s) print s[k],k}' | sort -nr

方法三:

sed "s#[ ,\.]##g" 12.log|awk -F "" '{for(i=1;i<NF;i++)s[$i]++}END{for(k in s)print s[k],k}'|sort -nr
posted @ 2019-04-22 08:45  游走在边缘的人  阅读(277)  评论(0编辑  收藏  举报