LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-30-2012, 07:47 AM   #1
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Rep: Reputation: Disabled
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
 
Old 09-30-2012, 07:55 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
1 members found this post helpful.
Old 09-30-2012, 09:24 AM   #3
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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

Last edited by B Akshay; 09-30-2012 at 09:26 AM.
 
Old 09-30-2012, 10:12 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
show us the script you have so far.
 
Old 10-01-2012, 03:01 AM   #5
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-01-2012, 03:10 AM   #6
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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

Last edited by B Akshay; 10-01-2012 at 03:11 AM.
 
Old 10-01-2012, 03:48 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.

Last edited by catkin; 10-01-2012 at 03:50 AM.
 
Old 10-01-2012, 04:04 AM   #8
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-01-2012, 04:11 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by B Akshay View Post
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.
 
Old 10-02-2012, 05:34 AM   #10
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-02-2012, 06:46 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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
 
Old 10-02-2012, 01:10 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 10-03-2012, 05:29 AM   #13
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Thank you catkin for your code!!!
i will work on it and then inform accordingly if any thing goes wrong.
 
Old 10-04-2012, 10:04 PM   #14
cfajohnson
LQ Newbie
 
Registered: Aug 2012
Distribution: Linux Mint 17
Posts: 22

Rep: Reputation: Disabled

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

Last edited by cfajohnson; 10-04-2012 at 10:05 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 11:29 AM
[SOLVED] script to read number from config file mcollis Programming 6 09-13-2011 07:52 AM
How to Read Config file manohar Programming 7 12-30-2010 06:47 AM
read-only file system error when editing a config file smallfrowne Linux - Newbie 5 02-19-2008 06:06 PM
read options from config file biiiep Programming 4 05-05-2005 03:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration