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 
Case Studies 
iPad Development 
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

    - 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
    - Firefox Plugins You`re Not Using (and Should...
    - Working with MP3 ID3 Tags in FTP Server Usin...
    - How Switching to Linux Can Make Your Computi...
    - Set Up Your Home Office on Linux: a Guide fo...
    - Putty File Transfer Commands in SSH Protocol
    - Setting Up Ubuntu for Your Home Office
    - Installing Mint Linux





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek