Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - The Test 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

The Test in the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-05-20

    Table of Contents:
  • The Test in the UNIX Shell
  • Files and strings
  • Logical expressions and numbers
  • The Case

  • 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


    The Test in the UNIX Shell - The Case


    (Page 4 of 4 )

    The cases are an extension of the decision making. If we check if a statement is true or false, the case goes beyond this. A case assumes that there are multiple options available, and each one of them requires a different command line sequence. A case will have a single variable for which we will test what value it has from the list of the given values. The syntax of this is similar to that of the if.

    case [the variable] in

     

    [match_1]) ;;

    [match_2]) ;;

    ...

    [match_n]) ;;

     

    esac

     

    This type of case will only enter into the first valid match, unlike the one you may know from other programming languages. There is no need for break statements here. After entering into the first match, it will execute all of the command until the first double ; and continue afterward with the commands after the case. For example, initializing a variable according to the first argument of a script looks like this:

    case "$1" in

    0) alfa="zero";

    echo Option zero is active;;

    1) alfa="one";;

    2) alfa="two";;

    esac

    The match can contain a meta-character also. We have three types of characters available for this. The * stands for anything. The ? stands for a single character that can be anything. For alternatives we may use the | character. The example below will describe this:

    #!/bin/bash

     

    #check if we have a single argument

    if [ $# -ne 1 ]

    then

    echo Wrong Number of arguments

    exit

    fi

     

    case $1 in

     

    #exact match only

    abc)

    if rm abc 2>/dev/null

    then

    echo abc file deleted

    fi;;

     

    $this will match any string that contains 3 letter and the last two are bc

    ?bc) echo The first letter is wrong ;;

     

    # a little alternation

    a*c| ??)

    echo The first argument is either containing two letters

    echo Or it starts with a and finishes with c;;

    esac

    This will be all for today. Make sure you play around with these examples on your terminal and experiment with every one of the options. Learning all of the arguments is a damn hard task. Using them and playing with them repeatedly will make this titanic job child's play. After a time, it will jump right out at you when you need to use them, and then just go ahead and use them without needing to think too much about it. For this, all you need to do is to use them again and again.

    Next week I will cover the loop structure inside UNIX shell programming. I will show you what kind of structures resolve this issue, what to use and how to use them so that you will end up with minimal code written and the task completed in your scripts. Please rate my article. Moreover, ask your questions in the blog, otherwise you may end up with dilemmas unanswered. 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek