Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Edit config file

I currently work on writing bash scripts and I want to automize as much as possible in a safe and easy way. I need to edit config files in my bash script. The config files (e.g. /etc/fail2ban/jail.local on ubuntu) may look like this:

#
# SSH
#

[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = $MAXRETRY_

[ssh-blacklist] 
enabled = true
port = ssh
filter =sshd

Now I want to have different bash functions:

  1. set_config_value() {...}

  2. get_config_value() {...}

  3. remove_config_value() {...}

Is there already some code that implements these functions? Is it a good idea to use sed to implement parts of these functions?

This is what I have so far:

filepath='./testfile.txt'
cat > "$filepath" <<-EOF
#
# SSH
#

[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 10

[ssh-blacklist] 
enabled  = true
port     = ssh
filter   = sshd
EOF

function set_config_value() {
arrname_="$1"
varname_="$2"
varvalue_="$3"
filepath_="$4"

sed  -i '
    /^\['$arrname_'\]/,/\[.*\]/ {
        s/'$varname_'.*/'$varname_'  = '$varvalue_'/
    }
    ' "$filepath_"  
}

arrname='ssh'
varname='port'
varvalue='123'
set_config_value "$arrname" "$varname" "$varvalue" "$filepath"

For better understanding and how the functions should work, here some examples building up on the script from above:

set_config_value

$arrname not present in file should append the new config:

set_config_value abc port 123 "$filepath"

Should give

#
# SSH
#

[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = $MAXRETRY_

[ssh-blacklist] enabled = true
port = ssh
filter =sshd

[abc]
port     = 123

$arrname present in file, but $varname not present should append the new subsection to the right block:

set_config_value ssh xyz 123 "$filepath"

Should give

#
# SSH
#

[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = $MAXRETRY_
xyz      = 123

[ssh-blacklist] enabled = true
port = ssh
filter =sshd

$arrname present in file, $varname present in section should just modify the corresponding line:

set_config_value ssh port 123 "$filepath"

Should give

#
# SSH
#

[ssh]
enabled  = true
port     = 123
filter   = sshd
logpath  = /var/log/auth.log
maxretry = $MAXRETRY_

[ssh-blacklist] enabled = true
port = ssh
filter =sshd

remove_config_value

$arrname present in file, $varname present in section:

remove_config_value ssh port "$filepath"

Should give

#
# SSH
#

[ssh]
enabled  = true
filter   = sshd
logpath  = /var/log/auth.log
maxretry = $MAXRETRY_

[ssh-blacklist] enabled = true
port = ssh
filter =sshd

$arrname not present in file should not modify the file at all:

remove_config_value abc port "$filepath"

Should give

#
# SSH
#

[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = $MAXRETRY_

[ssh-blacklist] enabled = true
port = ssh
filter =sshd

[abc]
port     = 123

Thank you in advance for your answers and suggestions for solutions!

Comments