Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
ciclo assurdo di bash.
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano
View previous topic :: View next topic  
Author Message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4802
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Tue Oct 08, 2024 2:26 am    Post subject: ciclo assurdo di bash. Reply with quote

ho scoperto un programmino elementare di bash che, secondo me, ha un comportamento balordo:
Code:

#!/bin/bash
n=1;
max=94;

while [[ $n < $max ]]; do
   echo $((n++));
done
n=1;
max=24;

while [[ $n < $max ]]; do
   echo $((n++));
done
echo stop

in output ottengo:
Code:

...
89
90
91
92
93
1
2
stop

in pratica, ci sono due cicli di while identici.
il primo è impostato per essere eseguito fino al ciclo 94 (escluso), ed effettivamente esce al ciclo 93.
il secondo è impostato per essere eseguito fino al ciclo 24, ma esce al ciclo 2.

dove sta il guaio?
_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 31229
Location: here

PostPosted: Tue Oct 08, 2024 5:27 am    Post subject: Re: ciclo assurdo di bash. Reply with quote

cloc3 wrote:
dove sta il guaio?

Che le comparazioni di numeri in bash si fanno con -lt, -eq, -gt,....
Se usi <, >, = per bash stai comparando stringhe.
_________________
Questions are guaranteed in life; Answers aren't.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum