Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Loops in the UNIX Shell
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Loops in the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2009-05-27

    Table of Contents:
  • Loops in the UNIX Shell
  • The loops
  • More loops
  • Controlling the loops

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Loops in the UNIX Shell - The loops


    (Page 2 of 4 )

    Now then, the first loop we will learn is the "for." This is a simple one. This will execute the command lines that you put inside it for every value inside a list. The syntax is simple:

    for i in list

    do

    command line

    ...

    done

    Inside the loop we can use $i to refer to the current item in the list. This way $i will get all the values, one after another, inside the list. The list is optional; we can choose to add no specific list. Now the loop will look like this:

    for i

    do

    command line

    ...

    done

    If this is the case, then the $i will iterate through the arguments of the script. Therefore, we can conclude that, by default, a loop will iterate through the $@ special variable, containing a list of the variables. Here is a simple usage of this, showing how to echo back all the arguments of a script/shell program:

    for i

    do

    echo "$i"

    done

    The list may be explicit also, as in the example below:

    for i in alfa beta omega

    do

    ls $i 2>/dev/null

    done

    This will list the content of the directories alfa, beta and omega, if they exist. If we want to iterate through a sequence, we can use the seq command to generate one. This will have as arguments from where, in which steps, and until where to generate the sequence.

    for i in $( seq 1 2 10)

    do

    echo $i

    done

    This will print out 1, 3, 5, 7, and 9 to the terminal. Of course, we can use the $() syntax to store any list of items, and to iterate through those with the "for" loop. For example, iterating through the items inside the working directory looks like this:

    for i in $( ls )

    do

    if [ "$i" -x ]

    then

    echo You can execute "$i"

    fi

    done

    It is important to note that the bash here will call the original ls function, and not the ls alias that returns color signaling. This way, it will not interfere with our work.

    More Miscellaneous Articles
    More By Gabor Bernat


     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek