user3147180 user3147180 - 1 year ago 202
Linux Question

How to read config files with section in bash shell

I have the configuration file like this in sections




I have the patterns which i want to exlude in rsync and other configuration data in that file

Now i am confused how can i use those patterns on command line

rsync -avz --exclude-from 'content from config file rsync exclude' source/ destination/

I am not sure how can read part of config file and then use on command line

Answer Source

To use --exclude-from you will have to isolate the relevant section of the config into a temporary file. This is easy to do with a bit of sed:

sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file > $tmp_file
rsync -avz --exclude-from $tmp_file source/ destination/

I am omitting error checking and cleanup for clarity.

Note that rsync can read the exclude pattern from the stdin for an - input, so this is even shorter:

sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file | \
  rsync -avz --exclude-from - source/ destination/


  • The 1,/rsync_exclude/d excludes all lines up to the rsync_exclude section entry
  • The /\[/,$d excludes everything from the start of the next section to the end of the file
  • The /^$/d excludes empty lines (this is optional)

All of the above extracts the relevant section from the config.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download