Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - 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 - 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.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - 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
    - Install Slackware on Your Old PC


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