Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - The Test in the UNIX Shell
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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.
    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 6 - Follow our Sitemap