Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 965e33040dd61030a94f0eb89877aee8 > files > 609

howto-html-en-20080722-2mdv2010.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
 <TITLE>BASH Programming - Introduction HOW-TO: Loops for, while and until</TITLE>
 <LINK HREF="Bash-Prog-Intro-HOWTO-8.html" REL=next>
 <LINK HREF="Bash-Prog-Intro-HOWTO-6.html" REL=previous>
 <LINK HREF="Bash-Prog-Intro-HOWTO.html#toc7" REL=contents>
</HEAD>
<BODY>
<A HREF="Bash-Prog-Intro-HOWTO-8.html">Next</A>
<A HREF="Bash-Prog-Intro-HOWTO-6.html">Previous</A>
<A HREF="Bash-Prog-Intro-HOWTO.html#toc7">Contents</A>
<HR>
<H2><A NAME="s7">7. Loops for, while and until</A>        </H2>

<P> In this section you'll find for, while and until loops.
<P> The <B>for</B> loop is a little bit different from other programming
languages. Basically, it let's you iterate over a series of 
'words' within a string.
<P> The <B>while</B> executes a piece of code if the control expression
is true, and only stops when it is false (or a explicit break is found
within the executed code.
<P> The <B>until</B> loop is almost equal to the while loop, except that
the code is executed while the control expression evaluates to false.
<P> If you suspect that while and until are very similar you are right.
    
<H2><A NAME="ss7.1">7.1 For sample</A>
        </H2>

<P> 
<BLOCKQUOTE><CODE>
<PRE>
        #!/bin/bash
        for i in $( ls ); do
            echo item: $i
        done
        
</PRE>
</CODE></BLOCKQUOTE>
<P> 
<P> On the second line, we declare i to be the variable that
will take the 
different values contained in $( ls ).
<P> The third line could be longer if needed, or there could
be more lines 
before the done (4).
<P> 'done' (4) indicates that the code that used the value of $i has
finished and $i can take a new value.
<P> This script has very little sense, but a more useful way to use the
for loop would be to use it to match only certain files on the previous
example
<P>
<H2><A NAME="ss7.2">7.2 C-like for</A>
        </H2>

<P> fiesh suggested adding this form of looping. It's a for loop
more similar to C/perl... for.
<BLOCKQUOTE><CODE>
<PRE>
        #!/bin/bash
        for i in `seq 1 10`;
        do
                echo $i
        done    
        
</PRE>
</CODE></BLOCKQUOTE>
<H2><A NAME="ss7.3">7.3 While sample</A>
         </H2>

<P> 
<BLOCKQUOTE><CODE>
<PRE>
         #!/bin/bash 
         COUNTER=0
         while [  $COUNTER -lt 10 ]; do
             echo The counter is $COUNTER
             let COUNTER=COUNTER+1 
         done
         
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P> This script 'emulates' the well known 
(C, Pascal, perl, etc) 'for' structure
<H2><A NAME="ss7.4">7.4 Until sample</A>
         </H2>

<P> 
<BLOCKQUOTE><CODE>
<PRE>
         #!/bin/bash 
         COUNTER=20
         until [  $COUNTER -lt 10 ]; do
             echo COUNTER $COUNTER
             let COUNTER-=1
         done
         
</PRE>
</CODE></BLOCKQUOTE>
<HR>
<A HREF="Bash-Prog-Intro-HOWTO-8.html">Next</A>
<A HREF="Bash-Prog-Intro-HOWTO-6.html">Previous</A>
<A HREF="Bash-Prog-Intro-HOWTO.html#toc7">Contents</A>
</BODY>
</HTML>