Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Shell Script Writing
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

Shell Script Writing
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-06-03

    Table of Contents:
  • Shell Script Writing
  • The (()) structure
  • Select and Getopts
  • Putting it all together

  • 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


    Shell Script Writing - The (()) structure


    (Page 2 of 4 )

    The (()) structure may be used for evaluating arithmetic expressions or to write loops, as in the C programming language. I will discuss arithmetic expressions first. Earlier, I presented the expr command. This is good for making basic operations, but it is limited, and you need to remember to leave extra spaces between the operators and the numbers.

    To make operations simpler, a new syntax has been introduced into Bash with  version 2.04. However, the presence of this is not guaranteed, and if your goal is to write portable code, you should use the expr command. The syntax of this is:

    variable=$(( arithmetic expression))

    So all you need to do is double the parenthesis after a dollar sign. The arithmetic expressions rule closely follows the C language. Anything that would work there is possible here as well. Moreover, we can even use this to test for integer numbers (of course, we will not add the dollar sign now):

    i = 2;

     

    while (( i < 10))

    do

    echo $i

    ((--$i))

    echo $i

    i=$((i+2))

    done

    You can use any operator from the C language. In the case of the Bash shell, it will always use integer numbers, while the Korn shell will use double precision real numbers. Using the dollar sign in front of the variables inside this structure is not mandatory. Nevertheless, if you do use it, that will not be a problem. Simply put, it is optional. This is true only for your variables with names. When you want to refer to the special variables like the $1, you will have to add it.

    a=$(($1+1))

    b=$((a*2))

    Inside the double parentheses there are not valid meta-characters of the shell. Therefore, you can use the * for multiplication, just like in the C language. There is no need to precede it with a character. Using the assigning operators like the +=, -=, *= and so forth will require you to leave out the dollar sign prior to the variable.

    ((b-=3))

    You can use parentheses to clarify an expression. The evaluation of tests (like the >, <, <= and so forth) inside the structure is one. Nevertheless, once we get out of this, the shell will change the $? special variable to the negation of this (therefore to zero if it was one). This allows us to synchronize the true statement from the C language with the one inside the shell. This reveals why the upper while loop works.

    The operator for raising a number to a power is **(two * following closely one after another). Aside from this operator, the lack or addition of spaces inside the structure will have no effect. Therefore, the following two examples are both true and will produce the same end:

    a=$(( 2 ** 3 ))

    a=$( 2**3 ))

    We can use this even to make a C style for loop. In practice, the syntax of this strongly follows the syntax of the basic for loop in the shell, but the list will be replaced with a C style for:

    for (( initial value; do until true; execute after each loop))

    do

    #add here the command lines

    done

    For instance:

    for (( i=10; i > -1; --i))

    do

    #print a list of this

    echo $i

    done

    This will give us an easier way to pass through a sequence of integer numbers. Then again, you sacrifice a little portability. It is up to you if you are willing to pay the price, as most of the new UNIX-based distribution will have this.

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