🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
7 messages
7 total messages Started by Paul Gerwitz Wed, 07 Nov 1990 19:06
Using imbedded commands inside expr(1)
#1720
Author: Paul Gerwitz
Date: Wed, 07 Nov 1990 19:06
20 lines
1131 bytes
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      |
 +----------------------------------------------------------------------------+
Re: Using imbedded commands inside expr(1)
#1761
Author: Tom Christiansen
Date: Thu, 08 Nov 1990 03:38
10 lines
678 bytes
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
Re: Using imbedded commands inside expr(1)
#1723
Author: William R. Pringle
Date: Thu, 08 Nov 1990 04:56
15 lines
894 bytes
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
Re: Using imbedded commands inside expr(1)
#1727
Author: Hamish Macdonald
Date: Thu, 08 Nov 1990 13:58
28 lines
1451 bytes
>>>>> 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
Re: Using imbedded commands inside expr(1)
#1729
Author: Bob McGowen x4312 dept208
Date: Thu, 08 Nov 1990 16:49
42 lines
1984 bytes
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
Re: Using imbedded commands inside expr(1)
#1740
Author: news
Date: Fri, 09 Nov 1990 14:54
19 lines
971 bytes
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
Re: Using imbedded commands inside expr(1)
#1739
Author: Paul Chamberlain
Date: Fri, 09 Nov 1990 16:44
16 lines
1125 bytes
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads