Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] Bash verschachtelte While-Schleife (Sinnlos)
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
musv
Advocate
Advocate


Joined: 01 Dec 2002
Posts: 3337
Location: de

PostPosted: Sat Apr 12, 2014 7:02 am    Post subject: [Solved] Bash verschachtelte While-Schleife (Sinnlos) Reply with quote

Kurz, ich steh irgendwo auf dem Schlauch:

Code:
A=1
B=1

while [ $A -lt 5 ]; do
   while [ $B -lt 3 ]; do
      echo $A - $B
      ((A++))
   done
   ((B++))
done


Erwartet:
1 - 1
1 - 2
2 - 1
2 - 2
3 - 1
3 - 2
4 - 1
4 - 2

tatsächliches Ergebnis:
1 - 1
2 - 1
3 - 1
4 - 1
...
123455667 - 1
...

Wo ist mein Denkfehler?


Last edited by musv on Mon Apr 14, 2014 2:52 pm; edited 1 time in total
Back to top
View user's profile Send private message
Fijoldar
Apprentice
Apprentice


Joined: 10 Apr 2013
Posts: 248

PostPosted: Sat Apr 12, 2014 7:37 am    Post subject: Reply with quote

Du rufst zu früh ((A++)) auf. Das muss außerhalb der B Schleife passieren, sonst erhöhst du A schon, nachdem zu einmal B abgezogen hast. Statt A zu erhöhen, musst du B erhöhen.

Weiterhin musst du in der A-Schleife B jedesmal wieder auf 1 zurücksetzen. Also etwa so

Code:
#!/bin/bash

A=1

while [ $A -lt 5 ]; do
   B=1
   while [ $B -lt 3 ]; do
      echo $A - $B
      (( B++ ))
   done
   (( A++ ))
done
Back to top
View user's profile Send private message
musv
Advocate
Advocate


Joined: 01 Dec 2002
Posts: 3337
Location: de

PostPosted: Sat Apr 12, 2014 9:25 am    Post subject: Reply with quote

Ok, war wohl heut früh noch etwas belämmert im Halbschlaf. Beim nochmaligen Drübersehen hab ich jetzt meine Dämlichkeit erkannt.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Sat Apr 12, 2014 8:51 pm    Post subject: Reply with quote

musv wrote:
Ok, war wohl heut früh noch etwas belämmert im Halbschlaf. Beim nochmaligen Drübersehen hab ich jetzt meine Dämlichkeit erkannt.
Damit das nicht noch viel mehr andere realisieren, solltest Du ein "[Gelöst]" dem Topic voranstellen :-D
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum 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