歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> 一道實用Linux運維問題的9種Shell解答方法

一道實用Linux運維問題的9種Shell解答方法

日期:2017/2/28 15:58:04   编辑:SHELL編程
問題為:
4)已知:/etc/hosts的內容為
192.168.1.11 oldboy11.etiantian.org
192.168.1.21 oldboy21.etiantian.org
192.168.1.31 oldboy31.etiantian.org
#192.168.1.111 oldboy111.etiantian.org
請用shell腳本實現,怎麼才能在輸入IP後找到/etc/hosts裡對應的唯一的hostname?
解答:
法1)腳本過濾法
  1. [root@old_boy scripts]# cat judgehost.sh
  2. #!/bin/bash
  3. echo "please input ip address:"
  4. read ip
  5. [ -n "`grep "$ip " /etc/hosts`" ] && \ #注意前面的過濾條件結尾帶有空格。
  6. echo "The hostname is: `grep "$ip " /etc/hosts |awk '{print $2}'`" || \
  7. echo "The ip is invalid"

提示:
1)這是一個grep過濾加條件判斷的實現語法:
2)條件判斷語法為[ -n "ddd" ] && echo 1 || echo 0
3)[ -n "`grep "$ip " /etc/hosts`" ] && \ #注意前面的過濾條件結尾帶有空格。這裡啊,是為了排除下面的重復情況
192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org
----------------我是每種方法分隔符---------------
法2)腳本精確匹配法:

  1. #!/bin/bash
  2. #author oldboy
  3. #qq 31333741
  4. #judge input
  5. if [ $# -ne 1 ]
  6. then
  7. echo "input error!"
  8. exit 1
  9. fi
  10. flag=0
  11. exec < /etc/hosts
  12. while read line
  13. do
  14. if [ "$1" = "`echo $line|awk '{print $1}'`" ]
  15. then
  16. flag=1
  17. echo "the $1 's hostname is `echo $line|awk '{print $2}'`"
  18. break;
  19. fi
  20. done
  21. [ $flag -eq 0 ] && echo " sorrry,not find $1 's hostname!"

提示:此題,請大家學習while的用法及設置flag的思路。

執行結果:
[root@old_boy scripts]# sh oldboy.sh 192.168.1.11
the 192.168.1.11 's hostname is oldboy11.etiantian.org
[root@old_boy scripts]# sh oldboy.sh 192.168.1.21
the 192.168.1.21 's hostname is oldboy21.etiantian.org
[root@old_boy scripts]# sh oldboy.sh 192.168.1.311
sorrry,not find 192.168.1.311 's hostname!
----------------我是每種方法分隔符---------------

特別提示:下面的方法中,老男孩老師大量的使用了awk的不同方法來實現同樣的功能,來告訴大家,awk是很強大的, 希望同學們能按照老師的教學要求精通之。

法3)awk精確匹配:
准備:
[root@old_boy scripts]# tail -4 /etc/hosts
192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org
192.168.1.21 oldboy21.etiantian.org
192.168.1.31 oldboy31.etiantian.org
腳本:

  1. [root@old_boy scripts]# cat awkhost1.sh
  2. awk 'BEGIN {a="'$1'"} {if($1==a) print $2; }' /etc/hosts

執行結果:
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.21
oldboy21.etiantian.org
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.31
oldboy31.etiantian.org
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.11
oldboy11.etiantian.org
提示:注意a="'$1'"的用法,$1為命令行傳參。awk程序中調用系統變量的方法a="'$1'"。
----------------我是每種方法分隔符---------------
法4)awk精確匹配法

  1. [root@old_boy scripts]# cat awkhost2.sh
  2. awk '{if($1=="'$1'") print $2}' /etc/hosts

執行結果:
[root@old_boy scripts]# awkhost2.sh 192.168.1.11
oldboy11.etiantian.org
[root@old_boy scripts]# awkhost2.sh 192.168.1.21
oldboy21.etiantian.org
[root@old_boy scripts]# awkhost2.sh 192.168.1.311
----------------我是每種方法分隔符---------------
法5)awk過濾法

  1. [root@old_boy scripts]# cat awkhost4.sh
  2. awk '/'"${1} "'/''{print $2}' /etc/hosts
  3. 執行結果:
  4. [root@old_boy scripts]# awkhost4.sh 192.168.1.21
  5. oldboy21.etiantian.org
  6. [root@old_boy scripts]# awkhost4.sh 192.168.1.11
  7. oldboy11.etiantian.org
  8. [root@old_boy scripts]# awkhost4.sh 192.168.1.31
  9. oldboy31.etiantian.org
  10. 提示:除了語法外,這道題有個學問,就是過濾時傳參結尾要帶個空格,這樣才能過濾重復IP的情況
  11. 如:
  12. 192.168.1.11 oldboy11.etiantian.org
  13. 192.168.1.111 oldboy111.etiantian.org

