View previous topic :: View next topic |
Author |
Message |
musv Advocate
Joined: 01 Dec 2002 Posts: 3337 Location: de
|
Posted: Sat Apr 12, 2014 7:02 am Post subject: [Solved] Bash verschachtelte While-Schleife (Sinnlos) |
|
|
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 |
|
|
Fijoldar Apprentice
Joined: 10 Apr 2013 Posts: 248
|
Posted: Sat Apr 12, 2014 7:37 am Post subject: |
|
|
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 |
|
|
musv Advocate
Joined: 01 Dec 2002 Posts: 3337 Location: de
|
Posted: Sat Apr 12, 2014 9:25 am Post subject: |
|
|
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 |
|
|
toralf Developer
Joined: 01 Feb 2004 Posts: 3922 Location: Hamburg
|
Posted: Sat Apr 12, 2014 8:51 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
|