LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-20-2008, 12:27 PM   #1
matiasquestions
Member
 
Registered: Sep 2005
Posts: 65

Rep: Reputation: 15
Bash strings evaluation


Hi,

I've the following variable:

Code:
# echo $BINMAKE
`if [ -x /usr/obj/usr/src/make.i386/make ]; then echo /usr/obj/usr/src/make.i386/make; else echo make; fi` -m /usr/src/share/mk
#
It's a string wich includes a commmand.

How can I get the subcommand replaced by its output?

I'd like to have a second variable like:

Code:
# echo $newvariable
make -m /usr/src/share/mk 
#

Last edited by matiasquestions; 08-20-2008 at 01:00 PM.
 
Old 08-20-2008, 12:48 PM   #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
It should work the way you have written. Better to use the $(command) syntax instead of back ticks, for various reasons. Anyway it seems correct to me. Can you explain a bit more what the problem is?
 
Old 08-20-2008, 01:00 PM   #3
matiasquestions
Member
 
Registered: Sep 2005
Posts: 65

Original Poster
Rep: Reputation: 15
How would you assign the value to $myvariable so that the output on my main post is consistent?


That's the problem..........
 
Old 08-20-2008, 01:28 PM   #4
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
Have you any control on how the BINMAKE variable is created? It looks like single quotes have been used, whereas double quotes should do the trick:
Code:
# BINMAKE='`if [ -x /usr/obj/usr/src/make.i386/make ]; then echo /usr/obj/usr/src/make.i386/make; else echo make; fi` -m /usr/src/share/mk'

# echo $BINMAKE
`if [ -x /usr/obj/usr/src/make.i386/make ]; then echo /usr/obj/usr/src/make.i386/make; else echo make; fi` -m /usr/src/share/mk

# BINMAKE="`if [ -x /usr/obj/usr/src/make.i386/make ]; then echo /usr/obj/usr/src/make.i386/make; else echo make; fi` -m /usr/src/share/mk"

# echo $BINMAKE
make -m /usr/src/share/mk
 
Old 08-20-2008, 01:32 PM   #5
matiasquestions
Member
 
Registered: Sep 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Nope, I've no control on how BINMAKE is created.... :-(
 
Old 08-20-2008, 01:48 PM   #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
Ok. This is a complex variable substitution and evaluation problem (please, don't ask me to explain in details). You have to put the whole assignment statement in a dummy variable, then evaluate that variable. Better to explain with a real example:
Code:
# BINMAKE='`if [ -x /usr/obj/usr/src/make.i386/make ]; then echo /usr/obj/usr/src/make.i386/make; else echo make; fi` -m /usr/src/share/mk'

# echo $BINMAKE
`if [ -x /usr/obj/usr/src/make.i386/make ]; then echo /usr/obj/usr/src/make.i386/make; else echo make; fi` -m /usr/src/share/mk

# PLUTO="PIPPO=\"$BINMAKE\""

# eval $PLUTO

# echo $PIPPO
make -m /usr/src/share/mk
in the example above you put the assignment statement in the variable PLUTO, then eval $PLUTO to actually assign the variable PIPPO. After that you can unset PLUTO and use PIPPO the way you want.

PS - PIPPO and PLUTO are italian translations of Walt Disney characters Goofy and... Pluto. I often use them in my examples and as temporary file names.
 
Old 08-20-2008, 05:18 PM   #7
matiasquestions
Member
 
Registered: Sep 2005
Posts: 65

Original Poster
Rep: Reputation: 15
WoW!!!! That works perfect!!! thanks a lot for your help!! :-D
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with bash and strings Histamine Programming 4 06-27-2007 05:18 PM
BASH: Output everything between two strings systemparadox Programming 2 12-18-2004 11:26 AM
bash and strings graziano1968 Linux - Software 2 10-01-2004 07:50 AM
BASH variables evaluation question. Thetargos Programming 11 08-11-2003 05:11 AM
adding strings in bash FireAge Linux - General 4 03-11-2003 11:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:00 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
Open Source Consulting | Domain Registration