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編程:第四章 生產環境使用腳本案例》


分享到:


相關文章: