Using env variables to run a program

Hi there,

I need urgent help with a small program that is run via shell script. Unfortunately I only understand the bare basics of shell scripting and can't figure out how to do this.

We have a program that tests the connection between 3 servers. I have a script that lets the program run on one specific server.
We have nearly the same directory structure on all servers and I need the script to be able to run this program from all servers without using individual scripts for each one.
That is we deploy the program and then simply copy the script from one server to the other 2.

Here is the script so far.

#!/bin/ssh

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
	ls=`ls -ld "$PRG"`
	link=`expr "$ls" : '.*-> \(.*\)$'`
	if expr "$link" : '/.*' > /dev/null; then
		PRG="$link"
	else
		PRG=`dirname "$PRG"`/"$link"
	fi
	echo "PRG = $PRG"
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`

# Only set $CT_HOME if not already set
# [ -z "CT_HOME" ] && CT_HOME=`cd "$PRGDIR/.." ; pwd`
CT_HOME=`cd "$PRGDIR/.." ; pwd`

# Make sure prerequisite environment variables are set
if [ -z "$JAVA_HOME" ]; then
	# For our servers we assume our standard jdk location
	if [ -d "/spd/iu/system/java/jdk1.6.0_65" }; then
		JAVA_HOME="/spd/iu/system/java/jdk1.6.0_65"
		echo "Seetting JAVA_HOME to $JAVA_HOME"
	else
		echo "Please set JAVA_HOME variable to a JDK >= 1.6.x"
		exit 1
	fi
fi

_RUNJAVA="$JAVA_HOME"/bin/java

# Set classpath
CLASSPATH="$CT_HOME"
CT_LIBDIR="$CT_HOME/lib"
if [ -d "${CT_LIBDIR}" ]; then
	for i in "${CT_LIBDIR}"/*.jar; do
		CLASSPATH="$CLASSPATH":"$i"
	done
else
	echo "Does not exist: ${CT_LIBDIR}"
fi

echo "CLASSPATH = ${CLASSPAT}"

#/spd/iu/system/util/te-con/te-con-1.2.0:/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0.jar:/spd/iu/system/util/te-con/te-con-1.2.0/lib/arcjob-3.4.1.jar

$_RUNJAVA -d64 -Djava.library.path="/spd/iu/system/wls/wls1035/wlserver_10.3/server/native/solaris/x64/:/spd/iu/web/kon/lib/" -cp "/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/web/kon/lib/*" com.luz.telco.sandbox.tecon.ConnectionTest builtin/luz-kon-new.properties

Now I only get a fraction of what it does and the comments don't help me that much either.

Can anyone point me in the right direction?

Help would be much appreciated!

---------- Post updated at 03:37 PM ---------- Previous update was at 12:58 PM ----------

So far the only things referring to something unique on that server I found are:

In the last line:

These directories have different names on each server.

And the arcjob-3-4-1.jar doesn't exist on all servers (I'll have to find an equivalent expression for each server).

You almost certainly will need to have arcjob-3-4-1.jar on each of the servers

That shouldn't be the problem in the long run.

But I still don't understand how this script is limited to only that one server, as I understand so little of what it does.

I understand that it checks for a symbolic link in the beginning, but everything else in the first while is lost on me.

The first thing I understand after that is when it sets the Java homedir.

Everything after that is lost on me again.

---------- Post updated at 03:56 PM ---------- Previous update was at 03:49 PM ----------

I found an older version of the script, not yet fitted to that server:

#!/bin/sh

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
  echo "PRG = $PRG"
done


# Get standard environment variables
PRGDIR=`dirname "$PRG"`

# Only set $CT_HOME if not already set
# [ -z "CT_HOME" ] && CT_HOME=`cd "$PRGDIR/.." ; pwd`
CT_HOME=`cd "$PRGDIR/.." ; pwd`


# Make sure prerequisite environment variables are set
if [ -z "$JAVA_HOME" ]; then
  # For our servers we assume our standard jdk location
  if [ -d "/spd/iu/system/java/jdk1.6.0_65" ]; then
    JAVA_HOME="/spd/iu/system/java/jdk1.6.0_65"
    echo "Setting JAVA_HOME to $JAVA_HOME"
  else
    echo "Please set JAVA_HOME variable to a JDK >= 1.6.x"
    exit 1
  fi
fi

_RUNJAVA="$JAVA_HOME"/bin/java

# Set classpath
CLASSPATH="$CT_HOME"
CT_LIBDIR="$CT_HOME/lib"
if [ -d "${CT_LIBDIR}" ]; then
  for i in "${CT_LIBDIR}"/*.jar; do
    CLASSPATH="$CLASSPATH":"$i"
  done
else
  echo "Does not exist: ${CT_LIBDIR}"
fi

echo "CLASSPATH = ${CLASSPATH}"

$_RUNJAVA -cp ${CLASSPATH} com.luz.telco.sandbox.tecon.ConnectionTest $@

There is an obvious typo:

echo "CLASSPATH = ${CLASSPAT}"

should be:

echo "CLASSPATH = ${CLASSPATH}"

but fixing that won't change the behavior of the script; it will just make the status message more meaningful.

Thank you, I corrected that.

Now the script works perfectly fine on that one machine, but it was designed to do so.

I need it to work on the other two, so that when tecon is deployed it can be run via the script. Creating a script for every server would work, but is not desired, we want one script that works on every server.

Well, what's different on your other servers?

/spd/iu/web/kon/lib/ is /spd/iu/web/ent/lib on one and */prod/lib on the other.

If that's the only thing "personalized" about this script I just need a way for it to look for either of these.

---------- Post updated 12-02-14 at 10:04 AM ---------- Previous update was 11-02-14 at 04:39 PM ----------

Ok, I solved one of my problems.

Now:

# Set classpath
CLASSPATH="$CT_HOME"
CT_LIBDIR="$CT_HOME/lib"
if [ -d "${CT_LIBDIR}" ]; then
	for i in "${CT_LIBDIR}"/*.jar; do
		CLASSPATH="$CLASSPATH":"$i"
	done
else
	echo "Does not exist: ${CT_LIBDIR}"
fi

echo "CLASSPATH = ${CLASSPAT}"

#/spd/iu/system/util/te-con/te-con-1.2.0:/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0.jar:/spd/iu/system/util/te-con/te-con-1.2.0/lib/arcjob-3.4.1.jar

$_RUNJAVA -d64 -Djava.library.path="/spd/iu/system/wls/wls1035/wlserver_10.3/server/native/solaris/x64/:/spd/iu/web/kon/lib/" -cp "/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/web/kon/lib/*" com.luz.telco.sandbox.tecon.ConnectionTest builtin/luz-kon-new.properties

The last two paragraphs should be in a environment variable. I was told, that I could somehow use the CLASSPATH bit to do this, but I don't know how.

Note sure if it works,
couldnt you try accessing all 3 dirs at once with something like: /spd/iu/web/{kon,ent,prod}/lib/

Or probably better using like:

for VARIANT in kon ent prod;do
    cp /spd/iu/web/$VARIANT/lib/* $NEWDEST
done

But then again, this might break up the server's file structure and PATH definitions....

I'm not really understanding this line:

$_RUNJAVA -d64 -Djava.library.path="/spd/iu/system/wls/wls1035/wlserver_10.3/server/native/solaris/x64/:/spd/iu/web/kon/lib/" \
    -cp "/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/web/kon/lib/*" com.luz.telco.sandbox.tecon.ConnectionTest builtin/luz-kon-new.properties

But then again, it seems to me it contains all 3 diffrent locations...

Ok, I got an env variable that solves my problem with the 3 different dirs.

Now I want to have the paths for this argument in an external .conf file

$_RUNJAVA -d64 -Djava.library.path="/spd/iu/system/wls/wls1035/wlserver_10.3/server/native/solaris/x64/:/spd/iu/web/$STAGE/lib/" -cp "/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/web/$STAGE/lib/*" com.luz.telco.sandbox.tecon.ConnectionTest builtin/luz-$STAGE-new.properties

But when I create a file with

LIBPATH="/spd/iu/system/wls/wls1035/wlserver_10.3/server/native/solaris/x64/:/spd/iu/web/$STAGE/lib/" -cp "/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/web/$STAGE/lib/*" com.luz.telco.sandbox.tecon.ConnectionTest builtin/luz-$STAGE-new.properties

And enter into the original file:

CONFIG_FILE="/spd/iu/system/util/te-con/te-con-1.2.0/lib/tecon.conf"
$_RUNJAVA -d64 -Djava.library.path=LIBPATH

it doesn't really work

---------- Post updated at 09:59 AM ---------- Previous update was at 09:34 AM ----------

I want to test if the file exists at all, so I added:

#Test if config file exists
CONFIG_FILE="/spd/iu/system/util/te-con/te-con-1.2.0/bin/tecon.conf"
if [ -e $CONFIG_FILE ]; then
.$CONFIG_FILE
echo "Config file found."
fi

When I run the script it doesn't work.
The error message is:

#Test if config file exists and does not have null size 
CONFIG_FILE="/spd/iu/system/util/te-con/te-con-1.2.0/bin/tecon.conf"
if [ -s "$CONFIG_FILE" ]; then 
. $CONFIG_FILE
echo "Config file found."
fi

It should be a space between the dot and the $CONFIG_FILE in the line

. $CONFIG_FILE

I still get the same error.

Here a few more details:

I run the script with ./connectiontest.sh
It then prints out:
Setting JAVA_HOME to /spd/iu/system/java/jdk1.6.0_65
CLASSPATH = /spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/system/util/te-con/te-con-1.2.0/lib/sapjco3-3.0.7jar
./connectiontest.sh: test: argument expected

My script currently looks like this:

#!/bin/ssh

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
	ls=`ls -ld "$PRG"`
	link=`expr "$ls" : '.*-> \(.*\)$'`
	if expr "$link" : '/.*' > /dev/null; then
		PRG="$link"
	else
		PRG=`dirname "$PRG"`/"$link"
	fi
	echo "PRG = $PRG"
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`

# Only set $CT_HOME if not already set
# [ -z "CT_HOME" ] && CT_HOME=`cd "$PRGDIR/.." ; pwd`
CT_HOME=`cd "$PRGDIR/.." ; pwd`

# Make sure prerequisite environment variables are set
if [ -z "$JAVA_HOME" ]; then
	# For our servers we assume our standard jdk location
	if [ -d "/spd/iu/system/java/jdk1.6.0_65" }; then
		JAVA_HOME="/spd/iu/system/java/jdk1.6.0_65"
		echo "Seetting JAVA_HOME to $JAVA_HOME"
	else
		echo "Please set JAVA_HOME variable to a JDK >= 1.6.x"
		exit 1
	fi
fi

_RUNJAVA="$JAVA_HOME"/bin/java

# Set classpath
CLASSPATH="$CT_HOME"
CT_LIBDIR="$CT_HOME/lib"
if [ -d "${CT_LIBDIR}" ]; then
	for i in "${CT_LIBDIR}"/*.jar; do
		CLASSPATH="$CLASSPATH":"$i"
	done
else
	echo "Does not exist: ${CT_LIBDIR}"
fi

echo "CLASSPATH = ${CLASSPATH}"

#/spd/iu/system/util/te-con/te-con-1.2.0:/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0.jar:/spd/iu/system/util/te-con/te-con-1.2.0/lib/arcjob-3.4.1.jar

#Test if config file exists and does not have null size 
CONFIG_FILE="/spd/iu/system/util/te-con/te-con-1.2.0/bin/tecon.conf"
if [ -s "$CONFIG_FILE" ]; then 
. $CONFIG_FILE
echo "Config file found."
fi

$_RUNJAVA -d64 -Djava.library.path="/spd/iu/system/wls/wls1035/wlserver_10.3/server/native/solaris/x64/:/spd/iu/web/$STAGE/lib/" -cp "/spd/iu/system/util/te-con/te-con-1.2.0/lib/te-con-1.2.0_new.jar:/spd/iu/web/$STAGE/lib/*" com.luz.telco.sandbox.tecon.ConnectionTest builtin/luz-$STAGE-new.properties

$STAGE simply contains the dir name on each server.

Maybe it's me, but are you sure about the first line of your script ?
#!/bin/ssh shouldn't it be something else like #!/bin/sh ?

If changing the first line of the script doesn't fix the problem, it might be that there is a malformed if statement in /spd/iu/system/util/te-con/te-con-1.2.0/bin/tecon.conf . Please show us the contents of this file. (Note that the echo before you dot this file succeeds and the echo after you dot this file did not run.)

The ssh was a typo here, in the script it's simply sh

The tecon.conf contains only one variable as of now:
$LIBPATH="/spd/iu/system/util/te-con/te-con-1.2.0/lib/"

Presumably, that should be:

LIBPATH="/spd/iu/system/util/te-con/te-con-1.2.0/lib/"

not:

$LIBPATH="/spd/iu/system/util/te-con/te-con-1.2.0/lib/"

Yeah, my bad. It's without the $