【发布时间】:2012-10-10 08:13:08
【问题描述】:
我在使用更改名为“.backup.conf”的配置文件的 shell 脚本时遇到了一些问题。
配置文件如下所示:
inputdirs=(/etc /etc/apm /usr/local)
outputdir="test_outputdir"
backupmethod="test_outputmethod"
loglocation="test_loglocation"`
我的脚本需要更改配置文件变量之一,而最后 3 个变量我没有遇到任何问题。
如果我想将变量 "inputdirs" /etc/ 更改为 /etc/perl,我应该使用什么表达式?
如果我将 echo 与 append 一起使用,它只会将其附加到文件的末尾。
我尝试使用以下格式的 sed:
sed -i 's/${inputdirs[$((izbor-1))]}/$novi/g' .backup.conf 其中“izbor”是我想从 inputdirs 更改的变量,“novi”是新路径(例如 /etc/perl)。
因此,使用以下配置文件,并使用变量$izbor=1和$novi=/etc/perl,我应该将第一个变量inputdirs=/etc更改为/etc/perl
并且变量 inputdirs 最终应该看起来像 inputdirs=(/etc/perl /etc/apm /usr/local)
感谢您的帮助!
【问题讨论】:
-
Sed 是一个流编辑器。您正在对字符串进行 RegEx 搜索/替换...但尝试对数组进行操作?
-
如果与 /g 一起使用,sed 不应该在整个文件中搜索条件吗?
-
g不是这样做的,g是“全部替换”。但无论如何 - 您的搜索表达式无效。 grymoire.com/Unix/Sed.html#uh-1 -
您介意纠正它还是建议一些变量?我完全没有想法......
标签: linux bash shell configuration