Skip to content
Advertisement

How to read a variable from file, modify and safe it to an other variale

What I want:

There is a file /scripts/backup/config.cfg which contains variables. In my specific case the important ones are:

JavaScript

Then there is a script /scripts/backup/performBackup.sh
For a specific reason I want a part of the script do the following operations:

  1. read the value of the variable ROOTLOCATION
  2. add a (“/” and) timestamp (Date&Time)
  3. safe the new created value to BACKUPLOCATION (by replacing its current value)

Example

If this is the previous state of the config.cfg:

JavaScript

After the script ran it should be:

JavaScript

What I tried

First of all the config file gets “loaded” using

JavaScript

I then tried to use the sed command but the quotes are messing with me. Here is one try (which didn’t work):

JavaScript

Advertisement

Answer

Try this:

JavaScript

The problem with your sed is that you use / as delimiter, which is present in $ROOTLOCATION after expansion, therefore sed fails. I used |, which is usually is not present in filenames. If you ever create a file with |, that sed will fail too! So, “know your data” 🙂

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement