LinuxQuestions.org
Help answer threads with 0 replies.
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-17-2009, 03:45 PM   #1
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Rep: Reputation: 15
always true while loop


Hey guys-
Here's a real easy one for you. I need to make a while loop in bash that's always true. I will manually break out of it later on in the script. I thought one of these would work, but the script never enters the loop for some reason:
Code:
while:

while (1)

while [1]
Thoughts?

Jarrod
 
Old 08-17-2009, 03:50 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
So what's the body of your loop?
Code:
while true; do   date;   sleep 5; done                                                                                        
Tue Aug 18 08:49:35 UTC 2009
Tue Aug 18 08:49:40 UTC 2009
^C


Cheers,
Tink
 
Old 08-17-2009, 04:40 PM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,072

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
In bash there's a command called ":", which is always evaluated to true, you could use that to shorten it even more (however I advice to use "true" if you are writing this into a script if only for the sake of readability).

Code:
$ while :; do echo foo; sleep 5; done
foo
foo
You could also use any other command that you are sure will end without error (true). In this case you could use this, for example:

Code:
$ while sleep 5; do echo foo; done
Whose effect will be slightly different, I bet you can guess what the difference is
 
  


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 03:49 PM
Is it true? sycamorex General 3 01-08-2009 05:28 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM


All times are GMT -5. The time now is 04:36 PM.

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