Linux | 运维常见面试题——编程与应用题
前篇文章已经分享了,Linux | 运维常见面试题——填空题、Linux | 运维常见面试题——选择题和Linux | 运维常见面试题——简答题,今天继续分享Linux | 运维常见面试题——编程与应用题,编程与应用题还是需要一些编程基础的,还得有逻辑思维能力,创新能力,不怕不知道,就怕想不到,想到了可以先“度娘一下”或者“谷歌一下”,所以继续加油吧少年!!
编程与应用题
1.用Shell
参考程序:
#!/bin/sh
FILENAME=
echo
read FILENAME
if [ -c "$FILENAME" ]
then
cp $FILENAME /dev
fi
2.请下列shell
#!/bin/sh
#
# /etc/rc.d/rc.httpd
#
# Start/stop/restart the Apache web server.
#
# To make Apache start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.httpd
#
case "$1" in
'start')
/usr/sbin/apachectl start ;;
'stop')
/usr/sbin/apachectl stop ;;
'restart')
/usr/sbin/apachectl restart ;;
*)
echo "usage $0 start|stop|restart" ;;
esac
参考答案:
(1)程序注释
#!/bin/sh
#
# /etc/rc.d/rc.httpd
#
# Start/stop/restart the Apache web server.
#
# To make Apache start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.httpd
#
case "$1" in #case
'start') #若位置参数为start
/usr/sbin/apachectl start ;; #启动httpd
'stop') #若位置参数为stop
/usr/sbin/apachectl stop ;; #关闭httpd
'restart') #若位置参数为stop
/usr/sbin/apachectl restart ;; #重新启动httpd
*) #若位置参数不是start、stop
echo "usage $0 start|stop|restart" ;; #显示命令提示信息:程序的调用方法
esac #case
(2)程序的功能是启动,停止或重新启动httpd
(3)程序的调用方式有三种:启动,停止和重新启动。
3.设计一个shell
参考答案:
#!/bin/sh
i=1
groupadd class1
while [ $i -le 30 ]
do
if [ $i -le 9 ] ;then
USERNAME=stu0${i}
else
USERNAME=stu${i}
fi
useradd $USERNAME
mkdir /home/$USERNAME
chown -R $USERNAME /home/$USERNAME
chgrp -R class1 /home/$USERNAME
i=$(($i+1))
done
4.编写shell
参考程序:
#!/bin/sh
i=1
while [ $i -le 50 ]
do
userdel -r stud${i}
i=$(($i+1 ))
done
5.某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决 方案 :
(1)在下午4 :50
(2)从早8:00~下午6:00
加入到/backup
(3)每逢星期一下午5:50
backup.tar.gz;
(4)在下午5:55
(5)在早晨8:00
参考答案:
解决方案:
(1)用vi
prgx
50 16 * * * rm -r /abc/*
(2)、0 8-18/1 * * * cut -f1 /xyz/x1 >;>; /backup/bak01.txt
(3)、50 17 * * * tar zcvf backup.tar.gz /data
(4)、55 17 * * * umount /dev/hdc
(5)、由超级用户登录,用crontab
root@xxx:#crontab prgx;在每日早晨8:00
---------------------------------------
-------
6.设计一个shell
参考答案:
(1)编写shell
#!/bin/sh
DIRNAME=`ls /root | grep bak`
if [ -z "$DIRNAME" ] ; then
mkdir /root/bak
cd /root/bak
fi
YY=`date +%y`
MM=`date +%m`
DD=`date +%d`
BACKETC=$YY$MM$DD_etc.tar.gz
tar zcvf $BACKETC /etc
echo "fileback finished!"
(2)编写任务定时器:
echo "0 0 1 * * /bin/sh /usr/bin/fileback" >; /root/etcbakcron
crontab /root/etcbakcron
或使用crontab -e
0 1 * * * /bin/sh /usr/bin/fileback
7.有一普通用户想在每周日凌晨零点零分定期备份/user/backup
参考答案:(1)第一种方法:
用户应使用crontab
0 0 * * sun cp –r /user/backup /tmp
(2)第二种方法:
用户先在自己目录下新建文件file,文件内容如下:
0 * * sun cp –r /user/backup /tmp
然后执行
8.设计一个Shell
参考答案:
#!/bin/sh
i=1
while [ i -le 50 ]
do
if [ -d /userdata ];then
mkdir -p -m 754 /userdata/user$i
立目录
#chmod 754 /userdata/user$i
echo "user$i"
let "i = i + 1"
else
mkdir /userdata
mkdir -p -m /userdata/user$i
#chmod 754 /userdata/user$i
echo "user$i"
let "i = i + 1"
fi
done
9.有文件file
[root@xuegod63 ~]# cat file
aaaaaa
bbbbbbbb
cccccccabc
查询file里面空行的所在行号
awk
or
grep -n ^$ file |awk
查询file1
grep abc$ file1
打印出file1
sed -n '1,3p'
head -3 file
10.编写个shell
有时候我们发现一些新鲜事物,觉得很难,就远远的看着,如果你用心的去感受它,其实也就这么回事。 不要高估别人,低估自己,其实深入内心,很多你自以为很了不起的人,其实也没什么,真是这样。
您阅读这篇文章共花了: