Bash脚本,sed有两个变量不起作用

时间:2011-11-01 19:23:00

标签: bash

我正在编写一个脚本,每隔一段时间更改一次IP地址,但由于某种原因我无法正常工作

sed "s/$curLine/$nextLine/" </etc/network/interfaces>/etc/network/interfaces.new,即使我改变了结尾/ g,我也总会得到“sed unterminated`s”命令“

这是我的变量$ curLine和$ nextLine:

#Find current ip from /etc/network/interfaces

    curLine=$(sed -n "/address/p" /etc/network/interfaces)

#Find location of current ip in ips.txt.

    newLine=$(sed -n "/$curLine/=" ips.txt)

#Check and set next ip

    nextIP=$(($newLine+1))

    nextLine=$(sed -n "$nextIP,/address/p" ./ips.txt)

由于某种原因$ nextLine也给了我两个ips我不知道为什么因为在开始时工作得很好。

并且最终这条线不能正常工作。

"s/$curLine/$nextLine/" </etc/network/interfaces>/etc/network/interfaces.new

错误消息“sed unterminated`s”命令“。如果我用“地址xxx.xxx.xxx.xx”之类的文本替换$ nextline,那就可以了。

我已经阅读了很多主题,其中解决方案是使用双引号,但我不知道为什么它不适用于我。我也有一本书,其中一个例子基本上和我正在做的一样,但是当我尝试使用它时它再次起作用。

谢谢!

2 个答案:

答案 0 :(得分:0)

算术的语法没有额外的含义:

NUM=1
OTHERNUM=$((NUM + 1))
#        ^^^^^^^^^^^^

换句话说,请说nextIP=$((newLine+1))

答案 1 :(得分:0)

问题解决了。我刚刚改变了

nextLine=$(sed -n "$nextIP,/address/p" ./ips.txt)

搜索行的范围而不是带有模式的特定行。

nextLine=$(sed -n "$nextIP,/$nextIP" ./ips.txt)
相关问题