如何使用 bash 脚本修改 /etc/postfix/main.cf 文件中的某些值?
我需要更改文件/etc/postfix/main.cf 中的一些注释键值对。我没有尝试更改值,而是尝试将这些键值对附加到文件底部,因为这也有效。因此,我将这些键值对保存在一个单独的文件中,并尝试在代码中将这些键值对作为字符串获取。但是,在从文件中获取这些对时,循环中仅获取代码中的第一个字符串,而不是后面的字符串。我不明白代码中哪里出错了。这是我的代码 -
file1=/root/conf.txt
file2=/etc/postfix/main.cf
IFS=$'\n'
var1=($(grep -E '^mynetworks|^myhostname.*com$|^inet_interface.*all$' $file1))
echo ${var1[0]}
echo ${var1[1]}
echo ${var1[2]}
for line in $var1
do
grep -q $line $file2
if [ $? -eq 1 ]
then
echo "Strings are added to the target file!"
echo $line >> $file2
else
echo "String already exist in the file!"
fi
done
文件“conf.txt”的文件内容 -
mynetworks = 127.0.0.0/8, 168.100.189.0/28
myhostname = centos7.example.com
inet_interface = all
我在 main.cf 末尾得到的输出
mynetworks = 127.0.0.0/8, 168.100.189.0/28
- 我希望在 main.cf 末尾得到的输出 -
mynetworks = 127.0.0.0/8, 168.100.189.0/28
myhostname = centos7.example.com
inet_interface = all
I need to change some commented key value pairs in the file /etc/postfix/main.cf. Rather than changing the values i'm trying to append those key value pair at the bottom of the file, because this also works. So I've kept those key value pairs in a separate file and trying to fetch those pairs as a string in my code. But while fetching those pairs from the file, only the first string in code is getting fetched in the loop and not the later ones. I don't understand where i'm going wrong in the code. Here is my code -
file1=/root/conf.txt
file2=/etc/postfix/main.cf
IFS=
file content of the file 'conf.txt' -
mynetworks = 127.0.0.0/8, 168.100.189.0/28
myhostname = centos7.example.com
inet_interface = all
output I get at the end of main.cf-
mynetworks = 127.0.0.0/8, 168.100.189.0/28
Output I desire at the end of the main.cf-
mynetworks = 127.0.0.0/8, 168.100.189.0/28
myhostname = centos7.example.com
inet_interface = all
\n'
var1=($(grep -E '^mynetworks|^myhostname.*com$|^inet_interface.*all
file content of the file 'conf.txt' -
output I get at the end of main.cf-
Output I desire at the end of the main.cf-
$file1))
echo ${var1[0]}
echo ${var1[1]}
echo ${var1[2]}
for line in $var1
do
grep -q $line $file2
if [ $? -eq 1 ]
then
echo "Strings are added to the target file!"
echo $line >> $file2
else
echo "String already exist in the file!"
fi
done
file content of the file 'conf.txt' -
output I get at the end of main.cf-
Output I desire at the end of the main.cf-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题在 var1 变量中。尝试这样的事情:
I think your problem in in var1 variable. Try something like this: