LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-28-2010, 02:14 AM   #1
jestinjoy
Member
 
Registered: May 2004
Location: India
Distribution: Ubuntu 9.04, Debian Lenny
Posts: 121

Rep: Reputation: 22
Smile shell programming help: while loop


Hi,

I am trying to do some shell programs. I tried some sites for help. Regarding the while loop, they give the structure as
Code:
while [ n1 -lt 500 ]
do
	echo $((n1+100))
done
But the below code also worked for me

Code:
while ((n1 > 500))
do
	echo $((n1+100))
done
By using (( )) i could use while, for... But the documentations didnt follow this way. Please help me on this. Any problem using this way. I mainly use this for datastructure programming.
 
Old 06-28-2010, 02:29 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
For arithmetic comparison, as in your case, (( )) is fine. See man bash, sections on arithmetic evaluation and conditional expressions.

Do note that n1 -lt 500 is equivalent to n1 < 500, not n1 > 500 (-lt is short for "less than").
 
Old 06-28-2010, 02:32 AM   #3
jestinjoy
Member
 
Registered: May 2004
Location: India
Distribution: Ubuntu 9.04, Debian Lenny
Posts: 121

Original Poster
Rep: Reputation: 22
re

thanks for the info.
 
Old 06-28-2010, 02:55 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
You can also use the below code as per your requirement.

Code:
-sh-3.00$ awk '
BEGIN {
x=1;
while(1)
{           
print "Iteration"
if ( x==5 )
break;
x++;
}
}
'
Iteration
Iteration
Iteration
Iteration
Iteration
OR

Code:
awk 'BEGIN{while(i++ < 5)print "Iteration\t"}'
 
Old 06-28-2010, 03:11 AM   #5
pr_deltoid
Member
 
Registered: Jun 2010
Distribution: Fedora
Posts: 289

Rep: Reputation: 41
http://tldp.org/LDP/abs/html/index.html is good.
 
  


Reply

Tags
bash, shell script, while



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
C programming Loop question? chibi2666 Programming 8 06-16-2009 09:15 PM
Bash programming while loop Histamine Programming 3 10-29-2007 06:35 PM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:26 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
Open Source Consulting | Domain Registration