linux sell下達測試問題

2010-06-09 3:04 pm
#!/bin/bash
if ping -c1 -w2 192.168.1.1 &> /dev/null; then
echo 'king is GOOD'
elif grep "king" ~/maintenance.txt &> /dev/null; then
echo 'king is undergoing maintenance'
else
echo 'king is unexpectedly BAD!'
exit 1
fi

請問中間在幹嘛 這程式應該是在測可上網就顯示good
沒上網就顯示bad吧?!

中間我不會用 請問交一下我~~

回答 (4)

2010-06-10 7:52 pm
✔ 最佳答案
您好,誠摯的來回答您的問題 :
已經看過您的問題了:
首先幫您把程式碼,使用巢狀整理一下 :
1 #!/bin/bash
2 if ping -c1 -w2 192.168.1.1 &> /dev/null;
3 then
4 echo 'king is GOOD'
5 elif
6 grep "king" ~/maintenance.txt &> /dev/null; then
7 echo 'king is undergoing maintenance'
8 else
9 echo 'king is unexpectedly BAD!'
10 exit 1
11 fi
前面是表示 網路正常就顯示 king is GOOD,
假設 ping 不通的話,搜尋 家目錄當中的 maintenance.txt,
是否有 king 的存在,如果有顯示 king is undergoing maintenance,
(也就是意味著 king 曾經使用過...)
如果沒有在 maintenance.txt 找到 king,則顯示 king is unexpectedly BAD!
以上 AYO 阿佑
參考: 自己+業界多年經驗+電腦週邊急救團
2014-09-05 9:32 am
這有類似的

▶▶http://qoozoo1400703.pixnet.net/blog
2010-06-10 7:28 pm
你講的意思是 如果離開不是0的話 程式就會印good
就會檢查king 裡的/maintenance.txt和印出maintenance和離開

不然將會印出bad和1和離開嗎?!
2010-06-10 2:03 pm
If the ping statement exits with non-zero, the script will echo ...GOOD. If not, the script will check if the name "king" exits in a file ~/maintenance.txt. If so, it will echo ... maintenance and exit. Otherwise, it will echo ...BAD and exit with 1.

2010-06-10 12:38:23 補充:
Sorry about this. if the ping statement exits WITH zero then the script will echo ...GOOD. Note that ping in your script will always success and thus will echo xxx GOOD even the destination is not "pingable".


收錄日期: 2021-05-01 23:51:00
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100609000010KK01368

檢視 Wayback Machine 備份