LinuxQuestions.org
Visit Jeremy's Blog.
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-14-2006, 03:17 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Rep: Reputation: 30
Bash variable substitution


Hi,

My purpose is to compare the output of two commands with diff. I am trying to do this by assigning the two commands to two variables wherafter I use the variables with diff. Please let me know if I should use another approach to achieve my goal, I am new to bash programming.

The problem I have run into is that I have tried to assign a for loop to a variable like this:

Code:
name="for i in unzip; do dpkg -L $i; done"
but I get the next error when I do "$name":

Code:
-bash: for i in unzip ; do dpkg -L  ; done: command not found
Can someone show me what I am doing wrong?

Thanks for your help.

Ben
 
Old 04-14-2006, 04:18 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
It's not entirely clear to me what it is you want the script to do exactly. Anyways:
You need to prevent that $i is substituted ("expanded" in shell-script parlance) at the first assignment of the string. One way to do that is putting a backslash in fron of the $. Like this:
Code:
name="for i in unzip; do dpkg -L \$i; done"
Also, to have the shell (bash) execute the string, use eval, otherwise bash will treat the entire string (including whitespace) as a single command:
Code:
eval $name
 
Old 04-14-2006, 04:20 AM   #3
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,280

Rep: Reputation: 61
My programming is not the best, but if you want the value of a variable to contain a executed code try removing the double quotes and use this one instead and see if it will work:

name=`for i in unzip; do dpkg -L $i; done`
 
Old 04-14-2006, 02:16 PM   #4
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Hey thanks guys, your explanation helped me real well.

To explain what my goal is please assume there is another command assigned to a variable. For example:

name2="for i in gcc; do dpkg -L \$i; done"

Now I want to compare the output of this command and the previous command we mentioned with diff.

I have tried:

diff $name $name2

diff "$name" "$name2"

but these output errors.

So what command should I use to compare the output of the two commands?

Groeten/Regards,

Ben
 
  


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
Variable substitution (stupid noob) question in tcsh King of Men Linux - Newbie 4 12-04-2005 10:12 AM
Bash Process Substitution joshholt Programming 4 10-11-2005 04:15 AM
bash assignments of commands substitution p0rtius Programming 6 07-03-2005 07:41 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 05:38 PM
shell question variable substitution dpressm Programming 9 11-18-2004 08:00 AM


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