1、 用sed修改test.txt的23行test爲tset;
sed –i ‘23s/test/tset/g’ test.txt
2、 查看/web.log第25行第三列的內容。
sed –n ‘25p’ /web.log | cut –d “ ” –f3
head –n25 /web.log | tail –n1 | cut –d “ ” –f3
awk –F “ ” ‘NR==23{print $3}’ /web.log
3、 刪除每個臨時文件的最初三行。
sed –i ‘1,3d’ /tmp/*.tmp
4、 腳本編程:求100內的質數。
#!/bin/bash
i=1
while [ $i -le 100 ];do
ret=1
for (( j=2;j<$i;j++ ));do
if [ $(($i%$j)) -eq 0 ];then
ret=0
break
fi
done
if [ $ret -eq 1 ];then
echo -n "$i "
fi
i=$(( i+1 ))
done
5、 晚上11點到早上8點之間每兩個小時查看一次系統日期與時間,寫出具體配置命令
echo 1 23,1-8/2 * * * root /tmp/walldate.sh >> /etc/crontab
6、 編寫個shell腳本將當前目錄下大於10K的文件轉移到/tmp目錄下
#!/bin/bash
fileinfo=($(du ./*))
length=${#fileinfo[@]}
for((i=0;i<$length;i=$(( i+2 ))));do
if [ ${fileinfo[$i]} -le 10 ];then
mv ${fileinfo[$(( i+1 ))]} /tmp
fi
done
7、 如何將本地80端口的請求轉發到8080端口,當前主機IP爲192.168.2.1
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.2.1:8080
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
8、 在11月份內,每天的早上6點到12點中,每隔2小時執行一次/usr/bin/httpd.sh 怎麼實現
echo "1 6-12/2 * * * root /usr/bin/httpd.sh >> /etc/crontab"
9、 在shell環境如何殺死一個進程?
ps aux | grep | cut -f? 得到pid
cat /proc/pid
kill pid
10、 在shell環境如何查找一個文件?
find / -name abc.txt
11、 在shell裏如何新建一個文件?
touch ~/newfile.txt
12、 linux下面的sed和awk的編寫
1) 如何顯示文本file.txt中第二大列大於56789的行?
awk -F "," '{if($2>56789){print $0}}' file.txt
2) 顯示file.txt的1,3,5,7,10,15行?
sed -n "1p;3p;5p;7p;10p;15p" file.txt
awk 'NR==1||NR==3||NR==5||…||NR=15{print $0}' file.txt
3) 將file.txt的製表符,即tab,全部替換成"|"
sed -i "s#\t#\|#g" file.txt
13、 把當前目錄(包含子目錄)下所有後綴爲“.sh”的文件後綴變更爲“.shell”
#!/bin/bash
str=`find ./ -name \*.sh`
for i in $str
do
mv $i ${i%sh}shell
done
14、 編寫shell實現自動刪除50個賬號功能,賬號名爲stud1至stud50
#!/bin/bash
for((i=1;i<=50;i++));do
userdel stud$i
done
15、 請用Iptables寫出只允許10.1.8.179 訪問本服務器的22端口。
/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
16、 在shell中變量的賦值有四種方法,其中,採用name=12的方法稱( A ) 。
A直接賦值 B使用read命令
C使用命令行參數 D使用命令的輸出
17、 有文件file1
1) 查詢file1裏面空行的所在行號
grep -n ^$ file1
2) 查詢file1以abc結尾的行
grep abc$ file1
3) 打印出file1文件第1到第三行
head -n3 file1
sed "3q" file1
sed -n "1,3p" file1
18、 假設有一個腳本scan.sh,裏面有1000行代碼,並在vim模式下面,請按照如下要求寫入對應的指令
1) 將shutdown字符串全部替換成reboot
:%s/shutdown/reboot/g
2) 清空所有字符
:%d
3) 不保存退出
q!
19、 1到10數字相加,寫出shell腳本
#!/bin/bash
j=0
for((i=1;i<=10;i++));do
j=$[j+i ]
done
echo $j
20、 常見shell有哪些?缺省的是哪個?
/bin/sh /bin/bash /bin/ash /bin/bsh /bin/csh /bin/tcsh /sbin/nologin
21、 Shell循環語句有哪些?
for while until
22、 用SHELL模擬LVS,腳本怎麼寫
/sbin/iptable -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.1.11-192.168.1.12
23、 找出系統內大於50k,小於100k的文件,並刪除它們。
#!/bin/bash
file=`find / -size +50k -size -100k`
for i in $file;do
rm -rf $i
done
24、 腳本(如:目錄dir1、dir2、dir3下分別有file1、file2、file2,請使用腳本將文件改爲dir1_file1、dir2_file2、dir3_file3)
#!/bin/bash
file=`ls dir[123]/file[123]`
for i in $file;do
mv $i ${i%/*}/${i%%/*}_${i##*/}
done
25、 將A 、B、C目錄下的文件A1、A2、A3文件,改名爲AA1、AA2、AA3.使用shell腳本實現。
#!/bin/bash
file=`ls [ABC]/A[123]`
for i in $file;do
mv $i ${i%/*}/A${i#*/}
done