Quote:>However, now I'm stuck with another problem: I really would like to use
>this in the find -exec evaluation:
>find ${VBG_SPOOL} -type d -name $1-*-$2 \
> -exec echo "`basename \`dirname {}\``" \;
>but it always returns "."
>Somehow it doesn't carry forward the curley braces [...]
Right, it doesn't:
In
-exec program program-arg1 program-arg2 ...
find recognizes curly braces only if a program-arg consists
solely of {}. (Note, that UNIX passes a word list rather
than a command line to the program to execute.) Therefor,
if find sees -exec program ... {} ..., then {} is recognized
and substituted by find.
The second thing to note is that parameter and command
substitution is not done by find nor by echo. It is done by
the shell which You use to invocate find. The command word
given to echo in Your example below is constructed by the
shell which invocates find with the following parameters:
#0: find
#1: ??? (contents of variable VBG_SPOOL)
#2: -type
#3: d
#4: -name
#5: ???-*-??? (depends on contents of variables 1 and 2)
#6: -exec
#7: echo
#8: foo
#9: ;
Quote:>find ${VBG_SPOOL} -type d -name $1-*-$2 \
^
You should protect the * because you do not want the shell
to replace the pattern; you want it to pass it on to find.
Write "$1"-\*-"$2"
Quote:> -exec echo "`basename \`dirname /foo/foo/bar\``" \;
Both problems are solved, if You let find call the bourne
shell rather than echo:
find ... -e sh -c 'basename `dirname "$1"`' sh '{}' ';'
Suppose, find has found the file
/dir/subdir/some-file-name; then it calls the bourne shell
with 4 parameter words (listed literally how the shell sees
them):
#1: -c
#2: basename `dirname "$1"`
#3: sh
#4: /dir/subdir/some-file-name
The shell then discards the 3rd word and sets its OWN
positional parameters $1, $2, ... to the remaining words
(no. 4 up): $1 becomes /dir/subdir/some-file-name
The program basename writes to standard output, so there is
no need to capture basename's output with `...` and give it
to echo: Instead of
echo `basename ...`
write
basename ...
If You write sh -c 'exec basename ...' rather than
sh -c 'basename ...', the shell does not fork a subprocess
to execute basename but replaces itself with basename thus
reducing the risk of running short of processes.
Regards, Friedhelm.