Incrementing question

Incrementing question

Post by Thomas Camero » Mon, 12 Jan 1998 04:00:00



Hi all -

This is a silly question, and if I had my shell scripting book here at
the house instead of at the office, I would just RTFM.

I have a script that is doing something like:

x=1
while $x -lt 100000; do
  x=$x+1
  exho $x
done

So that I get 1, then 2, then 3, then 4, etc.  Obviously, I am doing it
wrong, because I am gettin 1, then 1+1, then 1+1+1, etc.

What am I doing wrong?

Thanks!
Thomas

 
 
 

Incrementing question

Post by Thomas Camero » Mon, 12 Jan 1998 04:00:00


Quote:>blush<

Thanks, Shawn Everett for answering, and making all clear...

Should be:

#!/bin/sh
x=1
while test $x -lt 100000; do
   let x=$x+1
   echo $x
done

Thanks for the help!
Thomas


> Hi all -

> This is a silly question, and if I had my shell scripting book here at
> the house instead of at the office, I would just RTFM.

> I have a script that is doing something like:

> x=1
> while $x -lt 100000; do
>   x=$x+1
>   exho $x
> done

> So that I get 1, then 2, then 3, then 4, etc.  Obviously, I am doing it
> wrong, because I am gettin 1, then 1+1, then 1+1+1, etc.

> What am I doing wrong?

> Thanks!
> Thomas


 
 
 

Incrementing question

Post by James R. Mart » Fri, 16 Jan 1998 04:00:00


: >blush<
: Thanks, Shawn Everett for answering, and making all clear...
: Should be:
: #!/bin/sh
: x=1
: while test $x -lt 100000; do
:    let x=$x+1
:    echo $x
: done

There is a definite problem with this. While it is true that in
_kornshell_ the above will work (and BTW is a sub-optimal solution)
there is no builtin arithmetric operators in /bin/sh, as you state
explicitly above.

Although in the very newest OSes, ksh93 is frequently called /bin/sh,
I would instead have you understand the confusion that will be
inevitably caused by your assertion above.

#!/bin/ksh
typeset -i x=0
while (((x+=1)<100000))
do      print -- $x
done

-James

 
 
 

Incrementing question

Post by era eriksso » Thu, 12 Feb 1998 04:00:00



(James R. Martin) posted to comp.unix.shell:

 > : #!/bin/sh
 > : x=1
 > : while test $x -lt 100000; do
 > :    let x=$x+1
 > :    echo $x
 > : done
 > There is a definite problem with this. While it is true that in
 > _kornshell_ the above will work (and BTW is a sub-optimal solution)
<...>
 > #!/bin/ksh
 > typeset -i x=0
 > while (((x+=1)<100000))
 > do        print -- $x
 > done

The canonical /bin/sh solution is to use the expr(1) to update the
variable:

    #!/bin/sh
    x=1
    while [ $x -lt 100000 ]; do
        x=`expr $x + 1`  # Spaces around plus sign are significant
        echo $x          # Is the intent really to echo 2 .. 100000??
    done

It recently occurred to me that you might instead use a generator
program of some sort to drive the loop, and save a bunch of processes:

    #!/bin/sh
    perl -e 'for ($x = 1; $x < 100000; ++$x) { print "$x\n" }' |
    while read x; do
      echo "$x"
    done

This can probably be solved more neatly with something markedly
simpler than Perl as the driver but I think you should already find
this to be more efficient on most systems. (Anybody wanna test?)

/* era */

What would be a good way to generate successive integers? bc(1)?
tr '\000' '\012' </dev/zero | head -100000 | nl -b a? :-)

--
 Paparazzi of the Net: No matter what you do to protect your privacy,
  they'll hunt you down and spam you. <http://www.iki.fi/~era/spam/>

 
 
 

Incrementing question

Post by Icarus Sparr » Sat, 14 Feb 1998 04:00:00



Quote:>It recently occurred to me that you might instead use a generator
>program of some sort to drive the loop, and save a bunch of processes:

>    #!/bin/sh
>    perl -e 'for ($x = 1; $x < 100000; ++$x) { print "$x\n" }' |
>    while read x; do
>      echo "$x"
>    done

>This can probably be solved more neatly with something markedly
>simpler than Perl as the driver

Some machines have a command called 'jot' to do exactly this kind of thing.
 
 
 

1. question about incrementing stats counters in network drivers

Hi!

If an error occur that falls into the "detailed errors" category, e.g. a
fifo overrun, are we supposed to increment both the total error count and
the detailed error count or just the detailed error count?

From isa-skeleton.c it seems that I'm supposed to increment both, but then I
spotted things like this in there as well:

------------< snip <------< snip <------< snip <------------
        } else {
            /* Malloc up new buffer. */
            struct sk_buff *skb;

            lp->stats.rx_bytes+=pkt_len;

            skb = dev_alloc_skb(pkt_len);
            if (skb == NULL) {
                printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
                       dev->name);
                lp->stats.rx_dropped++;
                break;
            }
            skb->dev = dev;

            /* 'skb->data' points to the start of sk_buff data area. */
            memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
                   pkt_len);
            /* or */
            insw(ioaddr, skb->data, (pkt_len + 1) >> 1);

            netif_rx(skb);
            dev->last_rx = jiffies;
            lp->stats.rx_packets++;
            lp->stats.rx_bytes += pkt_len;
        }
------------< snip <------< snip <------< snip <------------

Notice that rx_bytes gets incremented twice.

Also it doesn't make sense to increment both since the total count can be
derived from rx_errors+other rx errors or tx_errors+other tx errors, so I'm
not sure what to do.

--

Regards
 Abraham

QOTD:
        I love your outfit, does it come in your size?

__________________________________________________________
 Abraham vd Merwe - 2d3D, Inc.

 Device Driver Development, Outsourcing, Embedded Systems

  Cell: +27 82 565 4451         Snailmail:
   Tel: +27 21 761 7549            Block C, Aintree Park
   Fax: +27 21 761 7648            Doncaster Road

  Http: http://www.2d3d.com        South Africa

  application_pgp-signature_part
< 1K Download

2. JDK for OpenBSD

3. Newbie Question? Repeated Incrementing in sh

4. emergency error (19)

5. bash shell question for URL incrementing

6. System Administrators! Test your UNIX knowledge at new site!

7. Script Question -- Incrementing filename

8. MAILING-LIST: Linux At Home

9. sed question: substitution with incrementing

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

11. set kiobuf io_count once, instead of increment

12. Incrementing module use count of a low level driver

13. Solaris atomic/interlocked increment/swap operations?