面试常用脚本


1、使用shell,建立class1 用户组,再批量建立stu1--stu30 的用户,并指定用户组为
class1。
vi autoaddusr
#!/usr/bin/php -q
<?php
exec("groupadd class1");
for($i=1; $i<=30; $i++){
exec("useradd -G class1 stu".$i);
}
?>
chmod +x autoaddusr
./autoaddusr


2..编写个shell 脚本将当前目录下大于10K 的文件转移到/tmp 目录下
#/bin/sh
#Programm :
# Using for move currently directory to /tmp
for FileName in `ls -l |awk '$5>10240 {print $9}'`
do
mv $FileName /tmp
done
ls -al /tmp
echo "Done! "

 

 

将/usr/local/test目录下大于100K 的文件转移到/tmp 目录下。
答:
find /usr/local/test -type f -size +100k -exec mv {} /tmp \;

 

 

 

3.编写shell 脚本获取本机的网络地址。比如:本机的ip 地址是:
192.168.100.2/255.255.255.0,那么它的网络地址是
192.168.100.1/255.255.255.0
方法一:
1. #!/bin/bash
2. #This script print ip and network
3. file="/etc/sysconfig/network-scripts/ifcfg-eth0"
4. if [ -f $file ] ;then
5. IP=`grep "IPADDR" $file|awk -F"=" '{ print $2 }'`
6. MASK=`grep "NETMASK" $file|awk -F"=" '{ print $2 }'`
7. echo "$IP/$MASK"
8. exit 1
9. fi
方法二:
1. #!/bin/bash
2. #This programm will printf ip/network
3. #
4. IP=`ifconfig eth0 |grep 'inet ' |sed 's/^.*addr://g'|sed 's/ Bcast.*$//g'`
5. NETMASK=`ifconfig eth0 |grep 'inet '|sed 's/^.*Mask://g'`
6. echo "$IP/$NETMASK"

 

7. exit

 

 

4.有文件file1
1、请用shell查询file1 里面空行的所在行号
awk ‘{if($0~/^$/)print NR}’ file
or
grep -n ^$ file |awk ‘BEGIN{FS=”:”}{print $1}’

2、编写ShellScript查询file1 以abc 结尾的行
grep abc$ file1

3、打印出file1 文件第1 到第3 行
sed -n ’1,3p’ file1
head -3 file1

 

 

5.编写个shell 脚本将/usr/local/test 目录下大于100K 的文件转移到/tmp 目录下
#!/bin/bash
for file in `ls /root`
do
if [ -f $file ]; then
if [ `ls -l $file|awk '{print $5}'` -gt 10000 ]; then
mv $file /tmp/
fi
fi
done

 

6.有个文件如下:

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 或C。
得到的结果应该是:
3 a.domain.com
2 b.domain.com
1 c.domain.com
[root@mail ~]# awk ‘BEGIN{FS=”/”}{arr[$3]++}END{for(i in arr) print
arr[i],i}’ list| sort -r 答案
3 a.domain.com
2 b.domain.com
1 c.domain.com

 

7.

处理以下文件内容,将域名取出并进行计数排序,如处理:

http://www.baidu.com/index.html
http://www.baidu.com/1.html
http://post.baidu.com/index.html
http://mp3.baidu.com/index.html
http://www.baidu.com/3.html
http://post.baidu.com/2.html

得到如下结果:
域名的出现的次数 域名
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
可以使用bash/perl/PHP/c 任意一种
3、[root@localhost shell]# cat file | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort |
uniq -c | sort -rn
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
[root@codfei4 shell]# awk -F/ '{print $3}' file |sort -r|uniq -c|awk '{print
$1"\t",$2}'
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com

 

 

8.打印第一个域
[root@localhost bin]# cat 3
eqeqedadasdD
eqeqdadfdfDD
fdsfdsfQWEDD
DSADASDSADSA
[root@localhost bin]#
[root@localhost bin]#
[root@localhost bin]# awk -F "" '{print $1}' 3
e
e
f
D

9.用shell 编程,判断一文件是不是字符设备文件,如果是将其拷贝到/dev 目录下?
参考答案:
#!/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

 

10.某系统管理员需要每天做一定的重复工作,编制一个解决方案:
(1).从下午4:50 删除/abc 目录下的全部子目录和全部文件;
(2).从早上8:00~下午6:00 每小时读取/xyz 目录下x1 文件中每行第一个域的全部数
据加入到/backup 目录下的back01.txt 文件内;
(3).每逢周一下午5:50 将/data 目录下的所有目录和文件归档并压缩为文件
backup.tar.gz;
(4).在下午5:55 将IDE 接口的CD-ROM 缷载(假设CD-ROM 的设备名为hdc);
(5).在早上8:00 前开机后启动。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值