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
- #! /Bin/bash
- echo "Please input IP Address:"
- Read ip address
- [-n "'grep" $ IP "/etc /hosts' ".
- echo "the hostname is: 'grep " $ IP "/etc/hosts | awk '{print $2} ''" | \
- 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:
-
- #! /Bin/bash
-
- # Author oldboy
-
- # QQ 1, 31333741
-
- # Judge Input
-
- If [$ #-ne 1]
-
- Then
-
- Echo"Input error! "
-
- Exit 1
-
- Fi
-
-
- Flag = 0
-
- Exec</Etc/hosts
-
- WhileReadLine
-
- Do
-
- If ["$1"="'Echo $ Line | awk' {print $1 }''"]
-
- Then
-
- Flag = 1
-
- Echo"The $1's hostname is 'echo $ Line | awk '{print $2 }''"
-
- Break;
-
- Fi
- Done
-
- [$ Flag-EQ 0] & Echo"Sorrry, not find $1's hostname! "
-
-
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:
- [Root @ old_boy Scripts] # Cat awkhost1.sh
- 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
- [Root @ old_boy Scripts] # Cat awkhost2.sh
- 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
- # Cat awkhost4.sh
- Awk'/'"$ {1 }"'/''{Print $2 }'/Etc/hosts
- Execution result:
- # Awkhost4.sh 192.168.1.21
- Oldboy21.etiantian.org
- # Awkhost4.sh 192.168.1.11
- Oldboy11.etiantian.org
- # Awkhost4.sh 192.168.1.31
- Oldboy31.etiantian.org
- 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.
- For example:
- 192.168.1.11 oldboy11.etiantian.org
- 192.168.1.111 oldboy111.etiantian.org
---------------- I am a separator for each method ---------------
Method 6) awk Filtering
- # Cat awkhost5.sh
-
- 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
-
- Execution result:
-
- # Awkhost5.sh 192.168.1.31
-
- Oldboy31.etiantian.org
-
- # Awkhost5.sh 192.168.1.11
-
- Oldboy11.etiantian.org
-
- Oldboy111.etiantian.org------> A bug exists here.
-
- # Awkhost5.sh 192.168.1.21
-
- Oldboy21.etiantian.org
-
- Fix bugs after improvement:
-
- # Cat awkhost5-1.sh
- Awk'{If ($1 ~ /'$1'/) Print $2 }'/Etc/hosts ==> it is incorrect to add spaces above.
-
- # Cat awkhost5-1.sh
-
- Awk'{If ($1 ~ /'$1'$/) Print $2 }'/Etc/hosts # Add a regular expression $
-
- Execution result:
-
- # Awkhost5-1.sh 192.168.1.21
-
- Oldboy21.etiantian.org
-
- # Awkhost5-1.sh 192.168.1.11
-
- Oldboy11.etiantian.org
-
- # Awkhost5-1.sh 192.168.1.31
-
- Oldboy31.etiantian.org
---------------- I am a separator for each method ---------------
Method 7) awk-V exact match
- Command Line test:
-
- # Awk-V p = 192.168.1.21'$1 = P {print $2 }'/Etc/hosts
-
- Oldboy21.etiantian.org
-
- # Awk-V p = 192.168.1.11'$1 = P {print $2 }'/Etc/hosts
-
- Oldboy11.etiantian.org
-
- # Awk-V p = 192.168.1.11'$1 = P {print $2 }'/Etc/hosts
-
- Oldboy11.etiantian.org
-
- Actual script:
-
- # Cat awkhost6.sh
-
- #! /Bin/bash
-
- # P = $1
- # Awk-V p ="$ P" '$1 = P {print $2 }'/Etc/hosts
-
- 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
- # Cat awkhost9.sh
- Awk'$1 = "'$1'"{Print $2 }'/Etc/hosts
- Execution result:
- # Sh awkhost9.sh 192.168.1.11
- Oldboy11.etiantian.org
- # Sh awkhost9.sh 192.168.1.21
- Oldboy21.etiantian.org
- # Sh awkhost9.sh 192.168.1.31
- Oldboy31.etiantian.org
- Note: here, the old boys and teachers use different awk methods to implement the same function. It is very powerful,
- 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
-
- #! /Bin/bash
-
- B =/$ PWD/wang.txt
- Echo-n"Plase input IP :"
-
- ReadA
-
- If [$ A ="192.168.1.11"]
-
- Then
-
- Cat $ B | grep $ A | awk-F'' '{Print $2 }'
-
-
-
- Elif [$ A ="192.168.1.21"]
-
- Then
- Cat $ B | grep $ A | awk-F'' '{Print $2 }'
-
-
-
- Elif [$ A ="192.168.1.31"]
-
- Then
-
- Cat $ B | grep $ A | awk-F'' '{Print $2 }'
-
- Else
-
- Echo"Plase input the correct IP Address"& Exit 1
-
- Fi
- Tip: Let's see where the problem is? The script is not common.
-
-
---------- After the improvement
-
- #! /Bin/bash
-
- # Author oldboy
-
- # QQ 1, 31333741
-
- Hosts_file ="$ PWD/oldboy.txt"
-
- # Judge File
-
- [! -F $ hosts_file] & Echo"No test file! "& Exit 1
-
- Echo-n"Plase input IP :"
-
- ReadIP
-
- # Judge IP Format
- ["$ {# }"-Lt 8] & ["'Echo $ IP | SED's/[0-9] // g ''"! ="..."] & \
-
- Echo"Plase input the correct IP Address"& Exit 1
-
-
-
- # Start
-
- Result1 = $ (grep"$ IP"$ Hosts_file | awk'{Print $1 }')
-
- If ["$ IP"="$ Result1"]
-
- Then
- Grep"$ IP"$ Hosts_file | awk'{Print $2 }'
-
- Exit 0
-
- Else
-
- Echo"Not find the hostname of $ IP"
-
- Exit 1
-
- Fi
-
- Tip: This method is not advisable.