Find the answer to your Linux question:
Results 1 to 4 of 4
I am trying to write a shell script that will suck in data from a config file. The script should mount device nodes that are contained in a config file. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2

    Assistance with script & parsing of a config file with bash...


    I am trying to write a shell script that will suck in data from a config file. The script should mount device nodes that are contained in a config file. The config file would be named mounts.cfg and would look like this:

    Code:
    # filesystem type         # read/write  #device         # Mount Point
    xfs                            w                 /dev/sda1      /mnt
    ext3                           r                 /dev/hdc        /mnt1
    Note that the file contains # marks. I would like the script to treat them as comments and ignore anything behind a hash mark.

    I then want to place the data in the above config file into variables so I can do something in the script like so:

    Code:
    mount -t xfs -w /dev/sda1 /mnt
    where the type of the filesystem, whether it is read or write capable and the device and mount points become varibles so I can have something like this in the code of the script:

    Code:
    mount -t $fstype $readwritebit $device $mountpoint
    I need to have comments in the config file so need to ignore them when sucking in the configuration.

    So, assuming that I have a config file as formatted above named mounts.cfg and the following shell script:


    Code:
    FILE="./mounts.cfg"
    
    while read line
    do
            if [ -z "echo $line | sed '/^ *#/d;s/#.*//'" ]
            then
                    next;
            else
                    mount -t echo "$line | sed '/^ *#/d;s/#.*//'`";
            fi
    done < $FILE
    it should work? Well, it doesn't and I am just smart enough to know that my code is ugly and I am doing things all wrong. I know it is challenging since I have more than one possible line in the file. I guess that means I need to use a loop of some sort but am having real trouble. I have tried a lot of different things and I keep running up against another reason why it will not work. Any help would be greatly appreciated.

    OH, I am using BASH to do this.

    Thanks

    Phil

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    awk 'NR > 1{print "mount  -t " $1 " " $2 " " $3 " " $4}' "./mounts.cfg" | sh
    but remove the pipe to sh to be sure you get the right command.

    Regards

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2
    I knew I was going about this the wrong way. I had originally tried to do this with awk. My questions is, we the above command be executed for each row of the file?

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by pryker View Post
    I knew I was going about this the wrong way. I had originally tried to do this with awk. My questions is, we the above command be executed for each row of the file?
    The answer is yes, and once again, try the command without the pipe command at the end and check the output.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •