Q:

using environment variables in service script

I am facing an issue using environment variables in my service script. 
In my services script, i am using an environmental variable i.e. INSTALL_DIR whose value may vary on different system. I have to get the installation directory from $INSTALL_DIR and then i have to start the service. when i am running the service script the environment variable is not sourced at all. 
Is it possible to source the installation directory from INSTALL_DIR environment variable. another option i can think is dynamically creating the service script using INSTALL_DIR environment variable. 
echo "INSTALL DIR: ${INSTALL_DIR}"
name=`basename $0`
pid_file="/var/run/$name.pid"
get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

Start()
{
    echo "Starting Application"

    if is_running; then
        echo "[`get_pid`] Already Started"
    else
        if [ -z "$user" ]; then
            nohup $INSTALL_DIR/bin/application 2>&1 &
        else
            nohup sudo -u "$user" $cmd 1> $INSTALL_DIR/bin/application 2>&1 &
        fi
        echo $! > "$pid_file"
        if ! is_running; then
            echo "Unable to start, see logs"
            exit 1
        fi
        echo "[`get_pid`] Started"
    fi
}

I am trying to run the application using following command
service application start

A:

In my services script ... I have to get the installation directory from $INSTALL_DIR and then i have to start the service. 

Your question isn't really about shell scripting, but about your system's startup.  Unfortunately that process varies by Linux distribution, and tends to be poorly documented.  
For example, man service says, service runs a System V init script or upstart job in as predictable an environment as possible, removing most environment variables  and  with the current working directory set to /., but man upstart says:
$ man -k upstart
upstart: nothing appropriate.

Not only that, but the service manpage specifically lists the environment variables a script will start with.  Needless to say, yours isn't among them.  
The traditional approach to parameterizing startup scripts is to put the information in a known file, normally in /etc, and reference that file in the script.  In your case, you could do something like:
INSTALL_DIR=$(cat /etc/my-install-dir.cfg)

and then proceed accordingly.  
There might be ways to coerce your startup to support other environment variables.  But, sooner or later, the information you need has to be stored somewhere on the filesystem.  It seems to me the simplest approach is to reserve a filename to hold that information, and read that file directly.  

