前篇文章已经分享了,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
[root@xuegod63 ~]# cat test.sh
#!/bin/bash
cd /boot/grub
for file in `ls /boot/grub`
#for file in `ls /boot/grub`
do
if [ -f $file ]; then
if [ `ls -l $file|awk '{print $5}'` -gt 10000 ]; then
mv $file /opt/
fi
fi
done
11.有个文件如下:
http://a.domain.com/1.html
http://b.domain.com/1.html
http://c.domain.com/1.html
http://a.domain.com/2.html
http://b.domain.com/2.html
http://a.domain.com/3.html
要求:得到主机名(和域名),并统计哪个网址出现的次数,并排序。可以shell
得到的结果应该是:
3 a.domain.com
2 b.domain.com
1 c.domain.com
[root@mail ~]# awk
arr[i],i}‘
3 a.domain.com
2 b.domain.com
1 c.domain.com
12.如果得到随机的字串,长度和字串中出现的字符表可定义,并将字串倒序显示,如把0123456789
{srand();str="0123456789";len=length(str);for(i=count;i>0;i--)
marry[i]=substr(str,int(rand()*len),1);for(i=count;i>0;i--)
printf("%c",marry[i]);printf("\n");for
(i=0;i<=count;i++) printf("%c",marry[i]);printf("\n")}'
838705
507838
13.有10
#测试机器:虚拟机Linux as 4
#1.首先建立服务器间的信任关系。拿两台机器做测试
本机ip:192.168.1.6
[root@codfei ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y (以为我是第2
Enter passphrase (empty for no passphrase):(直接回车无须输入密钥)
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
04:37:13:2a:4b:10:af:c1:2b:03:3f:6b:27:ce:b9:62 root@codfei
[root@codfei ~]# cd .ssh/
[root@codfei .ssh]# ll
-rw------- 1 root root 883 Apr 25 17:51 id_rsa
-rw-r--r-- 1 root root 221 Apr 25 17:51 id_rsa.pub
-rw-r--r-- 1 root root 442 Apr 25 17:37 known_hosts
id_rsa
[root@codfei .ssh]# scp id_rsa.pub192.168.1.4:/root/.ssh/192.168.1.6
root@192.168.1.4's password:
id_rsa.pub 100% 221 0.2KB/s 00:00
这里把公钥文件取名为本机的ip
现在登陆到192.168.1.4
[root@codfei ~]# cd .ssh/
[root@codfei .ssh]# cat 192.168.1.6 >> authorized_keys
然后回到192.168.1.6
[root@codfei .ssh]# ssh 192.168.1.4
Last login: Wed Aug 8 12:14:42 2007 from 192.168.1.6
这样就可以了,里面偶尔涉及到权限问题。一般./ssh
####脚本如下#######################
#!/bin/bash
#SCRIPT:df_check.sh
#Writeen by codfei Mon Sep 3 07:25:28 CST 2007
#PURPOSE:This script is used to monitor for full filesystems.
#######################Begining####################
####################
FSMAX="80"
remote_user='root' #####完全可以不用root
remote_ip=(192.168.1.5 192.168.1.6 192.168.1.7 192.168.1.8 192.168.1.9
192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 ) ---->
这里填写你要监控的主机ip
ip_num='0'
while [ "$ip_num" -le "$(expr ${#remote_ip[@]} - 1)" ]
do
read_num='1'
ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp
grep '^/dev/*' /tmp/diskcheck_tmp|awk '{print $5}'|sed 's/\%//g' >
/tmp/diskcheck_num_tmp
while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ]
do
size=$(sed -n "$read_num"'p' /tmp/diskcheck_num_tmp)
if [ "$size" -gt "$FSMAX" ]
then
$(grep '^/dev/*' /tmp/diskcheck_tmp|sed -n $read_num'p' >
/tmp/disk_check_mail)
$(echo ${remote_ip[$ip_num]} >> /tmp/disk_check_mail)
$(mail -s "diskcheck_alert" admin < /tmp/disk_check_mail)
fi
read_num=$(expr $read_num + 1)
done
ip_num=$(expr $ip_num + 1)
done
#############over################################
################让脚本每十分钟执行一次#############
在cron
0/10 * * * * /home/codfei/diskcheck.sh 2>&1
################################################
##########################
14.如何统计apache
tail access_log | awk '{print $1,$4}'
[root@localhost logs]# grep -c `date -d '3 second ago' +%T` access_log
0
15.将一个文本的奇数行和偶数行合并,第2
[root@localhost bin]# cat 1
48 Oct 3bc1997 lpas 68.00 lvx2a 138
484 Jan 380sdf1 usp 78.00 deiv 344
483 nov 7pl1998 usp 37.00 kvm9d 644
320 aug der9393 psh 83.00 wiel 293
231 jul sdf9dsf sdfs 99.00 werl 223
230 nov 19dfd9d abd 87.00 sdiv 230
219 sept 5ap1996 usp 65.00 lvx2c 189
216 Sept 3zl1998 usp 86.00 kvm9e 234
[root@localhost bin]# sed '$!N;s/\n/ /g' 1
48 Oct 3bc1997 lpas 68.00 lvx2a 138 484 Jan 380sdf1 usp 78.00 deiv 344
483 nov 7pl1998 usp 37.00 kvm9d 644 320 aug der9393 psh 83.00 wiel 293
231 jul sdf9dsf sdfs 99.00 werl 223 230 nov 19dfd9d abd 87.00 sdiv 230
219 sept 5ap1996 usp 65.00 lvx2c 189 216 Sept 3zl1998 usp 86.00 kvm9e 234
[root@localhost bin]# sed -n -e 2p -e 3p 1|sed '$!N;s/\n/ /'
484 Jan 380sdf1 usp 78.00 deiv 344 483 nov 7pl1998 usp 37.00 kvm9d 644
16.自动ftp
#!/bin/sh
ftp -n<<END_FTP
open 192.168.1.4
user codfei duibuqi //用户名codfei
binary
prompt off //关闭提示
mput test //上传test
close
bye
END_FTP
自动ssh
#!/usr/bin/expect -f
set timeout 30
spawn ssh codfei@B
expect "password:"
send "pppppp\r"
expect "]*"
send "ssh codfei@C\r"
expect "password:"
send "pppppp\r"
interact
17.从a.log
egrep 'WARNING|FATAL' a.log|grep -v'IGNOR'|awk -F: '{print$5}'egrep 'WARNING|FATAL' a.log|grep -v'IGNOR'|awk -F: '{print$5}'
18.添加一个新组为class01,然后,添加属于这个组的30
#!/bin/bash
groupadd class1
for ((i=1;i<=30;i++))
do
if [ $i -lt 10 ];then
username="std0"$i
else
username="std"$i
fi
useradd -G class1 $username
done#!/bin/bash
groupadd class1
for ((i=1;i<=30;i++))
do
if [ $i -lt 10 ];then
username="std0"$i
else
username="std"$i
fi
useradd -G class1 $username
done
19.在每个月的第一天备份并压缩/etc
crottab -e
00
# vi /usr/bin/fileback
#!/bin/bash
cd /tmp
tar zcvf `date +%Y%m%d`_etc
mv `date +%Y%m%d`_etc
20.用shell
参考答案:
#!/bin/bash
directory=/dev
for file in anaconda-ks.cfg install.log install.log.syslog
do
if [ -f $file ]
then
cp $file $directory/$file.bak
echo " HI, $LOGNAME $file is backed up already in $directory !!"
fi
done
20.某系统管理员需要每天做一定的重复工作,编制一个解决方案:
(1).从下午4:50
(2).从早上8:00~下午6:00
(3).每逢周一下午5:50
(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
21.使用shell,建立class1
vi autoaddusr
#!/usr/bin/php -q
- 补充vim常用快捷方式
dd 删除当前行
G 移动到文章末行
gg移动到文本首行
x 编辑后保存推出,相对于wq!
shift + $ 删除当前位置到行末
shift + G 删除当前位置到文本结束[/hide]
有时候我们发现一些新鲜事物,觉得很难,就远远的看着,如果你用心的去感受它,其实也就这么回事。 不要高估别人,低估自己,其实深入内心,很多你自以为很了不起的人,其实也没什么,真是这样。