Technology Questions

Go Back   Technology Questions > Software Questions > Operating System Questions > Linux

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 03-20-2008, 06:00 AM
wylbur37
Newsgroup Contributor
 
Posts: n/a
running a command stored in a script variable

Consider the following script ...

#!/bin/bash
i="one two three.wav"
ls -l "${i}"
out_linetext="ls -l \"${i}\""
echo "generated stmt ... [$out_linetext]"
$out_linetext

when this is run, the results are ...

/home/user> xls
-rw-r--r-- 1 user user 5365804 2008-01-26 09:40 one two three.wav
generated stmt ... [ls -l "one two three.wav"]
ls: "one: No such file or directory
ls: two: No such file or directory
ls: three.wav": No such file or directory
/home/user>

note that "ls" is run twice.

The first time ...

ls -l "${i}"

it works with no problem.

The second time, I simply constructed the entire statement
and stored it into a variable and then attempted to "run"
what was in that variable ...

$out_linetext

and that's when the errors occurred.
you can see from the echo of the value of $out_linetext
that the statement was constructed with the correct syntax.

So why did it work the first time but not the second time?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 03-20-2008, 06:00 AM
  #2 (permalink)  
Old 03-20-2008, 06:20 AM
pk
Newsgroup Contributor
 
Posts: n/a
Re: running a command stored in a script variable

wylbur37 wrote:

> Consider the following script ...
>
> #!/bin/bash
> i="one two three.wav"
> ls -l "${i}"
> out_linetext="ls -l \"${i}\""
> echo "generated stmt ... [$out_linetext]"
> $out_linetext


i="one two three.wav"
ls -l "${i}"

The above command looks for a file named 'one two three.wav' (which is a
legal file name). If you have such a file in the current directory (which I
suppose is the case since you get no errors), the command succeeds.

out_linetext="ls -l \"${i}\""

After this command, the variable out_linetext contains exactly the
following:

ls -l "one two three.wav"

If you try to run this as a command, the shell does not re-evaluate the
command and treats '"one', 'two' and 'three.wav"' as three different file
names (all legal) and does not find them.
What you really want is probably

eval "$out_linetext"

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 03-23-2008, 11:10 AM
wylbur37
Newsgroup Contributor
 
Posts: n/a
Re: running a command stored in a script variable

On Mar 20, 10:30*am, pk <p...@pk.pk> wrote:
> What you really want is probably
> eval "$out_linetext"


Yes, using "eval" did the trick.
Thanks very much!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command prompt random variable jimshoe2112 Windows XP 1 09-20-2007 11:20 PM
How select a Sheet if the Name is stored in a variable TeddyBear Microsoft Office 1 07-29-2007 05:01 AM
Specify environmental variable when starting command prompt? Don Culp Windows XP 1 07-19-2007 04:40 AM
How do you pass a vb script variable to a batch file? Mike Windows XP 13 01-04-2007 03:03 AM
How do you pass a vb script variable to a batch file? Mike Windows XP 2 01-04-2007 03:02 AM


New To Technology Questions? Do You Need Help with Your Computer or Device? Do You Need Help with this site?

All times are GMT -8. The time now is 03:02 AM.


2003 - 2009 All Rights Reserved. Technology Questions

Search Engine Friendly URLs by vBSEO 3.3.0