Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - 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 - Controlling the loops


    (Page 4 of 4 )

      

    There are a couple of tools which will enhance your control over the loops. The "break" and the "continue" resemble heavily commands you may have already learned from C programming. The break will stop the loop and continue the execution of the script with the command line after the loop.

    The "continue" will not finish the current iteration and continue to the next. In other words, it will stop the execution of any further command inside the loop, and start over with the next item, from the beginning. In an extension of the C-like break here, we can use the break to exit from nested loops as well. Jumping out from a two level loop is done with the break 2 syntax.  

    To make an endless loop, you can use the true and the false commands. The true will always make the $? zero, while the false it will set it to one. Let us write a little script to illustrate this.  

    until false

    do

    while true

    do

    read alfa

    if [ "$alfa" = "one" ]

    then

    break;

    elseif "$alfa" = "two" ]

    then

    break 2;

    else

    echo "$alfa"

    continue;

    fi

    done

    echo Finished a while loop

    done

    The script above will go back into the first loop if you enter one, will exit from both loops on two, and in any other case it will print back the line and continue from the start of the while.

    Then there is the shift command. This will push the arguments with one to the front, and in the process, eliminate the first item.  

    For example, if you have inside the special variables $(number) as $1 - one, $2 - two and $3- three and the $# -3 representing the numbers of the arguments, after you call this you will have $# - 2 with $1 - two and $2 - three. It's a simple shift of these variables. We can use this to iterate through the arguments with the while loop:

    while [ $# -gt 0 ]

    do

    echo $1

    shift

    done

    You can call the shift command also with an argument, where the argument will represent the number with however many shifts will be made. Shift 2 will make $3 $1, and so on.

    If you want to exit from a script, you should use the exit command. The exit's argument is its return value. In a successful case, it returns 0; otherwise, it returns an integer that is not zero.

    For example:

    #finding file alfa

    for i in $(ls)

    do

    if [ "$i" = "alfa" ]

    then

    exit 0

    fi

    done

    exit 1

    If we do not use this in a script, the return value of the shell program is equal to the return value of the last executed command.

    With this, we have finished the lesson on loops. Next week we will put together everything we have learned so far, extend it with a couple of new tools, and write scripts that will achieve complex tasks. Make sure you rate my article if you read it, to express your thoughts. Moreover, you can state them explicitly in the blog following this article. Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek