Linux编程与应用

本文详细介绍了多个Linux Shell脚本的实例,包括文件类型判断、Apache服务器管理、用户及组管理、定时任务设置、文件备份压缩、文件操作以及日志分析等。通过这些实例,读者可以深入理解Shell编程在日常系统管理中的应用。
摘要由CSDN通过智能技术生成

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 ile9];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 ile50]douserdelrstud{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 程序,在每

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值