LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-21-2011, 03:47 PM   #1
coyboss
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
Stopping a Loop in Linux


Hi All,

How do you make a loop quit in Linux (redhat)

Here is the code, it just runs and won't stop
Code:
#!/bin/bash

count=1
echo "Start of the program"
while [ $count -le 10 ]
do
	echo "Loop #$count"
	sleep 10
	count=$ [ count = 1 ]
	done 
echo "end of the program"]
THanks in advance

IKE
 
Old 08-21-2011, 03:59 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You're not incrementing count anywhere, so it's always less than 10. Hence, the loop never ends. You can see that count isn't incremented because you output its value in the "echo" line.

Also, this has nothing to do with Red Hat (or even Linux) specifically. It's a Bash script, so you're asking about Bash.

Edit: Presumably the last line of the loop is meant to be, e.g.

let count=$count+1

There may be other ways of incrementing in Bash, but this is the one I know.

Last edited by Nylex; 08-21-2011 at 04:05 PM.
 
Old 08-21-2011, 09:47 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
'let' works but is a bit old school, here is a host of ways to do arithmetic, inc increment
http://tldp.org/LDP/abs/html/ops.html
 
1 members found this post helpful.
Old 08-21-2011, 11:21 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
This is the kind of process that's probably better done with a for loop instead.

There are two basic styles of for loop.

The one that's probably most appropriate here, is the c-style loop.
Code:
for (( count=1 ; count <= 10 ; count++ )); do
	commands
done
The first entry (count=1) initializes the variable; the second (count <= 10) tests it, and runs the commands if true. Then the third entry (count++) modifies the variable after the loop commands execute, and starts the next iteration. In this case it increments the variable by one.


The other type of for loop is the standard one that simply iterates over a list of elements, such as numbers.

Code:
for count in 1 2 3 4 5 6 7 8 9 10 ; do
	commands
done
You can clean it up a bit with brace expansion.

Code:
for count in {1..10} ; do
	commands
done
More on loops can be found here:
http://mywiki.wooledge.org/BashGuide...ndConditionals
http://www.tldp.org/LDP/abs/html/loops.html

Do read the whole BashGuide when you have the time. It's really educational.
 
Old 08-23-2011, 08:39 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by Nylex View Post
let count=$count+1

There may be other ways of incrementing in Bash, but this is the one I know.
This is probably the best one:

Code:
(( count++ ))
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
stopping you have mail on Linux manoj.linux Linux - Enterprise 8 02-16-2011 10:14 PM
Stopping a while loop from user input 454redhawk Programming 4 01-15-2008 07:39 AM
Java Stopping an Infinite loop oulevon Programming 3 10-18-2004 11:11 PM


All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration