老男孩教育每日一题-第60天-一道实用Linux运维问题的9种Shell解答方法!

简介:

2017-06-04

1.问题为:

已知:/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?

2.解答:

法1)脚本过滤法

[root@old_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 $2}'`" || \  
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)脚本精确匹配法:

#!/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 [ "$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!"

提示:此题,请大家学习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/hosts192.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

脚本:

[root@old_boy scripts]# cat awkhost1.sh   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精确匹配法

[root@old_boy scripts]# cat awkhost2.sh   
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过滤法

[root@old_boy scripts]# cat awkhost4.sh   awk '/'"${1} "'/''{print $2}' /etc/hosts

执行结果:  

[root@old_boy scripts]# awkhost4.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

提示:除了语法外,这道题有个学问,就是过滤时传参结尾要带个空格,这样才能过滤重复IP的情况

如:  

 192.168.1.11  oldboy11.etiantian.org  
 192.168.1.111  oldboy111.etiantian.org 
 ```
----------------我是每种方法分隔符---------------
###法6)awk过滤法
```bash
[root@old_boy scripts]# cat awkhost5.sh   
awk '{if($1~/'$1'/) print $2}'  /etc/hosts ##如果文件第一列包含命令行第一个参数字符则打印第二列


执行结果:  

[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 ------>这里有bug了。  
[root@old_boy scripts]# awkhost5.sh 192.168.1.21  oldboy21.etiantian.org

改进下来排除bug:  

[root@old_boy scripts]# cat awkhost5-1.sh  
 awk '{if($1~/'$1' /) print $2}'  /etc/hosts ==>用上面加空格的思路不对。  
[root@old_boy scripts]# cat awkhost5-1.sh   
awk '{if($1~/'$1'$/) print $2}'  /etc/hosts #增加一个正则表达式$

执行结果:  

[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


----------------我是每种方法分隔符---------------

法7)awk -v精确匹配法

命令行测试:  

[root@old_boy scripts]# awk -v p=192.168.1.21 '$1 == p{print $2}' /etc/hosts  oldboy21.etiantian.org  
[root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p{print $2}' /etc/hosts  oldboy11.etiantian.org  
[root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p {print $2}' /etc/hosts  oldboy11.etiantian.org

实际脚本:

[root@old_boy scripts]# 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

执行结果:

[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:精确匹配简单的写法

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

执行结果:  

[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.etiantian.org  
[root@old_boy scripts]# sh awkhost9.sh  192.168.1.31  
oldboy31.etiantian.org


特别提示:这里老男孩老师大量的使用了awk的不同方法来实现同样的功能,很强大吧,

希望同学们能按照老师的教学要求精通之。 
----------------我是每种方法分隔符---------------

法9:学生的一个不成熟的实现法

#!/bin/bash   b=/$PWD/wang.txt  
echo -n "plase input ip : " 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 " && exit 1  fi

提示:大家看看问题在哪?脚本不通用。  

----------老男孩老师改进后

#!/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" == "$result1" ]  
 then   
        grep "$ip" $hosts_file|awk '{print $2}' 
        exit 0  else 
        echo  "Not find the hostname of $ip" 
        exit 1  
 fi

提示:此法不可取,画蛇添足了。


本文转自 李导 51CTO博客,原文链接:http://blog.51cto.com/lidao/1932142

相关文章
|
3天前
|
Linux
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
|
2天前
|
Linux
Linux如何查询较大文件的方法
【5月更文挑战第8天】Linux如何查询较大文件的方法
6 0
|
3天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
3天前
|
Linux 网络安全
xmanager 4 连接SuSE linux server 11方法
xmanager 4 连接SuSE linux server 11方法
|
4天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
10 1
|
5天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
24 5
|
8月前
|
缓存 运维 Linux
Linux(CentOS)运维脚本工具集合
Linux(CentOS)运维脚本工具集合
158 2
|
1月前
|
运维 Linux Shell
linux运维常用命令
linux运维常用命令
|
2月前
|
监控 网络协议 Linux
Linux 命令大全 & CentOS常用运维命令
Linux 命令大全 & CentOS常用运维命令
164 0