LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-12-2005, 06:16 AM   #1
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Rep: Reputation: 15
Post BASH Shell program Read a configuration File


hi
what is the Shell program to read the configuration file given below

-----------------------------------------------------------

#C program Directory
c_dir "/home/minil/"

#server ip address
server_ip 192.168.48.100

#user login name
logname "minil"

-------------------------------------------------------------
i need to output only as follows

"/home/minil"
192.168.48.100
"minil"

Thanks in Advance
 
Old 01-12-2005, 07:33 AM   #2
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
Hmm... I think you should be interested by the command 'source'

If you type:
source config_file
then the variable would be set properly within the bash.

Nevertheless the appropriate syntax would be (Cf. bashrc or bash_profile):
C_DIR=/home/minil

And you could use it under bash with $C_DIR

For instance, to compile the file prog.c in this directory:
g++ $C_DIR/prog.c

Hoping it would give you clues to solve your problem.

Cheers, PY
 
Old 01-12-2005, 08:02 AM   #3
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
It shows me error

[root@laxman Backup]# source vs.conf
bash: c_dir command not found
bash: server_ip: command not found
bash: logname : command not found
 
Old 01-12-2005, 09:15 AM   #4
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
Hmm... Reread my message, there is a clue !
 
Old 01-12-2005, 09:45 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
summink like this....

Code:
billym.primadtpdev>cat ~/2
#C program Directory
c_dir "/home/minil/"

#server ip address
server_ip 192.168.48.100

#user login name
logname "minil"
Code:
billym.primadtpdev>cat ~/1

grep '^[^#].*' |
while read one two;do
    echo $two
done
Code:
billym.primadtpdev>bash ~/1 < ~/2
"/home/minil/"
192.168.48.100
"minil"
 
Old 01-12-2005, 05:24 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
To parse fields separated by whitespace, i'd use awk, but ala pycoucou, I'd recommend instead separating by '=' in a cfg file.
If it's good enough for .bashrc/.bash_profile, it's good enough for me
 
Old 01-13-2005, 07:39 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
Quote:
To parse fields separated by whitespace, i'd use awk, but ala pycoucou, I'd recommend instead separating by '=' in a cfg file.
If it's good enough for .bashrc/.bash_profile, it's good enough for me
naaah! You can do it like this...

Code:
billym.primadtpdev>cat ~/2
STARTER  soup 
DINNER  fish and chips
PUDDING   lemon cheesecake
Code:
billym.primadtpdev>cat ~/1
while read var param;do
    eval $var=\\'$param\\'
done
echo $STARTER, then $DINNER followed by $PUDDING
Code:

billym.primadtpdev>bash ~/1 < ~/2
soup, then fish and chips followed by lemon cheesecake
delicious!
 
Old 01-16-2005, 11:43 PM   #8
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
can u explain me the following line
grep '^[^#].*'
 
Old 01-16-2005, 11:55 PM   #9
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
Another solution
------------------------
/home/minil/ cat ~/temp.conf

#C program Directory
c_dir="/home/minil/"

#server ip address
server_ip= 92.168.48.100

#user login name
logname="minil"

--------------------------

code
-------------------------------------------------------
/home/minil/ cat ~/1.sh
#!/bin/sh

. ./temp.conf # To import temp.conf into our file
echo $c_dir
echo $server_ip
echo $logname



--------------------


code 3
------------------------------------------------
/home/minil/ sh 1.sh

"/home/minil"
192.168.48.100
"minil"
----------------------------------------------
 
Old 01-17-2005, 04:16 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
of course minil, even better! [removing hat]

Quote:
can u explain me the following line
grep '^[^#].*'
clunky way of excluding comments assuming the start in column 1.
in english:
grep only lines that don't start with a '#' .
 
Old 01-17-2005, 04:37 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
do a 'man regexp'

^ means start of line
[^xyz] means a character but not 'x' 'y' 'z'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM
How do I read from a file in BASH? vous Programming 4 03-22-2005 06:51 PM
Simple Bash Shell Program frankblack Programming 2 02-14-2003 11:59 PM
bash shell program help embsupafly Programming 7 11-27-2002 12:05 AM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration