shell常见问题

上传人:人*** 文档编号:546479317 上传时间:2023-11-23 格式:DOC 页数:29 大小:52.63KB
返回 下载 相关 举报
shell常见问题_第1页
第1页 / 共29页
shell常见问题_第2页
第2页 / 共29页
shell常见问题_第3页
第3页 / 共29页
shell常见问题_第4页
第4页 / 共29页
shell常见问题_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《shell常见问题》由会员分享,可在线阅读,更多相关《shell常见问题(29页珍藏版)》请在金锄头文库上搜索。

1、非常好的十道Linuxshell脚本面试题1、编写个shell脚本将当前目录下大于10K的文件转移到/tmp目录下#/bin/sh#Programm :# Using for move currently directory to /tmpfor FileName in ls -l | awk $510240 print $9domv $FileName /tmpdonels -al /tmpecho Done! 2、编写shell脚本获取本机的网络地址。比如:本机的ip地址是:192.168.100.2/255.255.255.0,那么它的网络地址是192.168.100.1/255.255

2、.255.0方法一:#!/bin/bash#This script print ip and networkfile=/etc/sysconfig/network-scripts/ifcfg-eth0if -f $file ;thenIP=grep IPADDR $file|awk -F= print $2 MASK=grep NETMASK $file|awk -F= print $2 echo $IP/$MASKexit 1fi方法二:#!/bin/bash#This programm will printf ip/network#IP=ifconfig eth0 |grep inet |

3、sed s/.*addr:/g|sed s/ Bcast.*$/gNETMASK=ifconfig eth0 |grep inet |sed s/.*Mask:/gecho $IP/$NETMASKexit3、用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下。参考程序:#!/bin/shFILENAME=echo “Input file name:”read FILENAMEif -c $FILENAME thencp $FILENAME /devfi4请为下列shell程序添加注释,并说明程序的功能和调用方法:#!/bin/sh# /etc/rc.d/rc.

4、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 instart)/usr/sbin/apachectl start ;stop)/usr/sbin/apachectl stop ;restart)/usr/sbin/apachectl restart ;*)echo usage $0 start|stop|restart ;es

5、ac参考答案:(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结构开始,判断“位置参数”决定执行的操作。本程序携带一个“位置参数”,即$1start) #若位置参数为start/usr/sb

6、in/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)程序的调用方式有三种:启动,停止和重新启动。5设计一个s

7、hell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。参考答案:#!/bin/shi=1groupadd class1while $i -le 30 doif $i -le 9 ;thenUSERNAME=stu0$ielseUSERNAME=stu$ifiuseradd $USERNAMEmkdir /home/$USERNAMEchown -R $USERNAME /home/$USERNAMEchgrp -R class1 /home/$USERNAMEi=$($i+1)done6编写shell程序,实现自动删除50个账

8、号的功能。账号名为stud1至stud50。参考程序:#!/bin/shi=1while $i -le 50 douserdel -r stud$ii=$($i+1 )done7某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决 方案 :(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接口的C

9、D-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文件中的内容:rootxxx:#crontab prg

10、x;在每日早晨8:00之前开机后即可自动启动crontab。8设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下。参考答案:(1)编写shell程序fileback:#!/bin/shDIRNAME=ls /root | grep bakif -z $DIRNAME ; thenmkdir /root/bakcd /root/bakfiYY=date +%yMM=date +%mDD=date +%dBACKETC=

11、$YY$MM$DD_etc.tar.gztar zcvf $BACKETC /etcecho fileback finished!(2)编写任务定时器:echo 0 0 1 * * /bin/sh /usr/bin/fileback ; /root/etcbakcroncrontab /root/etcbakcron或使用crontab -e 命令添加定时任务:0 1 * * * /bin/sh /usr/bin/fileback9有一普通用户想在每周日凌晨零点零分定期备份/user/backup到/tmp目录下,该用户应如何做?参考答案:(1)第一种方法:用户应使用crontab e 命令创

12、建crontab文件。格式如下:0 0 * * sun cp r /user/backup /tmp(2)第二种方法:用户先在自己目录下新建文件file,文件内容如下:0 * * sun cp r /user/backup /tmp然后执行 crontab file 使生效。10设计一个Shell程序,在/userdata目录下建立50个目录,即user1user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。参考答案: 建立程序 Pro16如下:#!/bin/shi=1while i -le 50 doif -d /userdata ;thenmkdir -p -m 754 /userdata/user$i 加上-m 754 就不用写下面那一句了 -p 是递归建立目录#chmod 754 /userdata/user$iecho user$ilet i = i + 1 (或i=$($i+1)elsemkdir /userdatamkdir -p -m /userdata/user$i#chmod 754 /userdata/user$iecho user$ilet i = i + 1 (或i=$($i1)fidone1) 如何向脚本传递参数 ?./script argument

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号