bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Commands executed with $($prog) do not run properly


From: Hans-Georg Bork
Subject: Re: Commands executed with $($prog) do not run properly
Date: Mon, 08 Nov 2010 16:51:00 +0100

Hi,

On Mon, 2010-11-08 at 09:49 -0500, Greg Wooledge wrote:
> On Mon, Nov 08, 2010 at 03:26:10PM +0100, Hans-Georg Bork wrote:
> > if this is true, then please explain the differences between
> >     $ find /test -type d ! -wholename "/test"
> > (/test not shown)
> > and
> >     $ prog='find /test -type d ! -wholename "/test"'
> >     $ echo $prog
> >     find /test -type d ! -wholename "/test"
> >     $ echo "$($prog)"
> > (/test shown)
> > (more results in original mail)
> 
> imadev:~$ args find /test -type d ! -wholename "/test"
> 7 args: <find> </test> <-type> <d> <!> <-wholename> </test>
> imadev:~$ prog='find /test -type d ! -wholename "/test"'
> imadev:~$ args $prog
> 7 args: <find> </test> <-type> <d> <!> <-wholename> <"/test">

this makes the whole thing now clear to me. I weren't aware that find
doesn't strip the quotes on itself, but takes them as part of the
pattern.

> 
> "args" is a script that I have in ~/bin.  Here is it:
> 
> imadev:~$ cat bin/args
> #! /bin/sh
> printf "%d args:" $#
> printf " <%s>" "$@"
> echo
> 
> I find it incredibly useful.

I do as well now. I copied it. Thanks for that.

> [...]
> GNU man page, I assume?  The one in Debian 5.0 has this example:
> 
>        -path pattern
>               File name matches shell pattern pattern.  The metacharacters  do
>               not treat `/' or `.' specially; so, for example,
>                         find . -path "./sr*sc"
> 
> This example is fine, and it demonstrates the use of quotes to protect
> a glob from exapansion BY A SHELL.  But then the shell removes the
> quotes before passing the glob on to find.  find should never see the
> actual quotes.  If it does, it will treat them as literal.  That is not
> what you want.

There was my misunderstanding. Thanks again for pointing me to that.

> 
> In your code, you were passing literal quotes to find along with the
> directory name.  This is why it failed.
> 
> The workarounds if you absolutely positively MUST put the argument vector
> into a variable are (1) eval, or (2) an array.  And I'd avoid eval if I
> were writing the script.  It's extremely dangerous, especially if your
> arguments are as complex as you say.
> 
> Since you appear not to have read http://mywiki.wooledge.org/BashFAQ/050
> yet, let me do all the hard reading for you.  

I read it; it just has a lot about arguments. I didn't realize that I
should translate it to commands as well.

> This is what you SHOULD have used:
> 
> prog=(find /test -type d ! -wholename "/test")
> echo "$( "address@hidden" )"

That example or a similar one would be nice in the FAQ. Thanks anyhow
for the clarification and your patience with me.

Regards
        -- hgb





reply via email to

[Prev in Thread] Current Thread [Next in Thread]