Newbie question: Nested command substitution???

Newbie question: Nested command substitution???

Post by Juergen Schrec » Tue, 17 Dec 1996 04:00:00



How can I nest substituted commands? Here's what I need:

I have a directory e.g. /usr/velocity/spool/Queued/VBG-DWS-00001
What I need to extract is Queued.
Right now, I do something like this:

directory="/usr/velocity/spool/Queued/VBG-DWS-00001"
parent=`dirname $directory`
status=`basename $parent`

This works fine, but I would like to do something like this:

status=`basename `dirname $directory``

However, the above does not work, probably because the quoting is
screwed up. How do I do this right?

TIA

js


--
-------------------------------------------------------------
-     Experience is knowing things, you should not do.      -
-------------------------------------------------------------

 
 
 

Newbie question: Nested command substitution???

Post by Icarus Spar » Wed, 18 Dec 1996 04:00:00




Quote:>How can I nest substituted commands? Here's what I need:

>I have a directory e.g. /usr/velocity/spool/Queued/VBG-DWS-00001
>What I need to extract is Queued.
>Right now, I do something like this:

>directory="/usr/velocity/spool/Queued/VBG-DWS-00001"
>parent=`dirname $directory`
>status=`basename $parent`

>This works fine, but I would like to do something like this:

>status=`basename `dirname $directory``

>However, the above does not work, probably because the quoting is
>screwed up. How do I do this right?

status=`basename \`dirname $directory\``

 
 
 

Newbie question: Nested command substitution???

Post by Bill Marc » Wed, 18 Dec 1996 04:00:00





>>How can I nest substituted commands? Here's what I need:

>>I have a directory e.g. /usr/velocity/spool/Queued/VBG-DWS-00001
>>What I need to extract is Queued.
>>Right now, I do something like this:

>>directory="/usr/velocity/spool/Queued/VBG-DWS-00001"
>>parent=`dirname $directory`
>>status=`basename $parent`

>>This works fine, but I would like to do something like this:

>>status=`basename `dirname $directory``

>>However, the above does not work, probably because the quoting is
>>screwed up. How do I do this right?

>status=`basename \`dirname $directory\``

Or, in ksh or bash, you can do
status=$(basename $(dirname $directory))

--

"You can pay Uncle Sam with the overtime
 Is that all you get for your money?" --Billy Joel

 
 
 

Newbie question: Nested command substitution???

Post by Juergen Schrec » Wed, 18 Dec 1996 04:00:00



> status=`basename \`dirname $directory\``

Ok, I tried this and in works fine.
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 because I tried this:

find ${VBG_SPOOL} -type d -name $1-*-$2 \
                  -exec echo "`basename \`dirname /foo/foo/bar\``" \;

which will return "foo". This proofs, that the -type and -name
evaluations were _true_ and -exec executes correctly.

Now what do I do????

TIA

js

--
-------------------------------------------------------------
-     Experience is knowing things, you should not do.      -
-------------------------------------------------------------

 
 
 

Newbie question: Nested command substitution???

Post by Gora Mohan » Thu, 19 Dec 1996 04:00:00



Quote:>I have a directory e.g. /usr/velocity/spool/Queued/VBG-DWS-00001
>What I need to extract is Queued.
>Right now, I do something like this:
>directory="/usr/velocity/spool/Queued/VBG-DWS-00001"
>parent=`dirname $directory`
>status=`basename $parent`
>This works fine, but I would like to do something like this:
>status=`basename `dirname $directory``

If you are using a sh variant, as it looks like, try

status=`basename \`dirname $directory\``

A cleaner way to do the same might be

status=`echo $directory | awk -F/ '{print $5}'`

Regards,
Gora

 
 
 

Newbie question: Nested command substitution???

Post by Friedhelm Waitzman » Thu, 19 Dec 1996 04:00:00



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.

 
 
 

Newbie question: Nested command substitution???

Post by Chet Ram » Thu, 19 Dec 1996 04:00:00




Quote:>How can I nest substituted commands? Here's what I need:

>I have a directory e.g. /usr/velocity/spool/Queued/VBG-DWS-00001
>What I need to extract is Queued.
>Right now, I do something like this:

>directory="/usr/velocity/spool/Queued/VBG-DWS-00001"
>parent=`dirname $directory`
>status=`basename $parent`

>This works fine, but I would like to do something like this:

>status=`basename `dirname $directory``

>However, the above does not work, probably because the quoting is
>screwed up. How do I do this right?

You can do one of two things.  If you're using a shell that supports
the $(...) form of command substitution, you can change your code to
use it:

        status=$(basename $(dirname $directory))

If your shell does not support that syntax, use backslashes to escape
the inner backquotes:

        status=`basename \`dirname $directory\``

--
``The lyf so short, the craft so long to lerne.'' - Chaucer


 
 
 

Newbie question: Nested command substitution???

Post by Geoff Cla » Fri, 20 Dec 1996 04:00:00



>>find ${VBG_SPOOL} -type d -name $1-*-$2 \
>>                  -exec echo "`basename \`dirname {}\``" \;
>Both problems are solved, if You let find call the bourne
>shell rather than echo:
>find ... -e sh -c 'basename `dirname "$1"`' sh '{}' ';'

This will work, but why execute three commands for each directory selected
by "find", when you can do the whole thing with just:

    find ... -print | awk -F/ '{ print $(NF-1) }'

--

UniSoft Limited, London, England.

 
 
 

Newbie question: Nested command substitution???

Post by Juergen Schrec » Sat, 21 Dec 1996 04:00:00


Yahoo!!

Every once in while someone realizes that explaining the root of problem
is more effective than providing the quick fix.
I pinned this email too my wall. Learned a lot!

Thanks again and have a Merry Christmas.

juergen

--
-------------------------------------------------------------
-     Experience is knowing things, you should not do.      -
-------------------------------------------------------------

 
 
 

1. nested command substitution in csh

Is it possible, in C-shell, to nest command substitution?  I'm looking for
a way to make instructions like the following work without having to capture
intermediate results into a shell variable:

    set DayOfWeek = ``date` | nawk '{split($0, arr); printf("%s", arr[1])}'`

Thanks in advance for any replies.

RLB

2. no sound after installing usb midi interface

3. Can I nest command substitution

4. can't print with Samba

5. Nested command substitution?

6. source for rusersd

7. Command Substitution within Parameter Substitution?

8. Can I install redhat from harddisk?

9. nested shell substitution

10. Korn shell: nested substitution?

11. Command substitution in rsh-command

12. bash command substitution question

13. csh command line substitution question