LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2009, 11:45 AM   #1
orange.toaster
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
bash "do-while" post-test syntax


The syntax for a while loop in bash is
Code:
while [ condition ]
do
<commands>
done
but what is the equivalent for the c-style
Code:
do
{
<commands>
}while(condition);
I can't seem to get the post-test loop syntax working in a bash script.

A possible work around is to use a break:
Code:
while [ 1 ]
do
<commands> <condition> || break
done
I would assume that there must be syntax for a post-test loop in bash.

Last edited by orange.toaster; 07-07-2009 at 11:50 AM.
 
Old 07-07-2009, 02:20 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
A quick glance at the ABS** boat anchor says there is no "do....while" construct in BASH. There is an "until" loop which might be useful.

**Advanced BASH Scripting Guide----free at http://tldp.org
 
Old 07-07-2009, 03:02 PM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by pixellany View Post
A quick glance at the ABS** boat anchor says there is no "do....while" construct in BASH. There is an "until" loop which might be useful.

**Advanced BASH Scripting Guide----free at http://tldp.org

UNTIL in bash is just a while with the condition negated. So it's logically speaking just a more readable version of.
Code:
while ! condition
It's not like the repeat/until post-test loop construct in Pascal or good old BASIC so it's not a great deal more use than the while.


There doesn't appear to be any post-test loop construct in bash that I can find.

Similar to Orange's example with the break, you could also do it like this, though it's still pretty kludgy.

Code:
while command
do
  continue
done
 
Old 07-07-2009, 04:26 PM   #4
gapan
Member
 
Registered: Feb 2007
Posts: 378

Rep: Reputation: 163Reputation: 163
I guess you could also do:
Code:
whatyouwanttodo()
{
<commands>
}

whatyouwanttodo
while [ condition ]
do
  whatyouwanttodo
done
 
  


Reply



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 write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 12:17 PM
Pls help to explain this syntax. "rpm -ivh bash.rpm --root /mnt/image --replacepkgs" nanosec Linux - Newbie 2 08-12-2008 01:37 PM
my var in bash doesn't work: ARRAY_$a_$b="test" SlackerJack Linux - General 3 01-13-2008 03:45 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LFS 6.2 Ch. 6.14 - 2nd Coreutils test returns "/bin.bash: No such file or directory" 0graham0 Linux From Scratch 2 08-23-2007 06:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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