Miscellaneous
  Home arrow Miscellaneous arrow 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


    (Page 1 of 4 )

    Shell scripts are designed to automate tasks that you would otherwise need to do manually, one step after another. This allows you to build up a sequence and a structure of commands that will take these steps instead of you. For this, you can use the tools present in the repertoire of the UNIX shell and its control mechanism. Exactly how this will come together to make a complete system is the subject of today's article.

    Now in the articles up to this point related to UNIX shell program/script writing, I have covered different aspects of how to perceive and use variables, commands and more. Moreover, I also managed to illustrate two of the most basic necessary control mechanisms in a programming language: the "if-then-else" and the loop structure.

    If you missed this, I strongly recommend searching for the articles here on the site or under my name. Please skim through them. This article will be by no means  enough for you to write a script on your own. Learning a script language is a little more time-consuming. You will need to comprehend more concepts and see more examples before you can write your own scripts.

    The core of this article will introduce a couple of new syntaxes that will help you to further simplify the writing of scripts, and present a couple of examples of scripts using all of the data I have presented so far. In particular, I will present the (()) structure, work with the strings, and explain how to use the getopts and select command and a couple of other UNIX tools.

    I will start with the operations with strings. Sometimes it is crucial to perform a couple of simple operations in a variable -- for example, to replace a portion from a string or to delete the start/end, fast and simple. In this case, you can use the ${} syntax coupled with the operators presented in the table below:

    How to use it

    What it does

    ${variable#pattern}

    If the pattern matches to the start of the value of the variable, then it will delete the shortest matching part and return that.

    ${variable##pattern}

    Like the syntax above, with the difference being that now the longest matching part will be deleted.

    ${variable%pattern}

    With the % operator, the pattern will have to occur at the start of the value of the variable. This will delete the shortest match

    ${variable%%pattern}

    This will delete the longest match and the end, therefore return this.

    ${variable/pattern/replace_with}

    This will replace a pattern matching the value of the variable with the replace_with string.

    If you start the pattern with the #, it will refer to the start of the variable. The % is equivalent to the end.

    For example:

    alfa=Avril_Avril

    echo ${alfa}

    Avril_Avril

    echo ${alfa#Avril}

    _Avril

    echo ${alfa%Avril}

    Avril_

    echo ${alfa/_/=}

    Avril=Avril

    Using the double operators ( ##, **) has its role only when you take advantage of the special characters * (any characters - none, one or more) and ? (any character - one). In these cases, there may be shorter or longer matching patterns, as the example below will show it (we refer to the same alfa variable as before):

    echo ${alfa##A*r}

    il

    echo ${alfa%%r*l}

    Av

    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 4 Hosted by Hostway
    Stay green...Green IT