Problem while concatenating variables in shell script

Hi folks,

I am facing problem when I concat variables with the string.

Value for 'JDBC_CLASSES' variable looks malformed (/classes12.zip2.0KAGES)

But, my expected result for 'JDBC_CLASSES' is
/opt/API-R111/PACKAGES/jdbc/ORACLE9.2.0/classes12.zip

Am I missing anything here?

My Script :

#!/bin/sh
echo "== SET ENVIRONMENT VARIABLES =="
export PACK_HOME=/opt/API-R111/PACKAGES
export JDBC_VER=ORACLE9.2.0
export JDBC_CLASSES=${PACK_HOME}/"jdbc/"${JDBC_VER}/"classes12.zip"
echo $PACK_HOME
echo $JDBC_CLASSES
 

jmc: added code tags
Output:

[weblogic@localhost admin]$ sh setenv.sh
== SET ENVIRONMENT VARIABLES ==
/opt/API-R111/PACKAGES
/classes12.zip2.0KAGES

Any help on this is highly appreciable.

Regards,
Adhil

First off you realize you have to source this, not run it, right? You do that by running . filename in your shell. Actually executing it will be pointless because it will get its own, separate copy of the environment variables to fiddle with, leaving yours alone.

So remove the #! line, it won't do anything.

Next, properly quote everything.

Next, split the export and = statements since some shells throw up when you combine them.

How does this work?

echo "== SET ENVIRONMENT VARIABLES =="
PACK_HOME="/opt/API-R111/PACKAGES"
export PACK_HOME
JDBC_VER="ORACLE9.2.0"
export JDBC_VER
JDBC_CLASSES="${PACK_HOME}/jdbc/${JDBC_VER}/classes12.zip"
export JDBC_CLASSES
echo "${PACK_HOME}"
echo "${JDBC_CLASSES}"
1 Like

Maybe the script has been edited with a Microsoft editor and the strings contain spurious carriage-return characters.

Please post the output from this command which is designed to make line terminators visible:

sed -n l setenv.sh

When viewed like this a normal unix text file will have a "$" at the end of each line and no other control characters.

1 Like

Try splitting your export command and variable setup in different lines, what do you get with this :

echo "== SET ENVIRONMENT VARIABLES =="

PACK_HOME=/opt/API-R111/PACKAGES
export PACK_HOME

JDBC_VER=ORACLE9.2.0
export JDBC_VER

JDBC_CLASSES="${PACK_HOME}/jdbc/${JDBC_VER}/classes12.zip"
export JDBC_CLASSES

echo $PACK_HOME
echo $JDBC_CLASSES

???

env | egrep -e 'JDBC|PACK'

Changing ctsgnb to Adhil:
Adhril, please follow the instructions provided by Corona688:

@Shell Life ,

Oooops, i just realized that Corona already suggested to split the export/assignation into 2 lines.

But in fact i don't have any problem, it is not me who is asking questions about how to source environment variable : it is Adhil

:wink:

ctsgnb, I am really sorry for the confusion. :confused:
I do apologize.
:slight_smile:

Thanks Folks.
You are correct. I copied the script from windows and edited in text editor.
I used Emacs and removed all ^M. Now, it is working fine.

Actually, I am trying to set all environment variables in one script (env.sh)
and re-use the variables in appln.sh (calling script).

Does Unix really supports this? If so, how can I achieve this?

appln.sh
--------
#! /bin /sh
sh ./env.sh

# This prints nothing
echo ${MY_VAR}

env.sh
---------
export MY_VAR="/opt/bea"
echo ${MY_VAR}

I'd like to re-use 'MY_VAR' in appln.sh. I don't want to add MY_VAR in /etc/profile.

Any help on this is highly appreciable

Thanks,
Adhil

UNIX really supports this but you're doing it wrong. You're also putting extra spaces and punctuation marks all over the place -- if your script was as you posted it it'd be complaining about syntax errors and files not found.

#!/bin/sh
# Don't put extra spaces in the #!/bin/sh

# This does nothing, because test2.sh will run in its own separate process
./test2.sh
echo "${MY_VAR}"

# My best guess at what you actually did, which also does nothing for the same reason.
sh ./test2.sh
echo "${MY_VAR}"

# This will run it in the same shell.  Note the dot and the space.
. test2.sh
echo "${MY_VAR}"
# Some shells demand that the = and the export happen separately.
MY_VAR="/opt/bea"
export MY_VAR

Folks,

I got it fixed by changing appln.sh

from
sh ./env.sh
to
. env.sh

What is the difference between these two?

Thanks,
-Adhil

---------- Post updated at 04:05 PM ---------- Previous update was at 04:00 PM ----------

Thanks a lot, Corona.

Sorry I edited my previous post before reading your reply. Could create confusion to the readers. My apologize.

Thanks,
Adhil

I forgive you. :wink: I manage to do that to people with fair regularity