----------------我是每種方法分隔符---------------
法6)awk過濾法

  1. [root@old_boy scripts]# cat awkhost5.sh
  2. awk '{if($1~/'$1'/) print $2}' /etc/hosts ##如果文件第一列包含命令行第一個參數字符則打印第二列
  3. 執行結果:
  4. [root@old_boy scripts]# awkhost5.sh 192.168.1.31
  5. oldboy31.etiantian.org
  6. [root@old_boy scripts]# awkhost5.sh 192.168.1.11
  7. oldboy11.etiantian.org
  8. oldboy111.etiantian.org ------>這裡有bug了。
  9. [root@old_boy scripts]# awkhost5.sh 192.168.1.21
  10. oldboy21.etiantian.org
  11. 改進下來排除bug:
  12. [root@old_boy scripts]# cat awkhost5-1.sh
  13. awk '{if($1~/'$1' /) print $2}' /etc/hosts ==>用上面加空格的思路不對。
  14. [root@old_boy scripts]# cat awkhost5-1.sh
  15. awk '{if($1~/'$1'$/) print $2}' /etc/hosts #增加一個正則表達式$
  16. 執行結果:
  17. [root@old_boy scripts]# awkhost5-1.sh 192.168.1.21
  18. oldboy21.etiantian.org
  19. [root@old_boy scripts]# awkhost5-1.sh 192.168.1.11
  20. oldboy11.etiantian.org
  21. [root@old_boy scripts]# awkhost5-1.sh 192.168.1.31
  22. oldboy31.etiantian.org

----------------我是每種方法分隔符---------------
法7)awk -v精確匹配法

  1. 命令行測試:
  2. [root@old_boy scripts]# awk -v p=192.168.1.21 '$1 == p{print $2}' /etc/hosts
  3. oldboy21.etiantian.org
  4. [root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p{print $2}' /etc/hosts
  5. oldboy11.etiantian.org
  6. [root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p {print $2}' /etc/hosts
  7. oldboy11.etiantian.org
  8. 實際腳本:
  9. [root@old_boy scripts]# cat awkhost6.sh
  10. #!/bin/bash
  11. #p=$1
  12. #awk -v p="$p" '$1 == p{print $2}' /etc/hosts
  13. awk -v p="$1" '$1 == p{print $2}' /etc/hosts

執行結果:
[root@old_boy scripts]# sh awkhost6.sh 192.168.1.11
oldboy11.etiantian.org
[root@old_boy scripts]# sh awkhost6.sh 192.168.1.21
oldboy21.etiantian.org
提示:
1)傳參非awk程序,因此寫法p="$1"
2)man awk
-v var=val
--assign var=val
Assign the value val to the variable var, before execution of the program begins. Such vari-
able values are available to the BEGIN block of an AWK program.
----------------我是每種方法分隔符---------------
法8:精確匹配簡單的寫法

  1. [root@old_boy scripts]# cat awkhost9.sh
  2. awk '$1 == "'$1'" {print $2}' /etc/hosts
  3. 執行結果:
  4. [root@old_boy scripts]# sh awkhost9.sh 192.168.1.11
  5. oldboy11.etiantian.org
  6. [root@old_boy scripts]# sh awkhost9.sh 192.168.1.21
  7. oldboy21.etiantian.org
  8. [root@old_boy scripts]# sh awkhost9.sh 192.168.1.31
  9. oldboy31.etiantian.org
  10. 特別提示:這裡老男孩老師大量的使用了awk的不同方法來實現同樣的功能,很強大吧,
  11. 希望同學們能按照老師的教學要求精通之。

----------------我是每種方法分隔符---------------
法9:學生的一個不成熟的實現法

  1. #!/bin/bash
  2. b=/$PWD/wang.txt
  3. echo -n "plase input ip : "
  4. read a
  5. if [ $a == "192.168.1.11" ]
  6. then
  7. cat $b | grep $a | awk -F ' ' '{print $2}'
  8. elif [ $a == "192.168.1.21" ]
  9. then
  10. cat $b | grep $a | awk -F ' ' '{print $2}'
  11. elif [ $a == "192.168.1.31" ]
  12. then
  13. cat $b | grep $a | awk -F ' ' '{print $2}'
  14. else
  15. echo "plase input the correct IP address " && exit 1
  16. fi
  17. 提示:大家看看問題在哪?腳本不通用。

----------老男孩老師改進後

  1. #!/bin/bash
  2. #author oldboy
  3. #qq 31333741
  4. hosts_file="$PWD/oldboy.txt"
  5. #judge file
  6. [ ! -f $hosts_file ] && echo "no test file!" && exit 1
  7. echo -n "plase input ip : "
  8. read ip
  9. #judge ip format
  10. [ "${#a}" -lt 8 ] && [ "`echo $ip|sed 's/[0-9]//g'`" != "..." ] && \
  11. echo "Plase input the correct IP address" && exit 1
  12. #start
  13. result1=$(grep "$ip" $hosts_file|awk '{print $1}')
  14. if [ "$ip" == "$result1" ]
  15. then
  16. grep "$ip" $hosts_file|awk '{print $2}'
  17. exit 0
  18. else
  19. echo "Not find the hostname of $ip"
  20. exit 1
  21. fi
  22. 提示:此法不可取,畫蛇添足了。
Copyright © Linux教程網 All Rights Reserved