Looping in ksh

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Looping in ksh

Uwe Dippel
I needed some little script; and - as usual - tried it out by typing:
i=0
uplim=10
while [ $i -lt $uplim ]
do
       ((i=i+1))
       echo $i
done
1
2
3
4
5
6
7
8
9
10

So good, so far.
So I put the history into a file; added
#!/bin/sh
and ran it.
To my surprise, it is an endless loop printing 0.
Typed again, works. Running the script: endless loop.

Question: What is missing in the loop ?
FYI: I tried all those variation of $i in the arithmetic expression, but I
won't do the trick. It works properly as line-by-line and loops endlessly
as script.

I can make it work by changing it to
...
       echo $((i=i+1))
...
, but I still ask myself, why the arithmetic expression is not being
evaluated within the script if not in combination with echo, e.g. ?

Uwe

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Kevin Foo
On Wednesday 20 December 2006 18:43, Uwe Dippel wrote:
> i=0
> uplim=10
> while [ $i -lt $uplim ]
> do
>        ((i=i+1))
>        echo $i
> done

#!/bin/ksh

--
Warm regards,
Kevin Foo

Key fingerprint : 4B23 FC1C E50B 9693 CCDD  2A7D A048 E909 8924 9BDD
Public key : http://keyserver.linux.it/pks/lookup?op=get&search=0x89249BDD
*****************************Internet Email Confidentiality Footer *****************************

Legal Privilege & Confidentiality
---------------------------------------------------------------------------------------------------------------------------------------------------------------

This email contains privileged and/or confidential information.  If you are not the intended recipient (or responsible for delivery of the message to such person) or if you have inadvertently received this email, you should destroy or delete this message and notify the sender by reply email accordingly. If you or your employer do not consent to using Internet email for messages of this kind please advise immediately by sending an email to the sender of this message .  All opinions, conclusions and other information in this message that do not relate to the official business of Zaid Ibrahim & Co shall be understood as neither given nor endorsed by Zaid Ibrahim & Co. Our company accepts no liability for the content of this email, or for the consequences of any actions taken on the basis of the information provided, unless that information is subsequently confirmed in writing.  

Caveat
-------------------------------------------------------------------------------------------------------------------------------------------------------------WARNING: Computer viruses can be transmitted via email, and you should check this email and any attachments for the presence of viruses. Zaid Ibrahim & Co accepts no liability for any damage caused by any virus transmitted by this email. Our employees are expressly required not to make defamatory statements nor infringe or authorise any infringement of copyright or any other legal right via any communications. Any such communication is contrary to our company policy and outside the scope of the employment of said individual. We will not be liable for such communication.

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Sebastian Benoit
In reply to this post by Uwe Dippel
Uwe Dippel([hidden email]) on 2006.12.20 18:43:35 +0000:
> I needed some little script; and - as usual - tried it out
> by typing:
     ^^^^^^!

> #!/bin/sh
         ^^!

> Question: What is missing in the loop ?

nothing, your shell ist ksh, not sh.

/B.
--
Sebastian Benoit <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Teemu Schaabl
In reply to this post by Uwe Dippel
Uwe Dippel([hidden email])@2006.12.20 18:43:35 +0800:

> I needed some little script; and - as usual - tried it out by typing:
> i=0
> uplim=10
> while [ $i -lt $uplim ]
> do
>        ((i=i+1))
>        echo $i
> done
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
>
> So good, so far.
> So I put the history into a file; added
> #!/bin/sh
^^^^^^^^^^^ tried /bin/ksh as shebang?

cheers,
teemu

--
Don't be too proud of the technological
terror you have constructed -- D. Vader

[demime 1.01d removed an attachment of type application/pgp-signature]

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Otto Moerbeek
In reply to this post by Uwe Dippel
On Wed, 20 Dec 2006, Uwe Dippel wrote:

> I needed some little script; and - as usual - tried it out by typing:
> i=0
> uplim=10
> while [ $i -lt $uplim ]
> do
>        ((i=i+1))
>        echo $i
> done
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
>
> So good, so far.
> So I put the history into a file; added
> #!/bin/sh
> and ran it.
> To my surprise, it is an endless loop printing 0.
> Typed again, works. Running the script: endless loop.

(( .. )) is ksh specific. Put #!/bin/ksh in your scritp and you'll be fine.

        -Otto

