Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - Loops in the UNIX Shell
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Attention: Forum and Site Maintenance
    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap