Shell Script Tips -1

The below shell scripts are typical example codes which helps us to understand certain frequently used codes.

1. How to get the absolute or full path while calling another shell script from one shell script.

#!/bin/sh
source $(readlink -f "ifix_config.sh") uninstall

The above code snippet calls another shell script by name ifix_config.sh. which is available in the same directory of current script.
Abosolute path will be used with the readlink -f option there. uninstall isa parameter passed to it.

2. How to check how many parameters are passed in to a shell script..
if [ $# != "1" ]; then
    echo "Usage: ifix_config.sh install or ifix_config.sh uninstall"
    exit
else
    ....
    ....
$# represents total number of parameters passed to a shell script when called.
In the above example code if at least one parameter is not passed it throws a message and exits.

3. How to check if a variable is set or not . How to check if a viariale has empty string ?

    LOCALE_CODE=$LANG
    if [ -z $LOCALE_CODE ];
        then JAZZSM_SYSTEM_LOCALE="en_US"
    fi   

In the above example snippet we are setting some value for LOCALE_CODE if in case it is not set for some reason -z option with if condition returns true
so in above example if it( if [ -z $LOCALE_CODE ] ) is true  it will set it to en_US

4. How to get the parent directory path of a file or directory ?

    PresentDir=`dirname $PWD` 

When dirname is given a pathname, it will delete any suffix beginning with the last slash ('/') character and return the result.
For example:

 $ dirname /usr/home/carpetsmoker/dirname.wiki
   /usr/home/carpetsmoker

5. How to get the only filename when absolute path is given ?

Example

    $ basename /home/jsmith/base.wiki
    base.wiki

    $ basename /home/jsmith/base.wiki .wiki
    base

    IFIX_NO=`basename $PWD`
    0001

6.     How to read a properties file in shell script  ?

    # Reading settings.properties to find IM Install location
    IM_HOME=`sed '/^\#/d' $PropertiesFile | grep 'product.install.location'  | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'`
    echo IM Install Location is : $IM_HOME
   
    Example :
    $PropertiesFile contents are like below ..
    -------------------------------------------
    #Fri, 01 Mar 2013 02:59:37 -0500

    product.was.user=smadmin
    product.jdbc.lib.path=/opt/IBM/JazzSM/lib/db2
    product.data.location=/var/ibm/InstallationManager
    product.install.location=/opt/IBM/InstallationManager/eclipse
    -------------------------------------------
    The while process is to Scan through the file to get an individual property

    This works well if you just want to read a specific property in a Java-style properties file. Here's what it does:

    * Remove comments (lines that start with '#') >> sed '/^\#/d' myprops.properties
    * Grep for the property >>  grep 'someproperty' 
    * Get the last value using >> tail -n 1
    * Strip off the property name and the '=' (using {{cut -d "=" -f2-}) to handle values with '=' in them).

     sed '/^\#/d' myprops.properties | grep 'someproperty'  | tail -n 1 | cut -d "=" -f2-

    It can also be handy to strip off the leading and trailing blanks with this sed expression:

    s/^[[:space:]]*//;s/[[:space:]]*$//

    Which makes the whole thing look like this:

    sed '/^\#/d' myprops.properties | grep 'someproperty'  | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'

Reference

7.  How to check file existance before running it ?

[ -f "$SCRIPT_LOCATION/myshellscript.sh" ] && . "$SCRIPT_LOCATION/myshellscript.sh"   

Having quotes for the file path eliminates issues arise out of spaces in the path.

8. How to read input values through shell script ?

    # Ask for WAS credentials
    if [ -z "${JAZZSM_WAS_USERNAME}" ]
    then
      echo -n "${wasUserNamePrompt}: "
      read JAZZSM_WAS_USERNAME
    fi

echo -n "${wasUserNamePrompt}: "     >> it helps to prompt user with particular string before commandline prompt while running shell script, in order to resolve wasUserNamePrompt string we need to run its correspinding properties file before this call .
For example config_messages.sh has text like below , and when it is run before the above command it will resolve this value.

#!/bin/sh

#NLS_MESSAGEFORMAT_NONE
#NLS_ENCODING=UTF-8

wasUserNamePrompt="Enter the Websphere Application Server user name:"

wasPasswordPrompt="Enter the Websphere Application Server password:"


0 comments to "Shell Script Tips -1 "

Post a Comment

Who ever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs u can at least use Name/URL option which don’t even require any sign-in, good thing is that it can accept your lovely nick name also and URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")
వీలయితే నాల్గు పోస్టులు ...కుదురితే ఒక కామెంటూ ...

Translate

Enter your email address:

Buffs ...

Visitors


hits counter
View My StatsCheck Google Page Rank

Add to Google Reader or Homepage

Bookmark and Share

Tags


Powered by WidgetsForFree