>
> Question: What is missing in the loop ?
> FYI: I tried all those variation of $i in the arithmetic expression, but I
> won't do the trick. It works properly as line-by-line and loops endlessly
> as script.
>
> I can make it work by changing it to
> ...
>        echo $((i=i+1))
> ...
> , but I still ask myself, why the arithmetic expression is not being
> evaluated within the script if not in combination with echo, e.g. ?
>
> Uwe

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

L. Ahmadi
In reply to this post by Uwe Dippel
Hi,

If you want to use /bin/sh, it works well if you replace

((i=i+1)) by
let i=i+1

L. Ahmadi


Uwe Dippel wrote:

> I needed some little script; and - as usual - tried it out by typing:
> i=0
> uplim=10
> while [ $i -lt $uplim ]
> do
>        ((i=i+1))
>        echo $i
> done
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
>
> So good, so far.
> So I put the history into a file; added
> #!/bin/sh
> and ran it.
> To my surprise, it is an endless loop printing 0.
> Typed again, works. Running the script: endless loop.
>
> Question: What is missing in the loop ?
> FYI: I tried all those variation of $i in the arithmetic expression, but I
> won't do the trick. It works properly as line-by-line and loops endlessly
> as script.
>
> I can make it work by changing it to
> ...
>        echo $((i=i+1))
> ...
> , but I still ask myself, why the arithmetic expression is not being
> evaluated within the script if not in combination with echo, e.g. ?
>
> Uwe

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Uwe Dippel
In reply to this post by Otto Moerbeek
Otto Moerbeek wrote:

> (( .. )) is ksh specific.

I know.

 > Put #!/bin/ksh in your scritp and you'll be fine.

Have you tried it ? I did. It doesn't work.

Uwe

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Uwe Dippel
In reply to this post by L. Ahmadi
L. Ahmadi wrote:

> If you want to use /bin/sh,

No I don't (and didn't).

But now I know my mistake:
I had put #!/bin/ksh for a good reason, but I did call it with
$ sh progname

That's rather me stupid, then !

Thanks everyone for answering,

Uwe

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Michael Lechtermann
In reply to this post by L. Ahmadi
L. Ahmadi schrieb:
> Hi,
>
> If you want to use /bin/sh, it works well if you replace
>
> ((i=i+1)) by
> let i=i+1

Or use

I=$((I+1))

which works in sh/ksh/bash and should be pretty universal.

 - Michael

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Bruno Carnazzi
In reply to this post by Uwe Dippel
2006/12/20, Uwe Dippel <[hidden email]>:

> I needed some little script; and - as usual - tried it out by typing:
> i=0
> uplim=10
> while [ $i -lt $uplim ]
> do
>        ((i=i+1))
>        echo $i
> done
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10

 Hi,

This is a shell rewrite of jot(1). Try :
$ jot 10 1 10

Best Regard,

Bruno.

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Otto Moerbeek
In reply to this post by Uwe Dippel
On Wed, 20 Dec 2006, Uwe Dippel wrote:

> Otto Moerbeek wrote:
>
> > (( .. )) is ksh specific.
>
> I know.
>
> > Put #!/bin/ksh in your scritp and you'll be fine.
>
> Have you tried it ? I did. It doesn't work.

Here it works. You must be doing something wrong.

        -Otto

Reply | Threaded
Open this post in threaded view
|

Re: Looping in ksh

Paul de Weerd
In reply to this post by Uwe Dippel
On Wed, Dec 20, 2006 at 08:17:04PM +0800, Uwe Dippel wrote:
| Otto Moerbeek wrote:
|
| >(( .. )) is ksh specific.
|
| I know.
|
| > Put #!/bin/ksh in your scritp and you'll be fine.
|
| Have you tried it ? I did. It doesn't work.

It works. How are you running this test ?

[weerd@despair] $ cat > test
#!/bin/ksh
i=0
uplim=10
while [ $i -lt $uplim ]
do
       ((i=i+1))
       echo $i
done
[weerd@despair] $ chmod u+x test
[weerd@despair] $ ./test
1
2
3
4
5
6
7
8
9
10
[weerd@despair] $ sh test
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
^C

Cheers,

Paul 'WEiRD' de Weerd

--
>++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+
+++++++++++>-]<.>++[<------------>-]<+.--------------.[-]
                 http://www.weirdnet.nl/

[demime 1.01d removed an attachment of type application/pgp-signature]