uderstanding .ksh -part 2

hi guys

This question is in continuation to my earlier question(http://www.experts-exchange.com/OS/Linux/Q_28253505.html#a39536410)
We inherited some .ksh from another team which we are trying to understand.

My questions are in comments..

pushd . > /dev/null              #what does pushd . > mean
cd `dirname ${SCRIPT_PATH}` > /dev/null    # cd is change directory but little confused what the entire line means
SCRIPT_PATH=`pwd`                          # $pwd shows the present working directory, what does 'pwd' mean
CO_HOME=`dirname $SCRIPT_PATH`;      
popd  > /dev/null                         #what does popd mean?

# specify the JVM heap size
. $CO_HOME/properties/app.cf

if [ -f $JAVA_HOME/bin/java ]; then      #what does -f mean

if [[ $1 == '-jmx' ]]; then
    JMXPROPERTIES="-Dtangosol.coherence.management=all -Dtangosol.coherence.management.remote=true"
    shift     #what does shift command mean?

CONFIG_FILE=$1    # $1 is the first parameter

echo "Sleeping for 10 secs"
sleep 10

TODAY=$(date '+%Y%m%d%S')
LOGFILE=${CO_HOME}/log/stdout.DefaultCacheServer.$CONFIG_FILE.$TODAY  #construct the LOGFILE

$JAVAEXEC -server -showversion $JAVA_OPTS  -Dtangosol.coherence.cacheconfig=${CONFIG_FILE} -cp "$CO_HOME/lib/coherence.jar:$CO_HOME/properties" com.tangosol.net.DefaultCacheServer $1 > $LOGFILE  2>&1 &    #What is this entire statement doing?

CHILD_PID=$!   # what does $! mean ?
echo "${CHILD_PID}" > ${PID_FILE}   # what does > mean in this echo statement?

RET_VALUE=$?      # what does $? mean 
echo "RC : $RET_VALUE"

Open in new window

Any help will be really appreciated.
Who is Participating?
TintinConnect With a Mentor Commented:
Line 1 - Pushes the current directory (.) onto the stack.  The >/dev/null gets rid of any output

Line 2 -  cd's to the first path in the variable $SCRIPT_PATH.   For example, if 4SCRIPT_PATH was set to /usr/local/bin, then then dirname would return /usr

Note that SCRIPT_PATH must be set elsewhere, otherwise this line makes no sense.

Line 5 - Pops the last directory "pushed" to the stack and discards any output.  

Line 12 - The -f tests if the specified file exists

Line 20 - shift is used to shift the specified parameters.   For example, if I call a script with

./script ABC XZY

Then $1 contains 'ABC' and $2 contains 'XZY'.

If I do


$1 now contains 'XZY' and $2 contains nothing

If I do

shift 2

It shifts both parameters, effectively leaving no parameters.

In your specific script, it can be optionally called with the -jmx flag along with the config file.   By testing if -jmx is the first parameter, the parameters are then shifted so that the config file is always $1

Line 33 - This is starting java with a whole bunch of parameters.

The part:

>$LOGFILE 2>&1

Means send all output (stdout and stderr) to $LOGFILE.   The 2>&1 part of it means redirect stderr to stdout.   This is a very common syntax to capture all output.

The & at the end of the line means run it in the background.

Line 35 - $! is a special built in variable that contains the process ID (PID) of the last command run in the background (in this case, the java process)

Line 37 - echo the value of $CHILD_PID into the file with the path set in $PID

Line 39 - $? is a special built in variable that contains the exit status of the last command.   In almost all cases, 0 means success and non-zero means failure.   In this case it would be non-zero if the echo command on line 37 failed for whatever reason, eg: permissions.
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
Here are a few:

$? - return code of previously executed command
$! - child process id
>  - redirect output
shift - 'shift' parameters one position to the left, use first parameter, shift and use first parameter again (until no more are there)
-f - check whether a file exists
popd / pusd - pusd currect directory on the stack, popd - get last entry from stack
Gerwin Jansen, EE MVETopic Advisor Commented:
pwd - print working directory - `pwd` executes pwd in a subshell, used to assign the output to a variable for instance
cd `dirname ${SCRIPT_PATH}` > /dev/null - first executes dirname to get the directory part of what is in ${SCRIPT_PATH} (in a sub shell) - then cd (changes directory) to the output of that
line 33 - basically starts a java (server) application with a few parameters, sends output to a logfile, redirect output and in the background (& at the end)
royjaydAuthor Commented:
thanks very much guys, i will get back with some questions if i have.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.