企业真实shell面试题,一起来挑战把!
声明:如有雷同,纯属抄袭
1、批量创建系统用户oldboy01-oldboy10,并随机生成密码
[root@xiaoya ~]# cat /server/scripts/user_add.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function useradd
# version 4.1.2
A=oldboy
for n in {01..10}
do
useradd $A$n -s /bin/bash
passwd=$(echo $RANDOM|md5sum|cut -c 3-10)
echo $passwd|passwd --stdin $A$n
echo $A$n >>~/oldboy.txt
echo $passwd >>~/oldboy.txt
done
###########批量删除系统用户#############
[root@xiaoya ~]# cat /server/scripts/user_del.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function delete user from os
# version 4.1.2
for n in {01..10}
do
id oldboy$n &>/dev/null
if [ $? -eq 0 ]
then
userdel -r oldboy$n
echo "delete oldboy$n successful"
else
echo "oldboy$n is not exit"
fi
> ~/oldboy.txt
done
2、写一个脚本,实现判断10.0.0.0/24网段里,有哪些用户的IP在使用
[root@xiaoya scripts]# cat ping.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function delete user from os
# version 4.1.2
. /etc/init.d/functions
echo "##########`date` start##########">>~/true.txt
for n in {1..254}
do
ping -c 1 -w 1 10.0.0.$n &>/dev/null
if [ $? -eq 0 ]
then
action "10.0.0.$n is exit" /bin/true >>~/true.txt
fi
done
echo "##########`date` stop###########">>~/true.txt
##此脚本可以记录脚本的执行时间,贴图为证
[root@xiaoya ~]# cat true.txt
##########2016年 01月 07日 星期四 17:08:30 CST start##########
10.0.0.1 is exit [确定]
10.0.0.2 is exit [确定]
10.0.0.4 is exit [确定]
##########2016年 01月 07日 星期四 17:12:42 CST stop###########
3、开发mysql多实例启动脚本:
已知mysql多实例启动命令为:mysqld_safe--defaults-file=/data/3306/my.cnf &
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown
[root@xiaoya scripts]# cat mysql.sh
#!/bin/sh
#########################################################
# functions this scripts is created by tom at 2016-01-09
# author tom
# QQ 402753795
# blog http://402753795.blog.51cto.com/
# version 4.1.2
#########################################################
. /etc/init.d/functions
#no.1
function_start_mysql()
{
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases;" &>/dev/null
if [ $? -eq 0 ]
then
echo "Mysql is running......" && exit 0
else
/application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf &>/dev/null &
action "Starting mysql......" /bin/true
exit 0
fi
}
#no.2
function_stop_mysql()
{
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases;" &>/dev/null
if [ $? -eq 0 ]
then
/application/mysql/bin/mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown
action "Stopping mysql......" /bin/true
#exit 0
else
echo "mysql is stopped......"
#exit 0
fi
}
#no.3
function_restart_mysql()
{
#mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases;" &>/dev/null
#if [ $? -eq 0 ]
#then
function_stop_mysql
sleep 2
function_start_mysql
#else
# function_start_mysql
#fi
}
#no.4
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
echo "PLS INPUT start|stop|restart"
esac
4、用脚本实现对mysql进行分库备份
[root@xiaoya scripts]# cat mysqldump_database.sh
#!/bin/sh
USER=root
PASSWD=123456
SOCKET=/data/3306/mysql.sock
LOGIN="mysql -u$USER -p$PASSWD -S $SOCKET"
DUMP="mysqldump -u$USER -p$PASSWD -S $SOCKET"
DATABASE=$($LOGIN -e "show databases;"|egrep -v "*chema|mysql|test"|sed '1d')
[ -d /backup ]||mkdir /backup -p
for database in $DATABASE
do
##[ -d /backup ]||mkdir /backup -p
$DUMP --events -B $database |gzip >/backup/$(date +%F)_${database}_mysqldump.gz
done
5、用脚本实现对mysql进行分库、分表备份
[root@xiaoya scripts]# cat mysqldump_table.sh
#!/bin/sh
USER=root
PASSWD=123456
SOCKET=/data/3306/mysql.sock
LOGIN="mysql -u$USER -p$PASSWD -S $SOCKET"
DUMP="mysqldump -u$USER -p$PASSWD -S $SOCKET"
DATABASE=$($LOGIN -e "show databases;"|egrep -v "*chema|mysql|test"|sed '1d')
for database in $DATABASE
do
#TABLE=$(mysql -uroot -p123456 -S /data/3306/mysql.sock -e "use $database;show tables"|sed '1d')
[ -d /backup/$database ]||mkdir /backup/$database -p
for table in $TABLE
do
TABLE=$(mysql -uroot -p123456 -S /data/3306/mysql.sock -e "use $database;show tables"|sed '1d')
$DUMP --events -B $database $table|gzip >/backup/$database/$(date +%F)_${table}_mysqldump.gz
done
done
6、用for循环打印下面字母数大于6的单词(至少两种方法实现)
I am oldboy teacher welcome to oldboy training class.
[root@xiaoya ~]# cat /server/scripts/print_word.sh
#!/bin/sh
for n in I am oldboy teacher welcome to oldboy training class
do
if [ `echo $n|wc -L` -gt 6 ]
then
echo $n
fi
done
[root@xiaoya ~]# cat /server/scripts/print_word1.sh
#!/bin/sh
for n in I am oldboy teacher welcome to oldboy training class
do
if [ `echo ${#n}` -gt 6 ]
then
echo $n
fi
done
7、用shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。
[root@xiaoya scripts]# cat read.sh
#!/bin/sh
read -p "Pls input two int :" a b
#no.1
if [ -z $a -o -z $b ]
then
echo "error: plr input two num"
exit 1
fi
#no.2
expr $a + $b + 2 &>/dev/null
if [ $? -ne 0 ]
then
echo "error: input must be int"
exit 2
fi
#no.3
echo "$a-$b =$(( $a - $b ))"
echo "$a+$b =$(( $a + $b ))"
echo "$a*$b =$(( $a * $b ))"
echo "$a/$b =$(( $a / $b ))"
echo "$a**$b =$(( $a ** $b ))"
echo "$a%$b =$(( $a % $b ))"
8、打印选择菜单
[root@oldboyscripts]# sh menu.sh
1.[install lamp]
2.[install lnmp]
3.[exit]
pls input the num you want:
要求:
1)、当用户输入1时,输出“startinstalling lamp.”然后执行/server/scripts/lamp.sh,脚本内容输出"lampis installed"后退出脚本;
2)、当用户输入2时,输出“startinstalling lnmp.”然后执行/server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本;
3)、当输入3时,退出当前菜单及脚本;
4)、当输入任何其它字符,给出提示“Input error”后退出脚本。
5)、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。
[root@xiaoya scripts]# cat menu.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function menu
# version 4.1.2
echo "1.[install lamp]"
echo "2.[install lnmp]"
echo "3.[exit]"
read -p "pls input the num you want :" num
case $num in
1)
if [ -f /server/scripts/lamp.sh -a -x /server/scripts/lamp.sh ]
then
/bin/sh /server/scripts/lamp.sh &
echo "lamp is installed"
else
echo "something is error" && exit 1
fi
;;
2)
if [ -f /server/scripts/lnmp.sh -a -x /server/scripts/lnmp.sh ]
then
echo "start installing lnmp"
/bin/sh /server/scripts/lnmp.sh
#echo "lnmp is installed"
else
echo "something is error" && exit 2
fi
;;
3)
exit 0
;;
esac
9、监控mysql服务器是否运行正常,要求间隔1分钟,持续监控。
[root@xiaoya scripts]# cat check_mysql.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function check_mysql_slave_status
# version 4.1.2
NUM=$(ss -lntup|grep mysql|wc -l)
while true
do
if [ $NUM -eq 1 ]
then
echo "mysql is running ok"
else
echo "mysql is not running " >>~/check_mysql_status.txt
mail -s "check_mysql_status" [email protected] <~/check_mysql_status.txt
exit 1
fi
sleep 2
done
10、面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)。
[root@xiaoya scripts]# cat md5_html_www.sh
#!/bin/sh
#no.1
find /var/html/www/ -type f |xargs md5sum >/var/html/check_md5sum.txt
#no.2
if [ `ls /var/html/www|wc -l` -ne 10 ]
then
echo "`ls|egrep -v "*oldboy*"` is not normal" >/var/html/Result.txt
mail -s "check_html_www" [email protected] < /var/html/Result.txt
exit 1
fi
#no.3
md5sum -c /var/html/check_md5sum.txt &>/dev/null
if [ $? -ne 0 ]
then
NUM=$(md5sum -c /var/html/check_md5sum.txt 2>/dev/null|egrep "FAILED"|cut -d":" -f1)
echo "$NUM may be change" > /var/html/Result.txt
mail -s "check_html_www" [email protected] < /var/html/Result.txt
exit 1
fi
11、企业案例:写网络服务独立进程模式下rsync的系统启动脚本
例如:/etc/init.d/rsyncd{start|stop|restart} 。
要求:
1.要使用系统函数库技巧。
2.要用函数,不能一坨SHI的方式。
3.可被chkconfig管理。
[root@xiaoya init.d]# cat mysql
#!/bin/sh
#########################################################
# functions this scripts is created by tom at 2016-01-09
# author tom
# QQ 402753795
# blog http://402753795.blog.51cto.com/
# version 4.1.2
#########################################################
. /etc/init.d/functions
# chkconfig: 2345 56 11
# description: Mysql's start file
#no.1
function_start_mysql()
{
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases;" &>/dev/null
if [ $? -eq 0 ]
then
echo "Mysql is running......" && exit 0
else
/application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf &>/dev/null &
action "Starting mysql......" /bin/true
exit 0
fi
}
#no.2
function_stop_mysql()
{
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases;" &>/dev/null
if [ $? -eq 0 ]
then
/application/mysql/bin/mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown
action "Stopping mysql......" /bin/true
#exit 0
else
echo "mysql is stopped......"
#exit 0
fi
}
#no.3
function_restart_mysql()
{
#mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases;" &>/dev/null
#if [ $? -eq 0 ]
#then
function_stop_mysql
sleep 2
function_start_mysql
#else
# function_start_mysql
#fi
}
#no.4
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
echo "PLS INPUT start|stop|restart"
esac
[root@xiaoya init.d]# chkconfig --add mysql
[root@xiaoya init.d]# chkconfig --list mysql
[root@xiaoya init.d]# ll /etc/rc.d/rc3.d/ |grep mysql
lrwxrwxrwx 1 root root 15 1月 10 14:36 S56mysql -> ../init.d/mysql
[root@xiaoya init.d]# ll /etc/rc.d/rc3.d/ |grep mysql
lrwxrwxrwx 1 root root 15 1月 10 14:37 K11mysql -> ../init.d/mysql