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.

source $(readlink -f "") uninstall

The above code snippet calls another shell script by name 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: install or uninstall"
$# 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 ?

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

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/

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


    $ basename /home/jsmith/

    $ basename /home/jsmith/ .wiki

    IFIX_NO=`basename $PWD`

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

    # Reading 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

    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'
    * 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' | 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:


    Which makes the whole thing look like this:

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


7.  How to check file existance before running it ?


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}" ]
      echo -n "${wasUserNamePrompt}: "

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 has text like below , and when it is run before the above command it will resolve this value.



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

Whoever 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 doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...


Powered by WidgetsForFree