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. ...
- 03-04-2008 #1Just 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:
Note that the file contains # marks. I would like the script to treat them as comments and ignore anything behind a hash mark.Code:# filesystem type # read/write #device # Mount Point xfs w /dev/sda1 /mnt ext3 r /dev/hdc /mnt1
I then want to place the data in the above config file into variables so I can do something in the script like so:
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 xfs -w /dev/sda1 /mnt
I need to have comments in the config file so need to ignore them when sucking in the configuration.Code:mount -t $fstype $readwritebit $device $mountpoint
So, assuming that I have a config file as formatted above named mounts.cfg and the following shell script:
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.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
OH, I am using BASH to do this.
Thanks
Phil
- 03-04-2008 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Try this:
but remove the pipe to sh to be sure you get the right command.Code:awk 'NR > 1{print "mount -t " $1 " " $2 " " $3 " " $4}' "./mounts.cfg" | sh
Regards
- 03-04-2008 #3Just 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?
- 03-04-2008 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631


Reply With Quote
