LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 12:04 PM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Rep: Reputation: 30
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, 12:23 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

Rep: Reputation: 46
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, 03:29 AM   #4
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Original Poster
Rep: Reputation: 30
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 10:47 AM
bash scripting need help getting a variable of a variable dovo Programming 6 10-29-2008 02:30 PM
BASH scripting -- command saved as variable bioinformatics_guy Linux - Newbie 10 09-09-2008 09:24 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 02:30 AM
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 06:13 AM


All times are GMT -5. The time now is 10:53 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration