[comp.unix.shell] command substitution

coleman@cam.nist.gov (Sean Sheridan Coleman X5672) (11/08/90)

I want to write the following independent statements into one
line:

set tmp= `awk '{print $7}' /tmp/process.$$`
set date = `basename $tmp`

$7 is /home/central/coleman/901012.log


I really want to do the following:

set date = `basename `awk '{print $7}' /tmp/process.$$``

Unfortuantly, the second set of Grave accent marks for command
substitution for the basename command will confuse the shell. 
Is there a way to "qoute" these so that those grave accent (`)
will be passed down for another layer command substitution?


Thanks

Sean Coleman
NIST
Boulder, CO

coleman@bldrdoc.gov

tchrist@convex.COM (Tom Christiansen) (11/08/90)

In article <5697@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:
>I really want to do the following:
>
>set date = `basename `awk '{print $7}' /tmp/process.$$``

I'm sure I just saw something like this.  If you were using ksh, you could
simply say:

    date=$(basename $(awk '{print $7}' /tmp/process.$$))

and be done with it, but for the csh, you're going to have to go through 
more convolutions to achieve the same effect.

You have to do it in two manual stages;

  set widget = `awk '{print $7}' /tmp/process.$$`
  set date = `basename $widget`

should work.  I found that quoting the widget string was enough,
so I think this will work for you:

  set widget = '`awk '"'"'{print $7}'"'"' /tmp/process.$$`'
  set date = `basename $widget`

because this worked for me:

  set cmd = '`whoami`'
  echo i am `id $cmd`
 
which came as a bit of a surprise.  You can do the same thing in /bin/sh, too:

  cmd='`whoami`'
  echo i am `id $cmd`

--tom

wrp@PRC.Unisys.COM (William R. Pringle) (11/08/90)

In article <5697@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:
>
>
>I really want to do the following:
>
>set date = `basename `awk '{print $7}' /tmp/process.$$``
>
>Unfortuantly, the second set of Grave accent marks for command
>substitution for the basename command will confuse the shell. 
>Is there a way to "qoute" these so that those grave accent (`)
>will be passed down for another layer command substitution?

There seems to be a rash of articles about nested backquotes.  Since I
answered the last one, ...

You can use a backslash to quote backward quotes:

	`basename \`awk '{print $7}' /tmp/process.$$\``

However, since you are already paying the overhead of calling awk, then why
not do everything there?


	`awk '{print substr($7,1,index($7,".log")-1)}' /tmp/process.$$`

The index function returns the location of the string ".log" (you could also
use strlen($7)-4) and then the substr function prints the portion of the
string from the first character up to the character before the ".log"
(hence the index()-1)

Hope this helps.

Bill Pringle

chris@mimsy.umd.edu (Chris Torek) (11/08/90)

In article <5697@alpha.cam.nist.gov> coleman@cam.nist.gov
(Sean Sheridan Coleman X5672) writes:
>I want to write the following independent statements into one line:
>
>set tmp= `awk '{print $7}' /tmp/process.$$`
>set date = `basename $tmp`

Since your syntax above is C shell (and incorrect C shell at that---the
first line sets the variable tmp to nothing, and sets the variable whose
name is produced by the awk command to nothing), the answer is `there is
no way to do it'.

In sh, it is easy:

	date=`basename \`awk '{print $7}' /tmp/process.$$\``

>I really want to do the following:
>
>set date = `basename `awk '{print $7}' /tmp/process.$$``
>
>Unfortuantly, the second set of Grave accent marks for command
>substitution for the basename command will confuse the shell.

Well no; actually, their main function is to confuse *you*. :-)  They
simply close the others.  This is:

	run "basename"
	run ""
	splice output from basename and "" onto both ends of the awk
	set date to the result.

>Is there a way to "qoute" these so that those grave accent (`)
>will be passed down for another layer command substitution?

No; the C shell internals make this impossible.  (There are several
places that assume that backquote expansion happens only to a depth
of one.)  If you somehow manage to trick the `scanner' all you will
get is a broken shell.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

jik@athena.mit.edu (Jonathan I. Kamens) (11/09/90)

In article <15527@burdvax.PRC.Unisys.COM>, wrp@PRC.Unisys.COM (William R. Pringle) writes:
|> In article <5697@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:
|> >set date = `basename `awk '{print $7}' /tmp/process.$$``
|> 
|> You can use a backslash to quote backward quotes:
|> 
|> 	`basename \`awk '{print $7}' /tmp/process.$$\``
|> 

  Backslashing backquotes to nest command evaluation substitution doesn't work
in the C-shell, and the syntax of shell code in the original poster's message
implies that that's what he's using.

  (As an aside, it would help if people would say in their messages what shell
they are using, and not leave us to guess based on their questions.  Sometimes
it's possible to guess, sometimes it isn't.  People who already make sure to
do this can just ignore the last two sentences. :-)


-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710