Using env variables to run a program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using env variables to run a program
# 1  
Old 02-11-2014
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.
Code:
#!/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:

Quote:
/spd/iu/web/kon/lib/
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).
# 2  
Old 02-11-2014
You almost certainly will need to have arcjob-3-4-1.jar on each of the servers
# 3  
Old 02-11-2014
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:

Code:
#!/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 $@

# 4  
Old 02-11-2014
There is an obvious typo:
Code:
echo "CLASSPATH = ${CLASSPAT}"

should be:
Code:
echo "CLASSPATH = ${CLASSPATH}"

but fixing that won't change the behavior of the script; it will just make the status message more meaningful.
# 5  
Old 02-11-2014
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.
# 6  
Old 02-11-2014
Well, what's different on your other servers?
# 7  
Old 02-12-2014
/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:

Code:
# 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Setting ENV variables in PERL

I have perl script and in the first line we are invoking .sh script to set ENV variables. e..g eval '. $envfile; exec $PERL -S $0 "$@"' I want to change some of the env variables while the program is running and I am settging it like this .. $ENV{ORACLE_HOME}=trim($oraclehome);... (1 Reply)
Discussion started by: talashil
1 Replies

2. Shell Programming and Scripting

Env variables in script

Hi All, I have script and it's hardcoded the script ca invoke in user home dir and logs will be redirected to home dir of user. how to make the same script will be invoke from /usr/bin with out chg the logs and other functions path from /user/homedir . code is below: pls check how to... (1 Reply)
Discussion started by: saku
1 Replies

3. Red Hat

how to use env variables within ed

i have a file that i need to edit and replace a single value with another. so i have two variables, $oldvalue and $newvalue but below doesn't work: ed file.txt << EOF ,s/$oldversion/$newversion/g wq EOFi presume it's the $ that is the issue since it's actually special to ed. any suggestions?... (1 Reply)
Discussion started by: crimso
1 Replies

4. Solaris

env variables again

What is the difference between ${variable} and $variable when used in a script? (2 Replies)
Discussion started by: orange47
2 Replies

5. UNIX for Dummies Questions & Answers

How to update env variables.

I am newbie on Unix system and seek help for updating env variables. The condition is like this: On Unix server, I log in as oracle user (this is the super for database on Unix), I type > env all envirnment variables show up. I saw one variable DBA_LIST contains a few email addreses. I need... (2 Replies)
Discussion started by: duke0001
2 Replies

6. UNIX for Dummies Questions & Answers

set env variables in a new xterm

Hi, I have a script that sets some env variables. I want to source the script in a new xterm and after the script execution is over, the xterm has to be alive with the env variables set according to the script. I tried xterm -e "source ./myscript;tcsh" & The variables are getting set... (3 Replies)
Discussion started by: chaitubek
3 Replies

7. Shell Programming and Scripting

Getting the value of env variables

Hi, I want to get the value of the env varables using the ksh script. All the env variables are stored in a file. Eg. file1 $INPATH $OUTPATH myscirpt: for name in `awk { print $1 } file1` do cd $name done i'm getting the error like $INPATH not found. in the same script... (1 Reply)
Discussion started by: vij_krr
1 Replies

8. UNIX for Dummies Questions & Answers

Env variables not getting updated from the bashprofile

Hi, I am using the HP-UX machine of version B.11.23. My bashprofile looks like this : # @(#)B11.23_LR # Default user .profile file (/usr/bin/sh initialization). #! /usr/bin/bash # Set up the terminal: if then eval ` tset -s -Q -m ':?hp' ` else eval ` tset -s -Q ` fi stty erase "^H"... (0 Replies)
Discussion started by: swethuanju
0 Replies

9. UNIX for Advanced & Expert Users

Ksh - Env. Variables ??

Hey all, I have been using Ksh and in that I am setting Environment variables. To set Env. Variables I have created my own file "BuildScript.sh" in which i have written : export CLASSPATH=/somedir/some other dir/file:. export PATH=/some dir/file:. But when i am calling this... (4 Replies)
Discussion started by: varungupta
4 Replies

10. Shell Programming and Scripting

export env variables

hi i want to write a shell script to set environment variables . But i am not been able to set that for the current shell instead i have to spawn a new shell. Is there a way to set the env variable for the current shell using shell script in bash shell ? Thnx (2 Replies)
Discussion started by: varun.81
2 Replies
Login or Register to Ask a Question