Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index International Gentoo Users Deutsches Forum (German) Diskussionsforum
  • Search

[Solved] Bash verschachtelte While-Schleife (Sinnlos)

Unterhaltung über Gentoo und andere Themen: Alles was nicht in ein Support-Forum gehört.
Post Reply
  • Print view
Advanced search
4 posts • Page 1 of 1
Author
Message
musv
Advocate
Advocate
User avatar
Posts: 3380
Joined: Sun Dec 01, 2002 4:13 pm
Location: de

[Solved] Bash verschachtelte While-Schleife (Sinnlos)

  • Quote

Post by musv » Sat Apr 12, 2014 7:02 am

Kurz, ich steh irgendwo auf dem Schlauch:

Code: Select all

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.
Top
Fijoldar
Apprentice
Apprentice
Posts: 248
Joined: Wed Apr 10, 2013 12:03 pm

  • Quote

Post by Fijoldar » Sat Apr 12, 2014 7:37 am

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: Select all

#!/bin/bash

A=1

while [ $A -lt 5 ]; do 
   B=1
   while [ $B -lt 3 ]; do 
      echo $A - $B 
      (( B++ )) 
   done 
   (( A++ )) 
done
Top
musv
Advocate
Advocate
User avatar
Posts: 3380
Joined: Sun Dec 01, 2002 4:13 pm
Location: de

  • Quote

Post by musv » Sat Apr 12, 2014 9:25 am

Ok, war wohl heut früh noch etwas belämmert im Halbschlaf. Beim nochmaligen Drübersehen hab ich jetzt meine Dämlichkeit erkannt.
Top
toralf
Developer
Developer
User avatar
Posts: 3944
Joined: Sun Feb 01, 2004 2:58 pm
Location: Hamburg
Contact:
Contact toralf
Website

  • Quote

Post by toralf » Sat Apr 12, 2014 8:51 pm

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
Top
Post Reply
  • Print view

4 posts • Page 1 of 1

Return to “Diskussionsforum”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic