LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Script to read config file (http://www.linuxquestions.org/questions/linux-software-2/script-to-read-config-file-4175429723/)

B Akshay 09-30-2012 08:47 AM

Script to read config file
 
hello,
The config file format which i designed for my script is,

*PROJECTNAME : PATH1(TO SOME SUB FOLDER) : PATH2(TO SOME OTHER SUB FOLDER).

PROJECTNAME will be given by user to the script.

I am able to read the config file using basic shell commands like grep, cut. But want to read it using sed or awk as no. of lines in code which i have written is more.

Also suggestion for config file format is requested.


Thanks in advance
Akshay

pixellany 09-30-2012 08:55 AM

I do not understand the question. Please show an example of how you want to process the file. Depending on what you want to do, there are many options available.

B Akshay 09-30-2012 10:24 AM

hello,

it is,
config file format:

proj1 : /abc/def/ijk : path/to/folder_1
proj2 : /efg/lmn/pqr : path/to/folder_2
proj3 : /xyz/ijk/mno : path/to/folder_3
.
.
.
.


The user will give the name of the project (i.e proj1/proj2/proj3) and the script should accordingly assign the corresponding paths to the variables which would be used for further processing.
Hope it is enough. I want to know how to do this reading of script and assignment of path to some variables by using sed /awk/perl

Habitual 09-30-2012 11:12 AM

show us the script you have so far.

B Akshay 10-01-2012 04:01 AM

following is the configuration file contents
#*PROJECTNAME : PATH TO THE FILE : PATH TO THE FOLDER
*proj1 : path1 for proj1 : PATH2 for proj1
*proj2 : path1 for proj2 : path2 for proj2
*proj3 : path1 for proj3 : PATH2 for proj3
*proj4 : PATH1 for proj4 : PATH2 for proj4
*proj5 : PATH1 for proj5 : PATH2 for proj5
*proj6 : PATH 1 for proj6 : PATH2 for proj6


the code which I have written is as follows,

read -p "ENTER PROJECT NAME" nam
CONFILE=sample_1.config
cnt=`grep -ic *$nam $CONFILE`
PRO=`grep -iw *$nam $CONFILE`
if [ $cnt -gt 0 ]
then
echo $PRO > CONFILE_0
pth_0=`cut -d ":" -f 2 CONFILE_0`
pth_1=`cut -d ":" -f 3 CONFILE_0`
else
echo "PROJECT NAME DOES NOT EXISTS IN CONFIG FILE"
exit
fi
echo $pth_0
echo $pth_1

B Akshay 10-01-2012 04:10 AM

i have included the script into my .bashrc the script is running but it gives an error "sample_1.config: No such File or Directory". I have kept the config file along with the script.


Thanks in advance

catkin 10-01-2012 04:48 AM

It is helpful to use CODE tags when posting code. The easiest way is to use Advanced editing and use the # button.

If sample_1.config exists but the script does not find it then the script's current working directory is not the one containing sample_1.config. The solution is either for the script to cd to the file's directory or to give the full path of the file.

B Akshay 10-01-2012 05:04 AM

Thank you!!! I will take care of it next time.

Can you suggest any easy way to read the config file. using sed or awk.
Also i am working on CRC generation using cksum command in linux, but want an alternate for it.

catkin 10-01-2012 05:11 PM

Quote:

Originally Posted by B Akshay (Post 4793778)
Can you suggest any easy way to read the config file. using sed or awk.
Also i am working on CRC generation using cksum command in linux, but want an alternate for it.

sed or awk could be used to read the config file and make values from it available to the script but I think grep would be easier. Is there any reason for preferring sed or awk?

Have you got your script working?

Best leave the checksum discussion in your other thread.

B Akshay 10-02-2012 06:34 AM

yes Catkin my script is working properly.
I have used grep and cut commands for reading the config file.

I thought awk & sed scripts won't be lengthy ...

Also i want some improvements/suggestions for the code(already posted)



Thanks for your response.

pan64 10-02-2012 07:46 AM

Code:

read -p "ENTER PROJECT NAME" nam  # this can be also implemented in awk

awk -F: ' /'$nam'/ { print "$2\n$3\n", exit } ' <config file>

it is not tested. Probably you need to specify your config file with full path

catkin 10-02-2012 02:10 PM

Assuming the proj<n> string normally appears once and only once in the config file, I would code something like this. Follows convention of using upper case variable names only for bash specials and environment variables:
Code:

#!/bin/bash

conf_file=sample_1.config
# Error trap.  Could also test whether it is an ordinary file
if [[ ! -r "$conf_file" ]]; then
    echo "ERROR: $conf_file not readable or does not exist"
    exit 1
fi

read -p "ENTER PROJECT NAME: " proj_nam

oIFS=$IFS  # Record value for restoring later
n_matches=0
while read -r line
do
    if [[ $line = "" ]]; then
        echo "ERROR: $proj_nam not found in $conf_file"
        exit 1
    fi
    ((n_matches++))
    IFS=:  # For splitting $line at : characters
    array=( $line )
    path1=${array[1]}  # May want to strip leading and trailing spaces too
    path2=${array[2]}
done <<< $( grep "*$proj_nam :" "$conf_file" )       

case $n_matches in
    1 )
        echo "DEBUG: path1: '$path1', path2: '$path2'"
        ;;
    * )
        echo "ERROR: $proj_nam found $n_matches times in $conf_file"
        exit 1
esac
IFS=$oIFS  # Restore original value

# Here do whatever you want with those paths

exit 0


B Akshay 10-03-2012 06:29 AM

Thank you catkin for your code!!!
i will work on it and then inform accordingly if any thing goes wrong.

cfajohnson 10-04-2012 11:04 PM


My configuration files are all valid shell scripts. That way, I can just source the file.

For example, for my scripting environment:

Code:

ScriptDir=$HOME/scripts
InstalDir=/usr/local/bin
BackupDir=$HOME/scripts/bak
ConfigDir=$HOME/.config
devel_suffix=-sh
bin_suffix=
VERSION_WIDTH=3
verbose=0
script_shell=/bin/bash



All times are GMT -5. The time now is 08:06 AM.