It is currently Tue, 30 Nov 2021 18:40:46 GMT



 
Author Message
 Challange: increment function in Bourne Shell

I have a question and a challange.

I want a Bourne shell function to increment a variable.

That is, if I execute:

        #!/bin/sh
        A=10 # set A to ten
        inc A
        echo $A

The value of variable A is incremented by one, so it would equal eleven.

I have a Bourne shell function (using expr) that works, but I can't
believe it is so convoluted. It needed two "eval" calls! Yuk!

What's the simplist Bourne shell definition for this function?
I used the built-in "expr" to avoid executing another process.

--
Bruce Barnett <barn...@crd.ge.com>



 Fri, 19 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

The simplest way is just to write A=`expr $A + 1` instead of creating a
function to do it.  What does your function look like?

--
Bill Marcum    bmar...@iglou.com
"Outside of a dog, a book is man's best friend.  Inside a dog, it's too
dark to read." -- Marx (or was it Twain?)



 Sat, 20 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

In article <yekohjoe6xo....@crd.ge.com>,
Bruce Barnett  <barn...@crd.ge.com> wrote:

expr is _not_ builtin to the bourne shell, so you do have to fork another
process, but only one eval is needed

        inc() { eval $1=\`expr \$$1 + 1\`; }

John.



 Sat, 20 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

try:

           function inc() { eval $1=$(( $1 + 1 )); }

 (just a little change :) )

                                                                El Abraham



 Sat, 20 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

In article <yekohjoe6xo....@crd.ge.com>,
Bruce Barnett  <barn...@crd.ge.com> wrote:
:
: I have a question and a challange.
:
: I want a Bourne shell function to increment a variable.
:
: What's the simplist Bourne shell definition for this function?

A=`expr $A + 1`

That's Bourne shell, that is.  ksh would probably do it with ((A=A+1)) or
some such.  Icky, in my opinion.

Waitasec, expr's a built-in in your Bourne shell?  Cool!  What version
of Unix are you using?

--Dave
--
WARNING: sending me junk-email will be interpreted as granting permission to
bomb your offices and machine-gun your children. (10 in 4 days, 3 in 1 hour)
(thanks, Simon!)                    http://www.csclub.uwaterloo.ca/~dagbrown



 Sat, 20 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

In article <322CAE31.42B76...@campus.mty.itesm.mx>,
Abraham Vadillo  <avadi...@campus.mty.itesm.mx> wrote:

Which unfortunatly does not work with the Bourne shell! The '$(( ... ))'
is ksh syntax. Bash uses '$[ ... ]' for almost the same thing.
Icarus



 Sun, 21 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

|> In article <322CAE31.42B76...@campus.mty.itesm.mx>,
|> Abraham Vadillo  <avadi...@campus.mty.itesm.mx> wrote:
|>> John L. Allen wrote:
|>>>
|>>> inc() { eval $1=\`expr \$$1 + 1\`; }
|>>
|>> try:
|>>
|>> function inc() { eval $1=$(( $1 + 1 )); }
|>>
|>> (just a little change :) )

|> Which unfortunatly does not work with the Bourne shell! The '$(( ... ))'
|> is ksh syntax. Bash uses '$[ ... ]' for almost the same thing.

$((...)) is also POSIX, and Bash supports it too.
--
Andreas Schwab                                      "And now for something
sch...@issan.informatik.uni-dortmund.de              completely different"



 Sun, 21 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

I said a Bourne shell function.

John's example works.

--
Bruce Barnett <barn...@crd.ge.com>



 Sun, 21 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

In article <322CAE31.42B76...@campus.mty.itesm.mx>,
Abraham Vadillo  <avadi...@campus.mty.itesm.mx> wrote:
: John L. Allen wrote:
: >
: >         inc() { eval $1=\`expr \$$1 + 1\`; }
:
: try:
:
:            function inc() { eval $1=$(( $1 + 1 )); }

Just what exactly do you think this does?

It's in a weird combination of ksh and Bourne shell syntax, and when I
finally found a shell that even accepted it (zsh), it didn't work
anyway.

--Dave
--
WARNING: sending me junk-email will be interpreted as granting permission to
bomb your offices and machine-gun your children. (10 in 4 days, 3 in 1 hour)
(thanks, Simon!)                    http://www.csclub.uwaterloo.ca/~dagbrown



 Sun, 21 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

In article <50kdqs$...@calum.csclub.uwaterloo.ca>,

It's perfectly valid bash syntax, and it even works.  Take off the
`function' and it works in ksh, too.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, Case Western Reserve University     Internet: c...@po.CWRU.Edu



 Mon, 22 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

After looking at this example, I realize better how bash improved
on sh:

inc () { eval let $1+=1 }

It works in bash or bash invoked as sh. It won't in ordinary
sh's I realize.

        Mike



 Fri, 26 Feb 1999 03:00:00 GMT   
 Challange: increment function in Bourne Shell

Actually, I just realized it's even better in bash; forget
about all the confusing eval's:

inc () { let $1+=1 }



 Fri, 26 Feb 1999 03:00:00 GMT   
 
   [ 12 post ] 

Similar Threads

1. Bourne Shell Problem - variable not incrementing within a while loop

2. Bourne shell: incrementing variables

3. Light Speed Bourne Shell! (was: Bourne shell tricks)

4. Bourne shell string length function?

5. Bourne Shell Function Syntax

6. Bourne shell: Testing the existance of a function?

7. Bourne shell string length function?

8. Bourne shell returning a string from a function

9. aritmetic functions in Bourne shell

10. Bourne shell function to check variables?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software