From: lbrtchx on
~
it amazes me how fragile bash scripting is
~
you try first things on the command prompt which works just find but then when you paste your command on a bash files thing stops working. As I see "I am not the only one". I'd wish I could not the reason why that happens to be smarter about this
~
I am just trying to use find with printf and exec(dir). I found a few such commands that work, but their results was not what I needed
~
# BSLSH
_BSLSH="\\"


# SPACE
_SPC=" "

# FIND DIRECTORY
_FND_DIR=/UNIONFS/var

# EXCLUDING (should be done in a loop and using awk)
_XKLD="-noleaf -wholename '/proc' -prune -o -wholename '/media/sda1' -prune"

#eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s %d ' -exec md5sum -b {} \; > 2sort_md5sum.tmp
#find: paths must precede expression: %d

#eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s%d' -exec md5sum -b {} \; > 2sort_md5sum.tmp
#find: missing argument to `-exec'

#eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s%d' -exec md5sum -b '{}' \; > 2sort_md5sum.tmp
#find: missing argument to `-exec'

#eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s%d' -exec md5sum -b {} ${_BSLSH}; > 2sort_md5sum.tmp
#find: missing argument to `-exec'

#eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s%d' -exec md5sum -b '{}' ${_BSLSH}; > 2sort_md5sum.tmp
#find: missing argument to `-exec'

eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s${_SPC}%d${_SPC}' -exec md5sum -b '{}' ${_BSLSH}; > 2sort_md5sum.tmp
#find: missing argument to `-exec'
From: Ben Bacarisse on
lbrtchx(a)gmail.com writes:

You might want to check your newsreader's setup. I find the added ~s
and the unwrapped paragraphs make your posts hard to read.

> ~
> it amazes me how fragile bash scripting is
> ~
> you try first things on the command prompt which works just find
> but then when you paste your command on a bash files thing stops
> working. As I see "I am not the only one". I'd wish I could not the
> reason why that happens to be smarter about this
> ~
> I am just trying to use find with printf and exec(dir). I found a
> few such commands that work, but their results was not what I
> needed
> ~
> # BSLSH
> _BSLSH="\\"

Why do you put _ in front of your bash variable names?

> # SPACE
> _SPC=" "
>
> # FIND DIRECTORY
> _FND_DIR=/UNIONFS/var
>
> # EXCLUDING (should be done in a loop and using awk)
> _XKLD="-noleaf -wholename '/proc' -prune -o -wholename '/media/sda1' -prune"
>
> #eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s %d ' -exec md5sum -b {} \; > 2sort_md5sum.tmp
> #find: paths must precede expression: %d

This, and all the subsequent errors, are caused by the eval. Why are
you using eval? It is quite rare in shell scripting and certainly not
needed when all you want to do is run a command.

A couple of other details: Why use {} round your variables? I find
$XKLD simpler and more readable than ${_XKLD}. Second, what are all
the initial # characters? Are they, too, from you newsreader when you
paste output? Anyway, they confuse matters (at least they confuse me)
so you might want to look at removing them.

<snip>
--
Ben.
From: Ben Bacarisse on
Ed Morton <mortonspam(a)gmail.com> writes:

> On 2/5/2010 6:38 AM, Ben Bacarisse wrote:
>> lbrtchx(a)gmail.com writes:
<snip>
>>> # FIND DIRECTORY
>>> _FND_DIR=/UNIONFS/var
>>>
>>> # EXCLUDING (should be done in a loop and using awk)
>>> _XKLD="-noleaf -wholename '/proc' -prune -o -wholename '/media/sda1' -prune"
>>>
>>> #eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s %d ' -exec md5sum -b {} \;> 2sort_md5sum.tmp
>>> #find: paths must precede expression: %d
>>
>> This, and all the subsequent errors, are caused by the eval. Why are
>> you using eval? It is quite rare in shell scripting and certainly not
>> needed when all you want to do is run a command.
>
> He wants to be able to store parts of the command in shell variables
> and then execute them together later.

As the example illustrates, this gets messy using eval and there is
nothing in the line as posted that suggests eval is needed. If the
parts of the command are simply options and the like (as illustrated)
I don't see the need for it. Maybe a previous example showed why
eval is needed, but I missed that.

To the OP: can you post a short example of the kind of thing you want
that does not work using simple variable expansion? If you are forced
to use eval you will need to develop a sound understanding of the
quoting rules for your shell and your hair will go grey before you get
all the details right!

<snip>
--
Ben.
From: Seebs on
On 2010-02-05, lbrtchx(a)gmail.com <lbrtchx(a)gmail.com> wrote:
> it amazes me how fragile bash scripting is

It's not.

> # EXCLUDING (should be done in a loop and using awk)
> _XKLD="-noleaf -wholename '/proc' -prune -o -wholename '/media/sda1' -prune"
>
> #eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s %d ' -exec md5sum -b {} \; > 2sort_md5sum.tmp
> #find: paths must precede expression: %d

I think your problem is that you're using "eval", which does things you don't
expect.

In particular, the argument to -printf is quoted by the shell, but that just
means that eval is given the argument <%s %d > as a single argument. When
eval runs, it then re-does all the usual shell input processing, including
word splitting, so "%d" ends up as a separate argument, and since it doesn't
start with a -, it's a path.

.... And that's why "%d" is the path that must precede expression.

> #eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s%d' -exec md5sum -b {} \; > 2sort_md5sum.tmp
> #find: missing argument to `-exec'

Here, it's the semicolon, which is getting eaten by the shell in eval.

> #eval find ${_FND_DIR} ${_XKLD} -o -type f -printf '%s%d' -exec md5sum -b {} ${_BSLSH}; > 2sort_md5sum.tmp
> #find: missing argument to `-exec'

Here, it's the semicolon, which is getting eaten by the calling shell before
it even reaches the eval.

Most important rule:

If you don't understand why these are blowing up, NEVER use eval. At all.

When you understand why these blew up, you should still *almost* never use
eval. None of these commands needed an eval. In most cases, eval was
what blew them up. (You still need a backslash on semicolons for find even
without the eval, though.)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Seebs on
On 2010-02-05, Ben Bacarisse <ben.usenet(a)bsb.me.uk> wrote:
> A couple of other details: Why use {} round your variables? I find
> $XKLD simpler and more readable than ${_XKLD}. Second, what are all
> the initial # characters? Are they, too, from you newsreader when you
> paste output? Anyway, they confuse matters (at least they confuse me)
> so you might want to look at removing them.

One of my coworkers does this habitually, I think because:
1. It is *sometimes* necessary.
2. It is easier to do it always than to sanity-check whether to do it
on each specific occasion.

Having been burned at least once by "why does $FILE_bar not expand to
foo_bar", I am sympathetic.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!