如何通过bash脚本访问.dev文件 [英] How to access .dev file via bash script

查看:73
本文介绍了如何通过bash脚本访问.dev文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个 device.dev 文件,其中包含有关称为ads2的特定应用程序的一些配置信息.

Below is a device.dev file that contains some configuration info for a specific application called ads2.

1 DEVICES {
2        GLOBAL-CONFIG {
3            framerate = "20000";
4            subframes = "0";
5            max_consec_timeouts = "10";
6            max_total_timeouts = "1000";
7            schedmode = "Standard";
8        }
9        IO-DEVICES {
10        }
11        COMPUTING-DEVICES {
12            RT_WORKSTATION x-NB-0144 = {
13                hostname = "x-x-0144";
14                ipaddress = "xxx.x.x.xx";
15                DISPLAYS {
16                    main = "FDT-C-VM-0094:0.0";
17                }
18                SCHEDPARAM {
19                    active = "0";
20                    framerate = "20000";
21                    subframes = "0";
22                    max_consec_timeouts = "10";
23                    max_total_timeouts = "1000";
24                }
25            }
27  
28            RT_HOST xxx-c-agx-vw-89 = { 
29                hostname = "xxxxx@xxxx-desktop";
30                ipaddress = "xx.xx.xx.xx";         
31                SCHEDPARAM {
32                    active = "0";
33                    framerate = "20000";
34                    subframes = "0";
35                    max_consec_timeouts = "10";
36                   max_total_timeouts = "1000";
37                }
38            }
39        }
40    }

我正在尝试编写一个bash脚本,该脚本接受一个I​​P地址输入,然后访问 device.dev 文件并将其传递给变量 ipaddress 在第30行.

I'm trying to write a bash script that accepts an input which is an IP-address and then access the device.dev file and pass it to the variable ipaddress in line 30.

那么可以通过bash脚本访问devices.dev文件吗?

So is it possible to access devices.dev file via bash script?

预先感谢

推荐答案

使用GNU awk并仅关注第二个条目:

Using GNU awk and focusing on the second entry only:

awk -v ipadd="1.1.1.1" '/ipaddress/ { cnt++ } /ipaddress/ && cnt==2 { lne=gensub(/(^.*\")(.*)(\".*$)/,"\\1"ipadd"\\3",$0);print lne;next }1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev

说明:

awk -v ipadd="1.1.1.1" '/ipaddress/ {                                          # Pass the ip address to change to as a variable ipadd to awk
            cnt++                                                              # Where there is ipaddress in the line, increment a cnt variable
         } 
/ipaddress/ && cnt==2 {                                                        # Where there is ipaddress in the line and cnt is 2, process
            lne=gensub(/(^.*\")(.*)(\".*$)/,"\\1"ipadd"\\3",$0);               # Set a variable lne to an entry that substitutes the existing IP address for the one passed in ipadd using awk's gensub function
            print lne;                                                         # Print the amendment
            next                                                               # Skip to the next line
         }1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev        # Print all none amended lines and redirect the output to a temp file (devices.tmp) before over writing the devices.dev file with the temp file

这篇关于如何通过bash脚本访问.dev文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