Shell gets the specified content exactly

Source: Internet
Author: User
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 the shell script to realize, how can you find the unique hostname in the/etc/hosts after you enter IP?
Answer:
Method 1) Script filtering method
[Root@old_boy scripts]# cat judgehost.sh #!/bin/bash echo "Please input IP address:" Read IP [-n "' grep" $ip "/e   Tc/hosts ' "] && #注意前面的过滤条件结尾带有空格. echo "The hostname is: ' grep ' $ip"/etc/hosts |awk ' {print $} ' "| | \ echo "The IP is invalid"

Tips:
1 This is an implementation syntax for grep filter plus conditional judgment:
2 The Conditional judgment syntax is [-n "ddd"] && echo 1 | | Echo 0
3 [-N "grep" $ip "/etc/hosts"] && #注意前面的过滤条件结尾带有空格. Here, to eliminate the following duplication.
192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org
----------------I am the delimiter for each method---------------
Method 2 Script Exact matching method:

#!/bin/bash   #author  oldboy   #qq  31333741   #judge  input   if [ $# &NBSP;-NE&NBSP;1&NBSP]     then      echo  "input error!"       exit 1   fi     flag=0   exec < /etc/hosts   While read line   do    if [  "$  = " ' echo  $line |awk  ' { print $1} ' "&NBSP;]      then         flag=1          echo  "the $1  ' s hostname is  '" echo  $line |awk  ' {print $2} ' "           break;   &NBSP;FI   done    [  $flag  -eq 0 ] && echo  "  sorrry,not find $1  ' s hostname! '    

Hint: This problem, please learn the use of while and set flag ideas. Execution results:
[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!
----------------I am the delimiter for each method---------------

Special Note: The following method, the old boy teacher a lot of use awk different methods to achieve the same function, to tell you, awk is very powerful, hope that students can be proficient in accordance with the teacher's teaching requirements.

Method 3) awk exact match:
Get ready:
[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
Script:

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

Execution results:
[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
Tip: Note the use of a= "'", which is a command line argument. The method of calling system variables in an awk program A= "' $.
----------------I am the delimiter for each method---------------
Method 4) awk exact matching method

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

Execution results:
[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
----------------I am the delimiter for each method---------------
Method 5) Awk Filtration method

[Root@old_boy scripts]# cat awkhost4.sh awk '/' ${1} '/' {print $} '/etc/hosts execution results: [Root@old_boy scripts]#-Awkh   ost4.sh 192.168.1.21 oldboy21.etiantian.org root@old_boy scripts]# awkhost4.sh 192.168.1.11 oldboy11.etiantian.org [Root@old_boy scripts]# awkhost4.sh 192.168.1.31 oldboy31.etiantian.org hint: In addition to grammar, this problem has a knowledge, that is, filtering the newsletters parameter end to take a space, so as to filter the repeated IP Situation such as: 192.168.1.11 oldboy11.etiantian.org 192.168.1.111 oldboy111.etiantian.org

----------------I am the delimiter for each method---------------
Method 6) Awk filtration method

[root@old_boy scripts]# cat awkhost5.sh    awk  ' {if ($1~/' $ '/)  print $2} '   /etc/hosts # #如果文件第一列包含命令行第一个参数字符则打印第二列   Implementation results:   [root@old_boy scripts]#  awkhost5.sh 192.168.1.31   oldboy31.etiantian.org   [root@old_boy scripts]# awkhost5.sh  192.168.1.11   oldboy11.etiantian.org   oldboy111.etiantian.org ------> There's a bug in here.   [root@old_boy scripts]# awkhost5.sh 192.168.1.21   oldboy21.etiantian.org   Improved to troubleshoot bugs:   [root@old_boy scripts]# cat awkhost5-1.sh    awk  ' {if ($1~/' $ '  /)  print $2} '   /etc/hosts ==> the idea of adding spaces above is wrong.   [root@old_boy scripts]# cat awkhost5-1.sh    awk  ' {if ($1~/' $/')  print  $2} '   /etc/hosts  #增加一个正则表达式 $   Execution results:   [root@old_boy scripts]#  awkhost5-1.sh 192.168.1.21   OLDBOY21.Etiantian.org   [root@old_boy scripts]# awkhost5-1.sh 192.168.1.11   oldboy11.etiantian.org   [root@old_boy scripts]# awkhost5-1.sh 192.168.1.31   oldboy31.etiantian.org 

----------------I am the delimiter for each method---------------
Method 7) Awk-v exact matching method

Command-line testing: [Root@old_boy scripts]# awk-v p=192.168.1.21 ' = = p{print $} '/etc/hosts oldboy21.etiantian.org [root@old  _boy scripts]# awk-v p=192.168.1.11 ' = = p{print $} '/etc/hosts oldboy11.etiantian.org [Root@old_boy scripts]# awk -V p=192.168.1.11 ' = = = p {print $} '/etc/hosts oldboy11.etiantian.org actual script: [Root@old_boy scripts]# cat Awkhost6. SH #!/bin/bash #p =$1 #awk-v p= "$p" ' = = = P{print $} '/etc/hosts awk-v p= ' $ ' = = P{print $} '/etc/hosts

Execution results:
[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
Tips:
1 The reference is not awk program, so the writing p= "$"
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 the" AWK program.
----------------I am the delimiter for each method---------------
Method 8: Exact matching of simple wording

[Root@old_boy scripts]# cat awkhost9.sh awk ' = = ' ' {print $} '/etc/hosts execution result: [Root@old_boy scripts]# SH awkhost9.sh 192.168.1.11 oldboy11.etiantian.org [root@old_boy scripts]# sh awkhost9.sh 192.168.1.21 Oldboy21.etian tian.org [root@old_boy scripts]# sh awkhost9.sh 192.168.1.31 oldboy31.etiantian.org Special tip: Here the old boy teacher used a lot of different methods of awk to achieve the same function, very powerful, I hope the students can be proficient in accordance with the teacher's teaching requirements.

----------------I am the delimiter for each method---------------
Law 9: A student's immature realization method

#!/bin/bash   b=/$PWD/wang.txt   echo -n  "plase input ip : "   Read  a   if [  $a  ==  "192.168.1.11" &NBSP;]            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   tips: Let's see what the problem is. The script is not generic.    

----------old boy teacher improved

#!/bin/bash   #author  oldboy   #qq  31333741   hosts_file= "$PWD/oldboy.txt"   #judge  file   [ ! -f  $hosts _file ] && echo  "no test  file! "  && exit 1   echo -n  "plase input ip : "   Read  ip   #judge  ip format   [  "${#a}"  -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"  ==  "$result 1"  ]     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, the superfluous.  

  from: http://oldboy.blog.51cto.com/2561410/760187
Software

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.