如何使用 bash 脚本修改 /etc/postfix/main.cf 文件中的某些值?

发布于 2025-01-15 21:46:02 字数 1041 浏览 0 评论 0原文

我需要更改文件/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韬韬不绝 2025-01-22 21:46:02

我认为你的问题在 var1 变量中。尝试这样的事情:

file1=/root/conf.txt
file2=/etc/postfix/main.cf
IFS=
\n'

for line in $(grep -E '^mynetworks|^myhostname.*com$|^inet_interface.*all
 $file1 )
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

I think your problem in in var1 variable. Try something like this:

file1=/root/conf.txt
file2=/etc/postfix/main.cf
IFS=
\n'

for line in $(grep -E '^mynetworks|^myhostname.*com$|^inet_interface.*all
 $file1 )
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文