1.用Shell 编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下。
参考程序:
#!/bin/sh
FILENAME=
echo ―Input file name:‖
read FILENAME
if [ -c “$FILENAME” ]
then
cp $FILENAME /dev
fi
2.请下列shell 程序加注释,并说明程序的功能和调用方法:#!/bin/sh
#!/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 定义实用的shell
#/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 结构开始,判断―位置参数‖决定执行的操作。本程序携带一个―位置参数‖,即$1
‘start’) #若位置参数为start
/usr/sbin/apachectl start ;; #启动httpd 进程
‘stop’) #若位置参数为stop
/usr/sbin/apachectl stop ;; #关闭httpd 进程
‘restart’) #若位置参数为stop
/usr/sbin/apachectl restart ;; #重新启动httpd 进程
*) #若位置参数不是start、stop 或restart 时
echo “usage $0 start|stop|restart” ;; #显示命令提示信息:程序的调用方法
esac #case 结构结束
(2)程序的功能是启动,停止或重新启动httpd 进程
(3)程序的调用方式有三种:启动,停止和重新启动。
3.设计一个shell 程序,添加一个新组为class1,然后添加属于这个组的30 个用户,用户名的形式为stdxx,其中xx 从01到30。
参考答案:
#!/bin/sh
i=1
groupadd class1
while [ $i -le 30 ]
do
if [ i − l e 9 ] ; t h e n U S E R N A M E = s t u 0 i -le 9 ] ;then USERNAME=stu0 i−le9];thenUSERNAME=stu0{i}
else
USERNAME=stu${i}
fi
useradd U S E R N A M E m k d i r / h o m e / USERNAME mkdir /home/ USERNAMEmkdir/home/USERNAME
chown -R U S E R N A M E / h o m e / USERNAME /home/ USERNAME/home/USERNAME
chgrp -R class1 /home/ U S E R N A M E i = USERNAME i= USERNAMEi=(($i+1))
done
4.编写shell 程序,实现自动删除50 个账号的功能。账号名为stud1 至stud50。
参考程序:
#!/bin/sh
i=1
while [ i − l e 50 ] d o u s e r d e l − r s t u d i -le 50 ] do userdel -r stud i−le50]douserdel−rstud{i}
i= ( ( (( ((i+1 ))
done
5.某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决 方案 :
(1)在下午4 :50 删除/abc 目录下的全部子目录和全部文件;
(2)从早8:00~下午6:00 每小时读取/xyz 目录下x1 文件中每行第一个域的全部数据
加入到/backup 目录下的bak01.txt 文件内;
(3)每逢星期一下午5:50 将/data 目录下的所有目录和文件归档并压缩为文件:
backup.tar.gz;
(4)在下午5:55 将IDE 接口的CD-ROM 卸载(假设:CD-ROM 的设备名为hdc);
(5)在早晨8:00 前开机后启动。
参考答案:
解决方案:
(1)用vi 创建编辑一个名为prgx 的crontab 文件;
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 执行 prgx 文件中的内容:
root@xxx:#crontab prgx;在每日早晨8:00 之前开机后即可自动启动crontab。
---------------------------------------
-------
6.设计一个shell 程序,在每