LinuxQuestions.org
Help answer threads with 0 replies.
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 11-26-2008, 11:04 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
Thumbs up Reading a bash variable in bash scripting problem


Hi,

I have a config file that contains:

my.config:
Code:
Section
INSTALLATION_PATH = $HOME
EndSection
Now in my bash script, I want to get the output /home/user instead of $HOME once read. So far, I have managed to get the $HOME variable but I can't get it to echo the variable. All I get is the output $HOME.

here is my parse_cmd script:
Code:
#Get the parameter from the section                                             
RESULT=`sed -n "/Section/,/EndSection/p" $1 | grep -w $3`                                                                            

#Ignore comment                                                                 
RESULT=`echo $RESULT | awk -F# '{print $1}'`

#remove all beginning white spaces                                              
RESULT=`echo $RESULT | awk -F= '{print $NF}' | awk '{sub("^ ", "", $0); print $0}'


#return value                                                                   
echo $RESULT

exit 0
Any idea how to get around the problem.

Thanks,
Indy
 
Old 11-26-2008, 11:23 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
eval echo $RESULT
 
Old 11-26-2008, 12:53 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

you can do the config parsing in one step:
Code:
jan@jack:~/tmp/config> cat cfg
Section
INSTALLATION_PATH = $HOME                 # the installation path
EndSection
jan@jack:~/tmp/config> eval echo `sed -nr '/^Section/,/^EndSection/{/Section/d;s/.* *= *//;s/#.*//;p}' cfg`
/home/jan
Jan
 
Old 11-27-2008, 02:29 AM   #4
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
thanks colucix, easy and neat.

Jan64,
I prefer not to do everything in one line. If it needs to be modified or improved, it's better policy in my opinion to have it separated into several commands than just one. Thanks for the tip though.

Indy
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading variable while remaining on the same line - bash GSMD Programming 3 04-24-2011 09:47 AM
bash scripting need help getting a variable of a variable dovo Programming 6 10-29-2008 01:30 PM
BASH scripting -- command saved as variable bioinformatics_guy Linux - Newbie 10 09-09-2008 08:24 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 01:30 AM
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 05:13 AM

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

All times are GMT -5. The time now is 12:03 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