Michael003
Michael003

Reputation: 442

Modify a part of a line on a file

I'm doing a script that should modify the value of a variable on a file which looks like something like that :

    smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = triton.itinet.fr
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = triton, localhost.localdomain, localhost

relayhost = smtp.free.fr
home_mailbox = Maildir/

virtual_mailbox_domains = $myhostname
virtual_mailbox_base = /var/mail/
virtual_mailbox_maps = hash:/etc/postfix/vmailbox

virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
#virtual_alias_maps = hash:/etc/postfix/virtual        

I want to modify the value of relayhost by the user entry, I have done this

    modify_relayhost ()
{
    read new_relayhost
    if (test -e /etc/postfix/main.cf)
    then
        grep "relayhost" /etc/postfix/main.cf | cut -d= -f1 && echo $new_relayhost
    fi    
}

But it does not modify the file, it just print me that value called relayhost and write the user's one on the prompt

Upvotes: 2

Views: 937

Answers (3)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

Using Perl:

host='new.host'

perl -pi.orig -e 's/^(\s*relayhost\s*=).*$/\1'"$host"'/' /etc/postfix/main.cf

With -p option, perl acts like a filter similar to grep, or sed. It actually adds while (<>) loop that reads the input line by line, and prints the result (to the file, due to -i option).

The -i.orig option creates a backup copy of the original file (main.cf.orig), and modifies the file in-place.

The substitution is applied for each line in the file. It captures the left part (\s*relayhost\s*=) and joins it with with the new value ($host).

Note that the substitution is applied for the first match only. If you want to replace all occurrences of the variable, use the g modifier.

Upvotes: 1

Inian
Inian

Reputation: 85530

This simple bash script could do the trick. Am using the read command to read the user prompt and sed for replacement

#!/bin/bash

read -p "enter relayhost value: " relayhost

if [ -f /etc/postfix/main.cf ];
then
    sed -i.bak -r "s/(relayhost *= *).*/\1$relayhost/" /etc/postfix/main.cf
fi

Running the script now

$ bash script.sh
enter relayhost value: abcd
$ cat /etc/postfix/main.cf
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = triton.itinet.fr
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = triton, localhost.localdomain, localhost

relayhost = abcd
home_mailbox = Maildir/

virtual_mailbox_domains = $myhostname
virtual_mailbox_base = /var/mail/
virtual_mailbox_maps = hash:/etc/postfix/vmailbox

virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
#virtual_alias_maps = hash:/etc/postfix/virtual

Upvotes: 1

anubhava
anubhava

Reputation: 784898

You can do this in a single sed command:

read new_relayhost

sed -i.bak -E 's/^([ \t]*relayhost[ \t]*=[ \t]*).*/\1'"$new_relayhost"'/' /etc/postfix/main.cf

This sed command finds relayhost key surrounded by optional spaces and replaces it's value part with value of variable $new_relayhost.

Upvotes: 1

Related Questions