LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-29-2010, 03:01 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
bash variable substitution


I have a config file (not a script) that states where to find a file named key.dat. With this config, I need to operate on the key.dat, so I did the following...

Code:
$ cat myconfig
keyfile=$mypath/key.dat

$ touch $mypath/key.dat # mypath is of course defined

$ KEYFILE=`grep '^keyfile=' myconfig | cut -d '=' -f 2`

$ echo $KEYFILE
$mypath/key.dat

$ [ -f $KEYFILE ] || echo 'Not Found'
Not Found
Since '$mypath' is not expanded in the test, the key.dat is not found.

My question is, how can I make $mypath expanded in the test so that I can get hold of the file key.dat?

Thanks.
 
Old 04-29-2010, 03:06 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
I think you don't need to mess around with grep. Just source the file. What is the output of the following?
Code:
. myconfig
echo $keyfile
Evo2.

PS. You say that myconfig is not a script, but from what you post above the syntax is bourne shell compatible, so sourcing it should work.

Last edited by evo2; 04-29-2010 at 03:07 AM.
 
Old 04-29-2010, 03:14 AM   #3
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Can't. The file is not executable. e.g. 664

and there are other stuff in the file that is not valid command.
 
Old 04-29-2010, 03:28 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
A file doesn't have to be executable in order to source it. Try the suggestion.
 
Old 04-29-2010, 03:35 AM   #5
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Right. That is true. Thank you both.

For the sake of learning, if sourcing the file doesn't work, e.g. myconfig does not contain valid bash syntax. Any work around to expand 'variable inside strings'?
 
Old 04-29-2010, 04:26 AM   #6
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
Quote:
Originally Posted by Jerry Mcguire View Post
My question is, how can I make $mypath expanded in the test so that I can get hold of the file key.dat?
The eval built-in may serve to force shell expansions. For example:
Code:
$ echo $mypath
/usr/local
$ eval $(grep '^keyfile=' myconfig)
$ echo $keyfile
/usr/local/key.dat
 
Old 04-29-2010, 09:33 AM   #7
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Thanks. This is what I needed.

Code:
$ KEYF=`eval echo $(grep '^keyfile=' myconfig | cut -d '=' -f 2)`

$ [ -f $KEYF ] && echo 'Found'
Found

Last edited by Jerry Mcguire; 04-29-2010 at 09:46 AM.
 
  


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 scripting - find command with variable substitution and quoting bgeddy Programming 4 07-29-2022 08:28 AM
Bash - Substring Extraction and Substitution on same variable jax8 Programming 5 04-26-2009 06:20 AM
bash asterisk variable substitution bt101 Linux - Software 3 02-08-2009 11:37 PM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM

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

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