Old boy Education daily-60th day-a practical Linux operation 9 kinds of shell solution Method!

Source: Internet
Author: User

2017-06-04

1. The problem is:

Known: The content of/etc/hosts is

192.168.1.11 oldboy11.etiantian.org192.168.1.21 oldboy21.etiantian.org192.168.1.31 oldboy31.etiantian.org# 192.168.1.111 oldboy111.etiantian.org

Please use shell script to implement, how can I find the corresponding unique hostname in/etc/hosts after entering IP?

2. Solution: Act 1) Script filtering method
[Email protected]_boy scripts]# cat judgehost.sh #!/bin/bash echo "Please input IP address:" Read IP [-n "' grep" $   IP "/etc/hosts"] && \ #注意前面的过滤条件结尾带有空格. echo "The hostname is: ' grep" $ip "/etc/hosts |awk ' {print $} '" | | \ echo "The IP is invalid"

Tips:
1) This is a grep filter plus conditional judgment implementation syntax:
2) The conditional judgment syntax is[ -n "ddd" ] && echo 1 || echo 0
3) [ -n "`grep "$ip " /etc/hosts`" ] && \ #注意前面的过滤条件结尾带有空格. Here, to rule out the following repetition .

192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org

------------------I am the delimiter of each method----------------------

Method 2) Script exact matching method:
#!/bin/bash    #author  oldboy    #qq  31333741    #judge  input   if [ $# -ne 1 ]    then      echo  "input error!"      exit 1  fi   flag=0  exec <  /etc/hosts  while read line  do   if [  "$"   =  "' 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! ' 

Hint: This question, please learn the use of while and set the idea of flag.
Execution Result:

[Email protected]_boy scripts]# sh oldboy.sh 192.168.1.11the 192.168.1.11 ' s hostname is oldboy11.etiantian.org [[email P Rotected]_boy scripts]# sh oldboy.sh 192.168.1.21 the 192.168.1.21 ' s hostname is oldboy21.etiantian.org[[email protected ]_boy scripts]# sh oldboy.sh 192.168.1.311sorrry,not find 192.168.1.311 ' s hostname!

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

Method 3) awk exact match:

Get ready:

[Email protected]_boy scripts]# tail-4/etc/hosts192.168.1.11 oldboy11.etiantian.org192.168.1.111 oldboy111.etiantian.org192.168.1.21 oldboy21.etiantian.org192.168.1.31 oldboy31.etiantian.org

Script:

[[Email Protected]_boy scripts]# cat awkhost1.sh awk ' BEGIN {a= ' ' $ '} {if ($1==a) print $;} '/etc/hosts

Execution Result:

[[Email protected]_boy scripts]# sh awkhost1.sh 192.168.1.21oldboy21.etiantian.org[[email protected]_boy scripts]# sh awkhost1.sh 192.168.1.31oldboy31.etiantian.org[[email protected]_boy scripts]# sh awkhost1.sh 192.168.1.11oldboy11.etiantian.org

Tip: Note the use of a= "' $ '" for command-line arguments. The method of calling system variables in the awk program a= "' $ '".
----------------I am the delimiter of each method---------------

Method 4) Awk exact Match method
[Email protected]_boy scripts]# cat awkhost2.sh awk ' {if ($1== "' $ '") print $ '/etc/hosts

Execution Result:

[Email protected]_boy scripts]# awkhost2.sh 192.168.1.11oldboy11.etiantian.org[[email protected]_boy scripts]# awkhost2.sh 192.168.1.21oldboy21.etiantian.org[[email protected]_boy scripts]# awkhost2.sh 192.168.1.311

----------------I am the delimiter of each method---------------

Method 5) Awk Filter method
[[Email Protected]_boy scripts]# cat awkhost4.sh awk '/' ' ${1} '/' ' {print $} '/etc/hosts

Execution Result:

[Email protected]_boy scripts]# awkhost4.sh 192.168.1.21 oldboy21.etiantian.org [[Email Protected]_boy scripts]# awkho st4.sh 192.168.1.11 oldboy11.etiantian.org [[email protected]_boy scripts]# awkhost4.sh 192.168.1.31 Oldboy31.etiantia n.org

Hint: In addition to the syntax, this problem has a knowledge, is to filter the end of the simultaneous to take a space, so as to filter the case of duplicate IP

such as:

192.168.1.11 oldboy11.etiantian.org 192.168.1.111 oldboy111.etiantian.org "---------------- I am the delimiter of each method---------------# # #法6) awk filter "' Bash[[email protected]_boy scripts]# cat awkhost5.sh awk ' {if ($1~/' $ '/) print $ '/etc/hosts # #如果文件第一列包含命令行第一个参数字符则打印第二列


Execution Result:

[Email protected]_boy scripts]# awkhost5.sh 192.168.1.31 oldboy31.etiantian.org [[Email Protected]_boy scripts]# awkho  st5.sh 192.168.1.11 oldboy11.etiantian.org oldboy111.etiantian.org------> There's a bug here. [Email protected]_boy scripts]# awkhost5.sh 192.168.1.21 oldboy21.etiantian.org

Improved to exclude bugs:

[[Email Protected]_boy scripts]# cat awkhost5-1.sh awk ' {if ($1~/' $ '/) print $ '/etc/hosts ==> the idea of adding a space above is incorrect. [[Email Protected]_boy scripts]# cat awkhost5-1.sh awk ' {if ($1~/' $ ' $/) print $ '/etc/hosts #增加一个正则表达式 $

Execution Result:

[Email protected]_boy scripts]# awkhost5-1.sh 192.168.1.21 oldboy21.etiantian.org [[Email Protected]_boy scripts]# awk host5-1.sh 192.168.1.11 oldboy11.etiantian.org [[email protected]_boy scripts]# awkhost5-1.sh 192.168.1.31 oldboy31.et iantian.org


----------------I am the delimiter of each method---------------

Method 7) Awk-v exact matching method

Command-line testing:

[Email protected]_boy scripts]# awk-v p=192.168.1.21 ' = = p{print $} '/etc/hosts oldboy21.etiantian.org [email Pro  Tected]_boy scripts]# awk-v p=192.168.1.11 ' $ = = P{print $} '/etc/hosts oldboy11.etiantian.org [[Email Protected]_boy scripts]# awk-v p=192.168.1.11 ' = = = p {print $} '/etc/hosts oldboy11.etiantian.org

Actual script:

[Email protected]_boy scripts]# cat awkhost6.sh #!/bin/bash #p =$1 #awk-v p= "$p" ' = = P{print $} '/etc/hosts a Wk-v p= "$" ' $ = = P{print '/etc/hosts

Execution Result:

[[Email protected]_boy scripts]# sh awkhost6.sh 192.168.1.11oldboy11.etiantian.org[[email protected]_boy scripts]# sh A Wkhost6.sh 192.168.1.21oldboy21.etiantian.org


Tips:
1) Pass the non-awk program, so the wording 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 is available to the BEGIN block of a AWK program.
----------------I am the delimiter of each method---------------

Method 8: Exact match simple notation
[Email protected]_boy scripts]# cat awkhost9.sh awk ' $ = ' ' $ ' "{print $} '/etc/hosts

Execution Result:

[Email protected]_boy scripts]# sh awkhost9.sh 192.168.1.11 oldboy11.etiantian.org [[Email protected]_boy scripts]# SH awkhost9.sh 192.168.1.21 oldboy21.etiantian.org [[email protected]_boy scripts]# sh awkhost9.sh 192.168.1.31 oldboy3 1.etiantian.org


Special Note: Here the old boy teacher a lot of use of awk different methods to achieve the same function, very powerful,

I hope that students can be proficient in accordance with the teacher's teaching requirements.
----------------I am the delimiter of each method---------------

Law 9: An immature realization of the students
#!/bin/bash   b=/$PWD/wang.txt  echo -n  "PLASE&NBSP;INPUT&NBSP;IP&NBSP;:    read a  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  "&NBSP;&AMP;&AMp; exit 1  fi 

Tip: Let's see where the problem is. Scripts are not generic.

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

#!/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

Hint: This method is not advisable, superfluous.





This article is from the "Lee blog" blog, make sure to keep this source http://lidao.blog.51cto.com/3388056/1932142

Old boy Education daily-60th day-a practical Linux operation 9 kinds of shell solution Method!

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.