track ip addresses, phone numbers, etc

Shell script to read several conf files and create archives

Published on 01 April 2008

Read several conf files, find user directory, create archive directory and log file

As a shell scripter, I use variations of bash script like this often. Note that this particular is not very robust, but you get the idea. As always, use code from this section at your own risk.


# Title: Create empty files under several directories mentioned in *.conf
# Author: Arul John
# Description:
# 0. Each conf file contains key=value pairs, one per line
# 1. Read conf files in ~/config/*.conf
# 2. In each conf file, search for USERDIR=(.+) and create directory ~/$1/archive
# 3. In each ~/*/archive directory, create file webserver.log

set +x # comment this to turn off debug

# List all files and dump into $CONFFILES
CONFFILES=(`ls -1 ~/config/*.conf`)

# Loop and read 
for FILE in "${CONFFILES[@]}"
    # Get USERDIR value from conf file
    while read line
        if [[ "$line" =~ "USERDIR=(.+)" ]]
    done < "$FILE"
    if [[ $USERDIR != "" ]]
    echo "Creating directory ~/$USERDIR/archive"
        mkdir $USERDIR/archive/
        if [[ -d $USERDIR/archive/ ]]
            echo "Creating webserver.log under archive directory"
            touch $USERDIR/archive/webserver.log

Created on 01 April 2008

TAGS: shell, conf

Affiliate Disclosure: Some of the links to products on this blog are affiliate links. It simply means, at no additional cost to you, we’ll earn a commission if you click through and buy any product.