企业面试题:
1.批量生产随机字符文件名
使用for循环在/oldboy目录下批量创建10个html文件,期中每个文件需要包含10个随机小写字母加固定字符串oldboy:
#!/bin/bash
p=/oldboy
[ -d $p ] || mkdir $p
for a in {1..10}
do
n=`openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 2-11`
touch $p/oldboy_${n}.html
done
======================================================================================
2.批量改名
将上题所得文件名中的oldboy字符串全部改成oldgirl,并且将扩展名html全部改成 大写HTML(不低于2 种方法)
#!/bin/bash
a=/oldboy
cd $a
for n in `ls /oldboy|sed -r 's#oldboy_(.*).html#\1#g'`
do
#b=`ls /oldboy|sed -r 's#oldboy_(.*).html#\1#g'`
mv oldboy_${n}.html oldgirl_${n}.HTML
done
#!/bin/bash
a=/oldboy
cd $a
for n in `ls /oldboy|awk -F "[_.]" '{print $2}'`
do
#b=`ls /oldboy|awk -F "[_.]" '{print $2}'`
mv oldboy_${n}.html oldgirl_${n}.HTML
done
=======================================================================================
3.批量创建特殊要求用户案例
批量创建10 个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位数)。 不用for 循环的实现思路:
#!/bin/bash
mima=/tmp/mima.log
for n in oldboy{01..10}
do
useradd $n &>/dev/null
a=`openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 2-9`
echo "$a"|passwd --stdin $n &>/dev/null
echo -e "$n:$a" >>$mima
done
=======================================================================================
4.扫描网络内存活主机
写一个shell脚本,判断10.0.0.0/24网络里,当前在线的Ip有哪些
#!/bin/bash
for n in 10.0.0.{1..253}
do
{
ping -W 2 -c 2 ${n} &>/dev/ull
if [ $? -eq 0 ]
then
echo "$n is ok"
fi
}&
done
sleep 2
=======================================================================================
5.解决DOS攻击
写一个Shell脚本解决DOS攻击生产案例。 请根据 web 日志,监控当某个 IP 短时内 PV 达到 100(读者根据实际情况设定),即调用 防火墙命令封掉对应的IP。防火墙命令为:iptables -I INPUT -s IP -j DROP(IP为要封的 地址)
#!/bin/bash
file=$1
awk '{print $1}' $1|grep -v "^$"|sort|uniq -c >/tmp/ip.log
exec
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 已干掉"
else
fi
done
=======================================================================================
6.MySQl数据库分库备份
请用脚本实现对MySQL数据库分库备份
#!/bin/bash
my=`mysql -uroot -poldboy123 -e 2>/dev/null "show databases;"|sed 1d|egrep -v "_schema"`
for n in $my
do
mysqldump -uroot -poldboy123 2>/dev/null -B $n |gzip >/tmp/${n}.sql.gz
done
=======================================================================================
7.MySQl数据库分库分表备份
请用脚本实现对MySQL数据库分库分表备份
#!/bin/bash
for dbname in `mysql -e "show databases;"|sed 1d|egrep -v "_schema"`;
do
for tablename in `mysql -e "show tables from $dbname;"|sed 1d`
do
mysqldump --lock-tables=0 $dbname $tablename|gzip >/tmp/${dbname}_${tablename}_$(date +%F).sql.gz;
done
done
=======================================================================================
8. 利用 bash for 循环打印下面这句话中字母数不大于 6 的单词(某企业面试真
题)。 I am oldboy teacher welcome to oldboy training class
#!/bin/bash
for n in I am oldboy teacher welcome to oldboy training class
do
if [ `echo $n|wc -L` -le 6 ]
then
echo $n
fi
done
=======================================================================================
9.已知下面的字符串是通过将RANDOM随机数
采用md5sum加密后任意取出连续10位的结果,
请破解这些字符串对应的md5sum前的数字?
4fe8bf20ed
#!/bin/bash
for n in {0..32767}
do
echo -n "$n " >>/tmp/md5sum.txt
echo $n|md5sum >>/tmp/md5sum.txt
done
grep "4fe8bf20ed" /tmp/md5sum.txt
=======================================================================================
10.使用 read 读入方式比较两个整数大小,要求对用户输入的内容严格判断是否
为整数,是否输入了两个数字。
#!/bin/bash
read -p "请比较两个数大小:" a b
expr $a + $b + 999 &>/dev/null
if [ $? -ne 0 ]
then
echo "输入两整数啊你输入的是嘛"
exit 1
elif [ $a -gt $b ]
then
echo "$a>$b"
exit 0
elif [ $a -lt $b ]
then
echo "$a<$b"
exit 0
else [ $a -eq $b ]
echo "$a=$b"
exit 0
fi
=======================================================================================
11.菜单自动化软件部署经典案例
综合实例:打印选择菜单,按照选择一键安装不同的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、要对执行的脚本进行相关的条件判断,例如:脚本文件是否存在,是否可执行等判断, 尽量用上前面讲解的知识点。
#!/bin/bash
cat<
========================
1.[install lamp]
2.[install lnmp]
3.[exit]
========================
EOF
read -p "pls input the num you want:" a
expr $a + 999 &>/dev/null
if [ $? -ne 0 ]
then
echo "pls input the num you want:{1|2|3}"
exit 1
fi
if [ $a -eq 1 ]
then
touch /server/scripts/lamp.sh
echo 'echo "lamp is installed"' >/server/scripts/lamp.sh
source /server/scripts/lamp.sh
exit 1
elif [ $a -eq 2 ]
then
echo 'echo "lnmp is installed"' >/server/scripts/lamp.sh
source /server/scripts/lamp.sh
exit 1
elif [ $a -eq 3 ]
then
echo "已退出"
fi
=======================================================================================12.MySQl服务器异常监测
用if条件语句实现对MySQL数据库是否正常的检查,如果服务未启动,则启动相应服务。
#1/bin/bash
if [ `netstat -lntup|grep mysqld|wc -l` -ge 1 ]
then
echo "mysql服务已开启"
else [ `netstat -lntup|grep mysqld|wc -l` -eq 0 ]
echo "mysql服务没开" | tee /tmp/mysql_jinc.log
systemctl start mysqld
fi
=======================================================================================
13.开发Rsync服务启动脚本
#!/bin/bash
. /etc/init.d/functions
function usage(){
echo $"usage:$0 {start|stop|restart}"
exit 0
}
function start(){
rsync --daemon
sleep 2
if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ]
then
action "rsyncd is started." /bin/true
else
action "rsyncd is started." /bin/false
fi
}
function stop(){
killall rsync &>/dev/null
sleep 2
if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ]
then
action "rsyncd is stopped." /bin/true
else
action "rsyncd is stopped." /bin/false
fi
}
function main(){
if [ $# -ne 1 ]
then
usage
fi
if [ "$1" = "start" ]
then
start
elif [ "$1" = "stop" ]
then
stop
elif [ "$1" = "restart" ]
then
stop
sleep 2
start
else
usage
fi
}
main $*
=======================================================================================
14.开发MySQL启动脚本
#!/bin/bash
. /etc/init.d/functions
function usage(){
echo $"usage:$0 {start|stop|restart}"
exit 0
}
function start(){
systemctl start mysqld
sleep 2
if [ `netstat -lntup|grep mysqld|wc -l` -ge 1 ]
then
action "mysqld is started." /bin/true
else
action "mysqld is started." /bin/false
fi
}
function stop(){
systemctl stop mysqld
sleep 2
if [ `netstat -lntup|grep mysqld|wc -l` -eq 0 ]
then
action "mysqld is stopped." /bin/true
else
action "mysqld is stopped." /bin/false
fi
}
function main(){
if [ $# -ne 1 ]
then
usage
fi
if [ "$1" = "start" ]
then
start
elif [ "$1" = "stop" ]
then
stop
elif [ "$1" = "restart" ]
then
stop
sleep 2
start
else
usage
fi
}
main $*
=======================================================================================
15.破解RANDOM随机数
已知下面字符串是RANDOM随机数变量经过md5sum处理后,在截取一部分连续字符串的结果,请破解这些字符串在使用md5sun处理前所未有的数字
21029299
00205d1c
a3da1677
1f6d12dd
890684b
#!/bin/sh
array=( #<==把待比较的字符串信息放到数组里。
21029299
00205d1c
a3da1677
1f6d12dd
890684b
)
Path=/tmp/md5.txt
funGetMd5() {
> $Path
for ((Num=0;Num<=32767;Num++))
do
{
Stat=$(echo $Num|md5sum)
echo "$Stat $Num" >> $Path #<==建立数字和md5sum后的内容对应关系。
}&
done
}
funFindMd5() {
word=$(echo "${array[@]}"|sed -r 's# |\n#|#g') #<==取出所有数组元素并用|分隔开。
grep --color=auto -E "$word" $Path #<==同时过滤包含所有不同字符串的内容。
}
funcMain(){
funGetMd5
funFindMd5
}
funcMain
===========================================================================================================
16.批量检查多个网站地址是否正常
企业面试题:批量检查多个网站地址是否正常
要求:
1、使用shell数组方法实现,检测策略尽量模拟用户访问。
2、每 10秒钟做一次所有的检测,无法访问的输出报警。
3、待检测的地址如下
http://blog.oldboyedu.com
http://www.baidu.com
http://oldboy.blog.51cto.com
http://10.0.0.7
#!/bin/bash
. /etc/init.d/functions
ch=0
url=(
http://blog.oldboyedu.com
http://www.baidu.com
http://oldboy.blog.51cto.com
http://10.0.0.7
)
function wait(){
echo -n '3秒后,执行检查URL操作'
for ((i=0;i<3;i++))
do
echo -n "."
sleep 1
done
echo
}
function ch_url(){
wait
for usr in ${url[*]}
do
wget -q -o /dev/null -T 13 --tries=2 --spider $usr &>/dev/null
if [ $? -eq 0 ]
then
action "$usr" /bin/true
else
action "$usr" /bin/false
fi
done
((ch++))
}
main(){
while true
do
ch_url
echo "-----------check count :${ch}-------------"
sleep 10
done
}
main
===========================================================================================================
17.单词及字母去重排序案例
用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.
方法1:
#!/bin/bash
px="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."
zimu(){
for n in `echo $px|sed -e 's#[^0-Z ]##g'`
do
for ((i=0;i<${#n};i++))
do
echo ${n:i:1}
done
done
}
danci(){
for n in `echo $px|sed 's#[^0-Z ]##g'`
do
echo $n
done
}
main(){
danci|sort|uniq -c|sort -n -r
echo ======================
zimu|sort|uniq -c|sort -n -r
}
main
其他方法
创建个文件xxx把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. 加进去
tr ",. " "\n" tr ",. " "\n" awk -F"[,. ]+" '{for(i=1;i<=NF;i++) S[$i]++}END{for(k in S) print S[k],k}' 17.txt|sort -nr sed 's# #\n#g;s#,#\n#g' danci.txt |sort |uniq -c|sort -nr sed 's# #\n#g;s#,#\n#g' danci.txt |awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -nr =========================================================================================================== 18.通过脚本传参的方式,检查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 else check_url $1 fi } main $* [root@web02 /server/scripts/kaoshi]# sh 5.sh www.baidu.com www.baidu.com is ok [ 确定 ] =========================================================================================================== 19.猜数字游戏。首先让系统随机生成一个数字,给这个数字定一个范围(1-60), 让用户输入猜的数字,对输入进行判断,如果不符合要求,就给予高或低的提示,猜对后则 给出猜对用的次数。 #!/bin/bash toutal=0 num=$((RANDOM%61)) echo "数字结果是$num" echo "==========================" usleep 100000 clear cat< ===================== | 猜猜0~60的数字 | ===================== EOF epple(){ read -p "请输入你猜的数字:" pa expr $pa + 1 &>/dev/null if [ $# -eq 0 ] then echo "别逗我了,快猜数字" exit fi } apple(){ read -p "请输入你猜的数字:" price expr $price + 1 &>/dev/null if [ $? -ne 0 ] then echo "别逗我了,快猜数字" apple fi } guess(){ ((total++)) if [ $price -eq $num ] then echo "猜对了就是$num" if [ $total -le 3 ] then echo "一共猜了$total次,厉害啊年轻人。" elif [ $total -gt 3 -a $total -le 6 ] then echo "一共猜了$total次,猜了这么多次,不行啊" elif [ $total -gt 6 ] then echo "一共猜了$total次,猜了这么多次,真废物" fi exit 0 elif [ $price -gt $num ] then echo "哈哈,没有这么大" echo "再给你一次机会,请继续猜:" apple elif [ $price -lt $num ] then echo "太小了,太小了" echo "再给你一次机会,请继续猜:" apple fi } main(){ apple while true do guess done } main =========================================================================================================== 20.计算从1 加到100之和(要求用for 和while,至少给出两种方法)。 方法一: [root@web02 /server/scripts/kaoshi]# cat 8.sh sum=0 i=1 for i in `seq 1 100` do sum=$(($sum+i)) i=$(($i+1)) done echo $sum 方法二: #!/bin/bash sum=0 for ((n=0;n<=100;n++)) do ((sum=sum+n)) done echo $sum 方法三: #!/bin/bash sum=0 for n in `seq 1 100` do sum=$[$sum+$n] done echo $sum 方法四: #!/bin/bash i=0 n=1 while [ $n -lt 101 ] do i=$[$n+$i] n=$[$n+1] done echo $i =========================================================================================================== 21.已知:/etc/hosts的内容为 192.168.1.11 oldboy11 192.168.1.21 oldboy21 192.168.1.31 oldboy31 请用shell脚本实现,怎么才能在输入IP后找到/etc/hosts里对应的唯一的hostname? 解答: #!/bin/bash cat>> /etc/hosts < 192.168.1.11 oldboy11 192.168.1.21 oldboy21 192.168.1.31 oldboy31 EOF if [ $# -ne 1 ] then echo "Usage: $0 + IP" exit 1 fi read -p "在输入IP后找到/etc/hosts里对应的唯一的hostname:" a if [ $a == "192.168.1.11" ] then echo oldboy11 elif [ $a == "192.168.1.21" ] then echo oldboy21 elif [ $a == "192.168.1.31" ] then echo oldboy31 fi =========================================================================================================== 22.请教一个问题 at 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 方法一: 最简单的方法是 #!/bin/bash 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 EOF cat oldboy.txt| awk -F "[/?]+" '{print $1 $3}' 方法2: #!/bin/bash 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
===========================================================================================================