-2

I'm expecting this to be an easy one for someone (alas not me!).

Using a bash script, I want to replace a value in a config file '/etc/app/app.cfg' (ini style), using variables for both search and replace.

The Value Name and Value I wish to update (note the space either side of the equals:

LOGDIR = /etc/app/logs

I have defined the following in the bash script:

# Get existing LogDir value 
CURRENT_LOGDIR=$(grep 'LogDir =' /pathtofile | sed 's/LogDir *= *//g')

# Set New LogDir
LOGDIR=/mnt/eft/fs1/logs

# Update LogDir if different
if [[ -d $(echo $CURRENT_LOGDIR) != $LOGDIR ]] ; then
  # Update LogDir value:
  **bash command - I need help with !**
fi

I have tried many combinations with sed, to no avail, hence asking this question.

Things I've tried:

echo "LogDir = $LOGDIR" | sed '#s/$CURRENT_DIR/$LOGDIR/#g' /etc/app/app.cfg
sed -i '/#/!s/\(LogDir[[:space:]]*=[[:space:]]*\)\(.*\)/\1$LOGDIR#/g' /etc/app/app.cfg
sed -i 's/LogDir[[:space:]]=.*/LogDir = {LOGDIR}/' /etc/app/app.cfg
sed -i "s/^LogDir[[:space:]]*=.*/LogDir=$LOGDIR/}" /etc/app/app.cfg
sed -i '/#/!s/\(LogDir[[:space:]]*=[[:space:]]*\)\(.*\)/\1"$LOGDIR"/' /etc/app/app.cfg

Desired output:

Update LogDir value in /etc/app/app.cfg

For example:

LogDir = /mnt/eft/fs1/logs

user51760
  • 127
  • 8
  • (If you remove the invalid `-d` in `[[ -d ... ]]`, to make it syntactically correct) [Shellcheck](https://www.shellcheck.net/) finds several problems with the code. – pjh Mar 04 '22 at 11:21
  • It the variable name in the configuration file always starts in column 1 and has a space after it, I would consider an `awk` solution. – user1934428 Mar 04 '22 at 13:18

3 Answers3

1

What is that } doing on the end? Looks like a typo.

sed "s/^LogDir[[:space:]]*=.*/LogDir=$LOGDIR/" /etc/app/app.cfg

And sed edit file in place

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

Using sed

$ LOGDIR=/mnt/eft/fs1/logs
$ sed -i.bak "s|\([^=]*=[[:space:]]\).*|\1$LOGDIR|" /etc/app/app.cfg
$ cat /etc/app/app.cfg
LOGDIR = /mnt/eft/fs1/logs
HatLess
  • 10,622
  • 5
  • 14
  • 32
0

How about this:

awk '$1 == "LogDir" {print "LogDir = /mnt/eft/fs1/logs"; next} {print}' old_configuration_file >new_configuration_file

The first awk clause replaces the old LogDir entry by the new one, and the second clause passes the other lines through unchanged.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Couldn't get this to work with the LOGDIR variable. Probably just a quote/escape issue: `awk '$1 == "LogDir" {print "LogDir = $LOGDIR"; next} {print}' old_configuration_file >new_configuration_file` – user51760 Mar 07 '22 at 08:56
  • This would indeed not expand `$LOGDIR`, because it is between single quotes; but you don't need the **shell** to expand it. You can also either turn the shell variable into an **awk** variable (see the `-v` option in the _awk man page_), or instead make `LOGDIR` an **environment** variable and [access it from within awk](https://stackoverflow.com/questions/46171193/how-do-i-use-environment-variables-in-awk#46171338) via the predefined `ENVIRON` array - it's a matter of taste, what you find easier to read afterwards. – user1934428 Mar 07 '22 at 09:55