[comp.unix.shell] Using imbedded commands inside expr

gerwitz@kodak.com (Paul Gerwitz) (11/08/90)

I seem to be having trouble using expr.  The following dies:

    test=`expr `tail +3 file` + 1` 

    Basically get a numeric value from a file and increment it, putting the
    result in a shell variable.

    I have tried all kinds of convolutions of quoting etc.
    Temporarily we have broken it up into several seperate statements.

    Any clues ??

    Running HP-UX at 7.0 using ksh.


-- 
 +----------------------------------------------------------------------------+
 | Paul F Gerwitz  WA2WPI  | SMTP: gerwitz@kodak.com                          |
 | Eastman Kodak Co        | UUCP: ..uunet!atexnet!kodak!eastman!gerwitz      |
 +----------------------------------------------------------------------------+

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

In article <1462@eastman.UUCP> gerwitz@kodak.com (Paul Gerwitz) writes:
>I seem to be having trouble using expr [in ksh].  The following dies:
>
>    test=`expr `tail +3 file` + 1` 

Write it this way:

    test=$(expr $(tail +3 file) + 1)

--tom

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

In article <1462@eastman.UUCP> gerwitz@kodak.com (Paul Gerwitz) writes:
>I seem to be having trouble using expr.  The following dies:
>
>    test=`expr `tail +3 file` + 1` 
>
>    Basically get a numeric value from a file and increment it, putting the
>    result in a shell variable.

The backslash can be used to quote nested backward quotes (and other
strange characters):

	test=`expr \`tail +3 file\` + 1`


Bill Pringle

himacdon@maytag.uwaterloo.ca (Hamish Macdonald) (11/08/90)

>>>>> In article <15525@burdvax.PRC.Unisys.COM>, wrp@PRC.Unisys.COM
>>>>> (William R. Pringle) writes:

William> In article <1462@eastman.UUCP> gerwitz@kodak.com (Paul
William> Gerwitz) writes:
>I seem to be having trouble using expr.  The following dies:
>
>    test=`expr `tail +3 file` + 1` 
>
>    Basically get a numeric value from a file and increment it,
>    putting the result in a shell variable.

William> The backslash can be used to quote nested backward quotes
William> (and other strange characters):

William> 	test=`expr \`tail +3 file\` + 1`

When using the Korn shell (as the original poster is), I think it is
less confusing to use the $() form of command substitution:

	test=$(expr $(tail +3 file) + 1)

William> Bill Pringle

Hamish.
--
--------------------------------------------------------------------
himacdon@maytag.uwaterloo.ca                 watmath!maytag!himacdon

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (11/09/90)

In article <1462@eastman.UUCP> gerwitz@kodak.com (Paul Gerwitz) writes:
>I seem to be having trouble using expr.  The following dies:
>
>    test=`expr `tail +3 file` + 1` 
>
>    Basically get a numeric value from a file and increment it, putting the
>    result in a shell variable.
>

....

It seems that there may be two problems with what you are doing.  You should
check your manual for tail.  Mine states that the command that you are
using will begin at the third line of "file" and print from that point to
the end.  So if your file has more than 3 lines the result of the first
step will confuse the second (expr will get multiple items before the
plus sign and will bomb. This will also happen if the single line has
more than one item.).  The second issue is the escaping of the grave
quotes (backquotes).

Try the following:

  a)  create a file with a single number in it (such as file "num",
      content is the digit "3").

  b)  create a script file with the following lines and run it:

	    #!/bin/sh
	    val=`expr \`cat num\` + 1`
	    echo val is $val

  c)  the result will be "val is 4" for the Bourne shell, probably also
      for ksh.

You could use head +1 to get the first line in "file", tail -1 to get
the last line.  If you want a line from the middle you will need to
try some other utility, perhaps awk.

Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

news@usenet.ins.cwru.edu (11/09/90)

In article <1462@eastman.UUCP> gerwitz@kodak.com (Paul Gerwitz) writes:
>I seem to be having trouble using expr.  The following dies:
>
>    test=`expr `tail +3 file` + 1` 

Since you're using ksh, how about:

	test=$(expr $(tail +3 file) + 1)

If you want backwards compatibility with sh, use this:

	test=`expr \`tail +3 file\` + 1`

Chet
-- 
Chet Ramey
Network Services Group			``I die, Horatio''
Case Western Reserve University
chet@ins.CWRU.Edu

tif@doorstop.austin.ibm.com (Paul Chamberlain) (11/10/90)

In article <1462@eastman.UUCP> gerwitz@kodak.com (Paul Gerwitz) writes:
>    test=`expr `tail +3 file` + 1` 

This works on my /bin/sh and my /bin/ksh:
	echo `expr \`expr 5 + 1\` + 3`
In fact so does this:
	echo `expr \`expr \\\`expr 2 + 3\\\` + 1\` + 3`

I didn't try it with tail or assigning it to a variable but it better
work just as well (I'm assuming that you have a 3 line file, personally
I'd use "tail -1" instead).

Anybody know why the same thing fails on csh?

Paul Chamberlain | I do NOT represent IBM.     tif@doorstop, sc30661 at ausvm6
512/838-7008     | ...!cs.utexas.edu!ibmchs!auschs!doorstop.austin.ibm.com!tif