Nine shell solutions to practical Linux O & M Problems

Source: Internet
Author: User

Source: Old Boy's Linux blog Author: Old Boy

The problem is:
4) known: the content of/etc/hosts is
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
Please use shell scripts. How can I find the unique hostname in/etc/hosts after entering the IP address?
Answer:
Method 1) script Filtering# Cat judgehost. Sh

    1. #! /Bin/bash
    2. echo "Please input IP Address:"
    3. Read ip address
    4. [-n "'grep" $ IP "/etc /hosts' ".
    5. echo "the hostname is: 'grep " $ IP "/etc/hosts | awk '{print $2} ''" | \
    6. echo "the IP is invalid"

Tip:
1) This is an implementation syntax for grep filtering and conditional judgment:
2) The condition judgment syntax is [-n "DDD"] & Echo 1 | Echo 0
3) [-n "'grep" $ IP "/etc/hosts. Here, we want to exclude the following duplicates.
192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org
---------------- I am a separator for each method ---------------
Method 2) exact script matching:

  1. #! /Bin/bash
  2. # Author oldboy
  3. # QQ 1, 31333741
  4. # Judge Input
  5. If [$ #-ne 1]
  6. Then 
  7. Echo"Input error! " 
  8. Exit 1
  9. Fi
  10.  
  11. Flag = 0
  12. Exec</Etc/hosts
  13. WhileReadLine
  14. Do
  15. If ["$1"="'Echo $ Line | awk' {print $1 }''"]
  16. Then 
  17. Flag = 1
  18. Echo"The $1's hostname is 'echo $ Line | awk '{print $2 }''"
  19. Break;
  20. Fi
  21. Done
  22. [$ Flag-EQ 0] & Echo"Sorrry, not find $1's hostname! " 
  23.  

Tip: For this question, please learn how to use while and how to set flag.

Execution result:
# Sh oldboy. Sh 192.168.1.11
The 192.168.1.11's hostname is oldboy11.etiantian.org
# Sh oldboy. Sh 192.168.1.21
The 192.168.1.21's hostname is oldboy21.etiantian.org
# Sh oldboy. Sh 192.168.1.311
Sorrry, not find 192.168.1.311's hostname!
---------------- I am a separator for each method ---------------

Note: In the following method, the old boys and teachers use different awk methods to implement the same function in large numbers to tell you That awk is very powerful, I hope that the students will be proficient in teaching.

Method 3) awk exact match:
Preparation:
# 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
Script:

    1. [Root @ old_boy Scripts] # Cat awkhost1.sh
    2. Awk'Begin{ A = "'$1'"} {If ($1 = A) Print $2 ;}'/Etc/hosts

Execution result:
# Sh awkhost1.sh 192.168.1.21
Oldboy21.etiantian.org
# Sh awkhost1.sh 192.168.1.31
Oldboy31.etiantian.org
# Sh awkhost1.sh 192.168.1.11
Oldboy11.etiantian.org
Tip: Pay attention to the usage of a = "'$ 1'". $1 is a command line parameter. AwkProgramA = "'$1 '".
---------------- I am a separator for each method ---------------
Method 4) awk exact match

    1. [Root @ old_boy Scripts] # Cat awkhost2.sh
    2. Awk'{If ($1 = "'$1'") Print $2 }'/Etc/hosts

Execution result:
# Awkhost2.sh 192.168.1.11
Oldboy11.etiantian.org
# Awkhost2.sh 192.168.1.21
Oldboy21.etiantian.org
# Awkhost2.sh 192.168.1.311
---------------- I am a separator for each method ---------------
Method 5) awk Filtering

    1. # Cat awkhost4.sh
    2. Awk'/'"$ {1 }"'/''{Print $2 }'/Etc/hosts
    3. Execution result:
    4. # Awkhost4.sh 192.168.1.21
    5. Oldboy21.etiantian.org
    6. # Awkhost4.sh 192.168.1.11
    7. Oldboy11.etiantian.org
    8. # Awkhost4.sh 192.168.1.31
    9. Oldboy31.etiantian.org
    10. Tip: apart from the syntax, this question is learned, that is, when filtering, passing parameters should end with a space to filter out duplicate IP addresses.
    11. For example:
    12. 192.168.1.11 oldboy11.etiantian.org
    13. 192.168.1.111 oldboy111.etiantian.org

---------------- I am a separator for each method ---------------
Method 6) awk Filtering

  1. # Cat awkhost5.sh
  2. Awk'{If ($1 ~ /'$1'/) Print $2 }'/Etc/hosts # print the second column if the first column of the file contains the first parameter character of the command line
  3. Execution result:
  4. # Awkhost5.sh 192.168.1.31
  5. Oldboy31.etiantian.org
  6. # Awkhost5.sh 192.168.1.11
  7. Oldboy11.etiantian.org
  8. Oldboy111.etiantian.org------> A bug exists here. 
  9. # Awkhost5.sh 192.168.1.21
  10. Oldboy21.etiantian.org
  11. Fix bugs after improvement:
  12. # Cat awkhost5-1.sh
  13. Awk'{If ($1 ~ /'$1'/) Print $2 }'/Etc/hosts ==> it is incorrect to add spaces above.
  14. # Cat awkhost5-1.sh
  15. Awk'{If ($1 ~ /'$1'$/) Print $2 }'/Etc/hosts # Add a regular expression $
  16. Execution result:
  17. # Awkhost5-1.sh 192.168.1.21
  18. Oldboy21.etiantian.org
  19. # Awkhost5-1.sh 192.168.1.11
  20. Oldboy11.etiantian.org
  21. # Awkhost5-1.sh 192.168.1.31
  22. Oldboy31.etiantian.org

---------------- I am a separator for each method ---------------
Method 7) awk-V exact match
 

  1. Command Line test:
  2. # Awk-V p = 192.168.1.21'$1 = P {print $2 }'/Etc/hosts
  3. Oldboy21.etiantian.org
  4. # Awk-V p = 192.168.1.11'$1 = P {print $2 }'/Etc/hosts
  5. Oldboy11.etiantian.org
  6. # Awk-V p = 192.168.1.11'$1 = P {print $2 }'/Etc/hosts
  7. Oldboy11.etiantian.org
  8. Actual script:
  9. # 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

execution result:
# sh awkhost6.sh 192.168.1.11
oldboy11.etiantian.org
# sh awkhost6.sh 192.168.1.21
oldboy21.etiantian.org
tip:
1) if the parameter is not an awk program, 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.
---------------- I am the separator of each method ---------------
Method 8: simple Method of exact match

    1. # Cat awkhost9.sh
    2. Awk'$1 = "'$1'"{Print $2 }'/Etc/hosts
    3. Execution result:
    4. # Sh awkhost9.sh 192.168.1.11
    5. Oldboy11.etiantian.org
    6. # Sh awkhost9.sh 192.168.1.21
    7. Oldboy21.etiantian.org
    8. # Sh awkhost9.sh 192.168.1.31
    9. Oldboy31.etiantian.org
    10. Note: here, the old boys and teachers use different awk methods to implement the same function. It is very powerful,
    11. I hope that the students will be proficient in teaching.

---------------- I am a separator for each method ---------------
Method 9: an immature Implementation Method for students

  1. #! /Bin/bash
  2. B =/$ PWD/wang.txt
  3. Echo-n"Plase input IP :" 
  4. ReadA
  5. If [$ A ="192.168.1.11"]
  6. Then 
  7. Cat $ B | grep $ A | awk-F'' '{Print $2 }' 
  8.  
  9. Elif [$ A ="192.168.1.21"]
  10. Then 
  11. Cat $ B | grep $ A | awk-F'' '{Print $2 }' 
  12.  
  13. Elif [$ A ="192.168.1.31"]
  14. Then 
  15. Cat $ B | grep $ A | awk-F'' '{Print $2 }' 
  16. Else 
  17. Echo"Plase input the correct IP Address"& Exit 1
  18. Fi
  19. Tip: Let's see where the problem is? The script is not common.
  20.  

---------- After the improvement

  1. #! /Bin/bash
  2. # Author oldboy
  3. # QQ 1, 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. ReadIP
  9. # Judge IP Format
  10. ["$ {# }"-Lt 8] & ["'Echo $ IP | SED's/[0-9] // g ''"! ="..."] & \
  11. Echo"Plase input the correct IP Address"& Exit 1
  12.  
  13. # Start
  14. Result1 = $ (grep"$ IP"$ Hosts_file | awk'{Print $1 }')
  15. If ["$ IP"="$ Result1"]
  16. Then
  17. Grep"$ IP"$ Hosts_file | awk'{Print $2 }' 
  18. Exit 0
  19. Else 
  20. Echo"Not find the hostname of $ IP" 
  21. Exit 1
  22. Fi
  23. Tip: This method is not advisable.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.