LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  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: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

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


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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