linux运维初级班shell脚本编程考试及参考答案20110523
############################################
#《老男孩linux就业培训中心-初级班第七期课前考试
#shell脚本编程实战模拟考试
#date:2011-5-23
#出题人:老男孩
#QQ:31333741 MAIL:oldboy521@hotmail.com
#blog: http://oldboy.blog.51cto.com
#psite: http://oldboy.cc(即将开放)
############################################
shell脚本编程实战模拟考试题(上机)
一、考试前准备工作:
1.执行yum install httpd -y 安装httpd.
2.检查安装情况
[root@oldboy-B tmp]# rpm -qa httpd
httpd-2.2.3-45.el5.centos.1
3.启动httpd,并检查
[root@oldboy-B tmp]# /etc/init.d/httpd start
启动 httpd:[确定]
[root@oldboy-B tmp]# netstat -lnt|grep 80 #==>检查端口
tcp 0 0 :::80 :::* LISTEN
[root@oldboy-B tmp]# ps -ef|grep httpd#==>检查进程
root 4041 1 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4043 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4044 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4045 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4046 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4047 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4048 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4049 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4050 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
root 4061 3793 0 14:25 pts/0 00:00:00 grep httpd
二、上机考试要求:
1.开发shell脚本/server/scripts/httpdctl,实现通过脚本httpdctl来控制apache服务的启动,停止,
重起.启动时命令要求:httpdctl start|stop|restart
拥有帝国一切,皆有可能。欢迎访问phome.net
提示:需要将httpdctl路径加到系统全局环境变量里.
参考答案:
[root@oldboy-B scripts]# pwd
/server/scripts
a.if语句实现:
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
if [ "$1" = "start" ];then
/etc/init.d/httpd $1 >/dev/null 2>&1
echo "启动 httpd:[确定]"
elif [ "$1" = "stop" ];then
/etc/init.d/httpd $1
elif [ "$1" = "restart" ];then
/etc/init.d/httpd $1 >/dev/null 2>&1
echo "重起 httpd: [确定]"
else
echo "用法: $0 {start|stop|restart}"
exit 1
fi
提示:这个问题用case语句实现效果更佳. 本文因学生还未讲解到case.
[root@oldboy-B scripts]# echo ''export PATH=/server/scripts:$PATH'' >>/etc/profile
#===>将httpdctl路径加到系统全局环境变量里.
[root@oldboy-B scripts]# source /etc/profile
#===>使httpdctl路径加到系统全局环境变量生效
[root@oldboy-B scripts]# which httpdctl
/server/scripts/httpdctl
[root@oldboy-B scripts]# httpdctl stop
停止 httpd:[确定]
[root@oldboy-B scripts]# httpdctl start
启动 httpd:[确定]
[root@oldboy-B scripts]# httpdctl restart
重起 httpd: [确定]
-------------------------------------------------------
b.把相同的路径用变量替换[脚本改进]
[root@oldboy-B |