【问题标题】:How to access .dev file via bash script如何通过 bash 脚本访问 .dev 文件
【发布时间】:2021-03-22 19:46:01
【问题描述】:

下面是一个device.dev 文件,其中包含名为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    }

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

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

提前致谢

【问题讨论】:

  • "那么是否可以通过 bash 脚本访问 devices.dev 文件?" - 是的,这是可能的。你试过什么?你有什么问题?
  • @Tsyvarev。问题是因为我已经尝试过 ex。 .txt 上的 sed 并且它有效,但它没有在 .dev 文件上。但我会尝试 Pierluigu 的答案

标签: bash shell command-line kernel


【解决方案1】:

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

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

【讨论】:

  • 这就是我得到的mv: cannot stat ‘devices.tmp’: No such file or directory。此外,devices.dev 文件位于c:\Techsat\ADS2\config\devices.dev
  • tmp 文件名上有一个“错字”。立即尝试。
  • 谢谢伙计。你真是个金矿。但是,该文件只能位于c:\Techsat\ADS2\config\devices.dev。那么我可以通过只写路径而不是devices.dev来简单地引用它吗?
  • 事实证明它是直截了当的并且有效,只是传递了文件的路径。谢谢
  • 如果我能在 linkdn 上与您联系,我将不胜荣幸。)
【解决方案2】:

假设你想在第 30 行完全替换 value,你可以在 device.dev 的同一目录中创建这个文件 sed.sh

#!/bin/bash
IP=$1
sed -i "30s/ipaddress.*/ipaddress = \"$IP\"/" device.dev

并执行

bash sed.sh 1.2.3.4

device.dev 在第 30 行将有 1.2.3.4

【讨论】:

  • 感谢您的回答,如果文件位于某个目录怎么办?
  • 并且我想通过用户输入设置的第 30 行的 ip-adress 无法位于第 30 行。变量 ip-adress 被提及两次,要替换的一个是总是第二个
  • 如果文件位于另一个目录中,只需在脚本中硬编码 device.dev 的路径(您也可以使用 $2 unix.stackexchange.com/questions/298706/… 进行参数化)
  • 如果你要替换的行总是在某一行之后,我认为更简单的方法是指定一个范围来应用替换。尝试使用不同的选项修改 sed 命令,例如: sed -i "20,1000s/ipaddress.*/ipaddress = \"$IP\"/"" device.dev 将适用于第 20 到第 1000 行
猜你喜欢
  • 1970-01-01
  • 2017-03-28
  • 2014-04-20
  • 2015-01-15
  • 2014-09-16
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多