02.28 《shell编程:第三章:常见shell面试问题》

Shell面试一般会有些常见的问题。以此来判断你对shell的掌握了解程度。本人总结了一些常见面试题,从面试题可以看出shell不同的功能方向,如有错误欢迎批评指正。


《shell编程:第三章:常见shell面试问题》


1、在shell环境如何查找一个文件test.txt

<code>#find / -name abc.txt/<code>

2、在shell里如何新建一个文件

<code>#touch ~/newfile.txt/<code>

3、脚本编程:求100内的质数

<code>#!/bin/bash
i=1
while [ $i -le 100 ];do
ret=1
for (( j=2;jif [ $(($i%$j)) -eq 0 ];then
ret=0
break
fi
done
if [ $ret -eq 1 ];then
echo -n "$i "
fi
i=$(( i+1 ))
done/<code>

4、晚上11点到早上8点之间每两个小时查看一次系统日期与时间

<code>#echo 1 23,1-8/2 * * * root /tmp/walldate.sh >> /etc/crontab/<code>

5、编写个shell脚本将当前目录下大于10K的文件转移到/tmp目录下

<code>#!/bin/bash
fileinfo=($(du ./*))
length=${#fileinfo[@]}
for((i=0;iif [ ${fileinfo[$i]} -le 10 ];then
mv ${fileinfo[$(( i+1 ))]} /tmp
fi

done/<code>

6、如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.1.1

<code>#/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.1.1:8080
#/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080/<code>

7、在11月份内,每天的早上6点到12点中,每隔2小时执行一次/usr/bin/httpd.sh 怎么实现

<code>#echo "1 6-12/2 * * * root /usr/bin/httpd.sh >> /etc/crontab"/<code>

8、在shell环境如何杀死一个进程

<code>#ps aux | grep | cut -f?   --得到pid
#cat /proc/pid
#kill pid
/<code>

9、把当前目录(包含子目录)下所有后缀为“.sh”的文件后缀变更为“.shell”

<code>#!/bin/bash
str=`find ./ -name \\*.sh`
for i in $str
do
mv $i ${i%sh}shell
done
/<code>

10、编写shell实现自动删除50个账号功能,账号名为stud1至stud50

<code>#!/bin/bash
for((i=1;i<=50;i++));do
userdel stud$i
done
/<code>

11、请用Iptables写出只允许10.1.8.179 访问本服务器的22端口

<code>#/sbin/iptables -A input -p tcp -dport 22 -s 10.1.8.179 -j ACCEPT
#/sbin/iptables -A input -p udp -dport 22 -s 10.1.8.179 -j ACCEPT
#/sbin/iptables -P input -j DROP/<code>

12、有文件file1

<code>#查询file1以abc结尾的行
#grep abc$ file1
#打印出file1文件第1到第三行
head -n3 file1
sed "3q" file1
sed -n "1,3p" file1/<code>

13、1到10数字相加,写出shell脚本

<code>#!/bin/bash
j=0
for((i=1;i<=10;i++));do
j=$[j+i ]
done
echo $j/<code>

14、找出系统内大于50k,小于100k的文件,并删除它们

<code>#!/bin/bash
file=`find / -size +50k -size -100k`
for i in $file;do
rm -rf $i
done/<code>

15、用SHELL模拟LVS

<code>#/sbin/iptable -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.1.11-192.168.1.12/<code>

16、将A 、B、C目录下的文件A1、A2、A3文件,改名为AA1、AA2、AA3.使用shell脚本实现

<code>#!/bin/bash
file=`ls [ABC]/A[123]`
for i in $file;do
mv $i ${i%/*}/A${i#*/}
done/<code>


下一章:《shell编程:第四章 生产环境使用脚本案例》


分享到:


相關文章: