歡迎您光臨本站 註冊首頁

linux運維初級班課前綜合考試及參考答案20110529

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0


linux運維初級班課前綜合考試及參考答案20110529

#########################################################
#《老男孩linux就業培訓中心-初級班第七期課前考試及課上講解
#shell腳本編程實戰模擬考試
#date:2011-5-29
#出題人:老男孩
#QQ:31333741 MAIL:[email protected]
#blog: http://oldboy.blog.51cto.com
#psite: http://oldboy.cc(即將開放)
##########################################################


問題1、實現每分鐘檢查一次apache進程,如果進程不存在列印數字1,存在列印數字0.
提示:
1、apache進程數獲取命令 ps -ef|grep http|wc -l.
2、判斷條件如果http進程數量大於0,即認為進程存在.
要求:
1.請使用if語句加定時任務實現.
2.請使用while語句實現

解答:
[[email protected] scripts]# cat judgethttp.sh
#!/bin/sh
httppronum=`ps -ef|grep http|grep -v http|wc -l`
#if [ `ps -ef|grep http|wc -l` -gt 0 ] #====這樣寫也是可以的.
if [ $httppronum -gt 0 ]
then
echo 0
else
echo 1
fi
提示:以上腳本執行結果是不對的?不知道你們發現沒有?執行結果httppronum=`ps -ef|grep http|grep -v http|wc -l`
不符合要求,而直接命令行ps -ef|grep http|grep -v http|wc -l執行又是對的?到底是什麼原因呢?請見下文?

====此處學生提問====
為什麼老師的腳本開頭寫的是#!/bin/sh而不是#!/bin/bash
解答:因為在Centos和RedHat Linux系統 /bin/sh為/bin/bash軟鏈接,就是說是同一個.
[[email protected] scripts]# ll /bin/sh
lrwxrwxrwx 1 root root 4 04-23 16:29 /bin/sh -> bash
[[email protected] scripts]# ll /bin/bash
-rwxr-xr-x 1 root root 735004 2009-01-22 /bin/bash



提示:
Bash 是 GNU/Linux 默認的 Shell 和 Bourne shell (sh) 兼容,採取了Korn shell (ksh) 和
C shell (csh) 的特色.符合 IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools 標準.Centos和
RedHat Linux下默認的Shell均為bash.因此,在寫shell腳本的時候,我們的腳本的開頭也可以不加
#!/bin/bash.但如果當前的shell非你默認的shell時,比如tcsh,那麼就必須要寫#!了.
建議大家最好都加上開頭語言標識.

使用while守護進程方式實現(不用定時任務)
[[email protected] scripts]# /application/apache/bin/apachectl start #===>啟動apache
[[email protected] scripts]# ps -ef|grep http|grep -v grep #===>查看apache進程
root 3675 1 1 19:06 ? 00:00:00 /application/apache2.2.17/bin/httpd -k start
apache 3684 3675 0 19:06 ? 00:00:00 /application/apache2.2.17/bin/httpd -k start
apache 3686 3675 0 19:06 ? 00:00:00 /application/apache2.2.17/bin/httpd -k start
apache 3688 3675 0 19:06 ? 00:00:00 /application/apache2.2.17/bin/httpd -k start
apache 3690 3675 0 19:06 ? 00:00:00 /application/apache2.2.17/bin/httpd -k start
[[email protected] scripts]# cat judgeweb-while.sh #===>查看進程的腳本
#!/bin/sh
httpnum=`ps -ef|grep http|grep -v grep|wc -l`
while true
do
#if [ `ps -ef|grep http|wc -l` -gt 0 ] #====這樣寫也是可以的.
if [ $httpnum -gt 0 ]
then
echo 0
else
echo 1
fi
sleep 60
done
[[email protected] scripts]# sh judgeweb-while.sh #===>執行看結果,返回0表示存在http進程
0
0
0

[[email protected] scripts]# pkill httpd #===>殺死apache進程然後進行測試


[[email protected] scripts]# ps -ef|grep http|grep -v grep #===>http進程不存在了
[[email protected] scripts]# sh judgeweb-while.sh #===>執行看結果,返回1表示存在http進程
1
1
1


=============以下是學生的不同語法腳本===============
#!/bin/bash
#filename:while1
httpdport=`ps -ef|grep http|grep -v grep|wc -l`
while true
do
if (( "$httpdport" > 0 )) #======這是(())的語法寫法
then
echo "1"
else
echo "0"
fi
sleep 60
done
--------------------------------------------------
問題2、已知如下命令及返回結果,請問 echo $user 的返回的結果為( ).並解釋為什麼?
[[email protected] ~]$ cat test.sh
user=`whoami`
[[email protected] ~]$ sh test.sh
[[email protected] ~]$ echo $user
(互動百科面試題)



--------------------------------------------------
問題3. 從ett.log文件中提取包含"WARNING"或"UNKNOWN",同時不包含"OK"的行,然後提取以":"分割的第 3個欄位
WARNING:UNKNOWN:OK:CRITICAL
WARNING:UNKNOWN:NO:OLDBOY
WARNING:NO:OLDBOY:QINGYUN
UNKNOWN:OK:CRITICAL::CRITICAL
(百度面試題)
解答;
步驟一:
[[email protected] scripts]# egrep "WARNING|UNKNOWN" ett.log
WARNING:UNKNOWN:OK:CRITICAL
WARNING:UNKNOWN:NO:OLDBOY
WARNING:NO:OLDBOY:QINGYUN
UNKNOWN:OK:CRITICAL::CRITICAL
步驟二:
[[email protected] scripts]# egrep "WARNING|UNKNOWN" ett.log|grep -v OK
WARNING:UNKNOWN:NO:OLDBOY
WARNING:NO:OLDBOY:QINGYUN
步驟三:
方法1:
[[email protected] scripts]# egrep "WARNING|UNKNOWN" ett.log|grep -v OK |awk -F ":" '{print $3}'


NO
OLDBOY

方法2:
[[email protected] scripts]# egrep "WARNING|UNKNOWN" ett.log|grep -v OK |cut -d: -f3
NO
OLDBOY

--------------------------------------------------
問題4.已知oldboy.txt內容為
i am oldboy! my qq number is 31333741
請把oldboy.txt中小寫字母轉換成的寫字母

[[email protected] scripts]# echo 'i am oldboy! my qq number is 31333741' >oldboy.txt
法1:
[[email protected] scripts]# cat oldboy.txt |tr a-z A-Z
I AM OLDBOY! MY QQ NUMBER IS 31333741
[[email protected] scripts]# cat oldboy.txt |tr "a-z" "A-Z"
I AM OLDBOY! MY QQ NUMBER IS 31333741
[[email protected] scripts]# cat oldboy.txt |tr "[a-z]" "[A-Z]"
I AM OLDBOY! MY QQ NUMBER IS 31333741
[[email protected] scripts]# cat oldboy.txt |tr [a-z] [A-Z]
I AM OLDBOY! MY QQ NUMBER IS 31333741
[[email protected] scripts]# cat oldboy.txt
i am oldboy! my qq number is 31333741
提示:上面為不同的寫法,tr的命令很好用,建議大家好好總結下.

這裡也分享一個老男孩曾經寫的生產環境使用tr的一個函數腳本.記住tr命令很有用.

function check_web_url()
#檢查URL
{
wait #====>wait函數,內容見下面.
echo '檢查url...!.'
for ((i=0; i<`echo ${#web_url_list[*]}`; i ))
do
judge=($(curl -I -s --connect-timeout 5 ${web_url_list[$i]}|head -1|tr "r" "n"))
if [[ "${judge[1]}" == '200' && "${judge[2]}"=='OK' ]]
then
action "${web_url_list[$i]}" /bin/true
else
action "${web_url_list[$i]}" /bin/false
echo -n "retrying again...";sleep 3;
judgeagain=($(curl -I -s --connect-timeout 10 ${web_url_list[$i]}|head -1|tr "r" "n"))


if [[ "${judgeagain[1]}" == '200' && "${judgeagain[2]}"=='OK' ]]
then
action "${web_url_list[$i]},retried again" /bin/true
else
action "${web_url_list[$i]},retried again" /bin/false
fi
fi
sleep 1;
done
}

function wait()
{
echo -n '3秒后,執行以下操作';
for ((i=0;i<3;i ))
do
echo -n ".";sleep 1
done
echo
}

解讀:這是一個檢查URL是否正常的一個生產環境使用的函數腳本,很實用.不過還需要有能力才能駕御
它呦,有想用,確弄不太明白的朋友可以聯繫我..
--------------------------------------------------
問題5.如何得到RedHat Linux系統的下列信息,內核版本,發行版本?(搜狐暢遊面試題)
解答:
[[email protected] scripts]# uname -a
Linux oldboy-B 2.6.18-164.el5 #1 SMP Thu Sep 3 03:33:56 EDT 2009 i686 i686 i386 GNU/Linux
[[email protected] scripts]# uname -r
2.6.18-164.el5
[[email protected] scripts]# cat /etc/issue
CentOS release 5.4 (Final)
Kernel r on an m

[[email protected] scripts]# cat /etc/redhat-release
CentOS release 5.4 (Final)
--------------------------------------------------
問題6.下面是一台RedHat Linux系統伺服器執行uptime命令的輸出結果,請解釋各欄位的含義(搜狐暢遊面試題)
13:33:53 up 11 days,20:08,3 users,load average: 0.07,0.05,0.00
暫略

問題7.在RedHat/Centos Linux系統中,如何將oldboy.tar.gz壓縮文件解壓到指定的目錄/home/oldboy/下(搜狐暢遊面試題)
解答:
tar zxvf oldboy.tar.gz -C /home/oldboy/
提示:tar 是5星命令 請同學們必須熟練掌握了.詳細總結見老男孩的命令教案


--------------------------------------------------
問題8.在RedHat/Centos Linux系統中,怎樣查詢/home/oldboy.txt文件中包含SOHU或CYOU的字元串?(搜狐暢遊面試題)
解答:
[[email protected] scripts]# echo 'SHHU OLDBOY' >>oldboy.txt
[[email protected] scripts]# echo 'CYOU my love' >>oldboy.txt
[[email protected] scripts]# cat oldboy.txt
i am oldboy! my qq number is 31333741
SHHU OLDBOY
CYOU my love
法一:
[[email protected] scripts]# grep -E "SOHU|CYOU" oldboy.txt
CYOU my love
[[email protected] scripts]# echo 'SOHU OLDBOY' >>oldboy.txt
[[email protected] scripts]# echo 'sohu bestchaoliang' >>oldboy.txt
[[email protected] scripts]# grep -E "SOHU|CYOU" oldboy.txt
CYOU my love
SOHU OLDBOY
拓展:不區分大小寫的過濾
[[email protected] scripts]# grep -Ei "SOHU|CYOU" oldboy.txt
CYOU my love
SOHU OLDBOY
sohu bestchaoliang
[[email protected] scripts]# cat oldboy.txt
i am oldboy! my qq number is 31333741
SHHU OLDBOY
CYOU my love
SOHU OLDBOY
sohu bestchaoliang
--------------------------------------------------
問題9.在RedHat/Centos Linux系統上,某個進程佔用了tcp的7777埠,如何得到此進程的ID?(搜狐暢遊面試題)
解答:
法1:
[[email protected] scripts]# lsof -i tcp:7777
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 1548 root 3u IPv6 4706 TCP *:7777 (LISTEN)
sshd 1804 root 3u IPv6 5737 TCP localhost:7777->localhost:jwclient (ESTABLISHED)
sshd 1806 oldboy 3u IPv6 5737 TCP localhost:7777->localhost:jwclient (ESTABLISHED)
[[email protected] scripts]# lsof -i :7777
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME


sshd 1548 root 3u IPv6 4706 TCP *:7777 (LISTEN)
sshd 1804 root 3u IPv6 5737 TCP localhost:7777->localhost:jwclient (ESTABLISHED)
sshd 1806 oldboy 3u IPv6 5737 TCP localhost:7777->localhost:jwclient (ESTABLISHED)
法2:
[[email protected] scripts]# netstat -lntup|grep 7777
tcp 0 0 :::7777 :::* LISTEN 1548/sshd

提示:lsof是個不錯的命令,老男孩經常使用. 有機會給大家分享使用lsof解決問題是的生產實戰案例.
--------------------------------------------------
問題10.在RedHat/Centos linux中swap的作用?,swap一般分多大?(慧聰網面試題)

--------------------------------------------------


解答:
測試準備:
[[email protected] scripts]# pwd
/server/scripts
[[email protected] scripts]# touch oldboy.HTM chaoliang.HTM chenyan.HTM qingyun.HTM CAO.HTM guoxing.HTM dong.HTM
[[email protected] scripts]# ls -l *.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 CAO.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 chaoliang.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 chenyan.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 dong.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 guoxing.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 oldboy.HTM
-rw-r--r-- 1 root root 0 05-29 00:15 qingyun.HTM

使用cat非交互方式生成改名腳本renamefile.sh:
cat >renamefile.sh <<EOF
for file in `ls *.HTM`
do
newfile=`echo $file | tr A-Z a-z`
mv $file $newfile
done
EOF

提示:此處提到這個目的是讓大家熟悉下cat非交互方式輸入內容的方法,這個方法常用來在腳本中使用.
注意:`及$符號需要轉義 如:`和$
查看腳本內容:


[[email protected] scripts]# cat -n renamefile.sh
1 for file in `ls *.HTM`
2 do
3 newfile=`echo $file | tr A-Z a-z`
4 mv $file $newfile
5 done

第1行遍歷當前目錄找出符合條件的.HTM文件
第3行定義變數newfile保存新的文件名,tr要用好(昨天考試過的),注意``(tab上面的鍵)
這兩個符號內的都會被執行,然後賦值給newfile.
第4行,執行mv進行重命名.搞定.


[[email protected] scripts]# cat renamefile.sh
for file in `ls *.HTM`
do
newfile=`echo $file | tr HTM htm` #===這個更符合題目要求
#newfile=`echo $file | tr A-Z a-z` #===這個也會同時把文件名大寫改掉
mv $file $newfile
done
[[email protected] scripts]# sh renamefile.sh
[[email protected] scripts]# ls -l *.HTM
ls: *.HTM: 沒有那個文件或目錄
[[email protected] scripts]# ls -l *.htm
-rw-r--r-- 1 root root 0 05-29 00:15 cao.htm
-rw-r--r-- 1 root root 0 05-29 00:15 chaoliang.htm
-rw-r--r-- 1 root root 0 05-29 00:15 chenyan.htm
-rw-r--r-- 1 root root 0 05-29 00:15 dong.htm
-rw-r--r-- 1 root root 0 05-29 00:15 guoxing.htm
-rw-r--r-- 1 root root 0 05-29 00:15 oldboy.htm
-rw-r--r-- 1 root root 0 05-29 00:15 qingyun.htm
可以看到所有大寫的HTM都被改成了小寫htm.

========以下是學生做的方法===========
法2:
[[email protected] scripts]# cat renamefile2.sh
#!/bin/sh
#create by oldboy
#qq 31333741
for filename in `ls *.HTM`
do
mv $filename `echo $filename|tr HTM htm`
done
法3:
[[email protected] scripts]# cat renamefile3.sh
#!/bin/bash
#create by oldboy
#qq 31333741
#filename:filename.sh
for fname in *
do


f=$(echo $fname | tr A-Z a-z)
if [[ "$fname" != "$f" ]] #====這裡是不同的判斷語法[[]]用法
then
mv $fname $f
fi

done

,由於個人水平及時間關係,老男孩這裡給的參考答案未必都準確,歡迎大家一起交流討論,共同進步.

===================================================================

本文出自 「老男孩的linux博客」 博客,請務必保留此出處http://oldboy.blog.51cto.com/2561410/577227


[火星人 ] linux運維初級班課前綜合考試及參考答案20110529已經有487次圍觀

http://coctec.com/docs/linux/show-post-48286.html