Linux/Unix Scripting - strangest behaviour ever in a few lines - variable set but empty

229 views Asked by At

I can tell you this is the craziest thing I have seen in a long time.

I have this (part of) sh script running on CentOS 5.4:

# Check GOLD_DIR`
echo $GOLD_DIR"<--"
#export GOLD_DIR=/share/apps/GOLD_Suite/GOLD <uncommenting this line works!!
if [ "X$GOLD_DIR" =  "X" ] ; then
  echo "ERROR: GOLD_DIR is (probably) not set on host ${HostName}" >> ${3}
  exit 1
fi

And this gives the following output:

/share/apps/GOLD_Suite/GOLD<-- 
Waiting for 5 seconds ..... Testing output

The test script did spawn a job (i.e. PVM ran OK), 
but errors were detected in the test script output 
on the host machine: Below is the output

ERROR: GOLD_DIR is (probably) not set on host xxx.yyy.local

As you can see the GOLD_DIR variable is set (the script finds it as shown by the output with postfixed "<--") ! If I uncomment the export of the GOLD_DIR variable in the script code (first snippet) everything works.

EDIT: GOLD_DIR is exported in /etc/profile (using export GOLD_DIR=/share/apps/GOLD_Suite/GOLD)

Any ideas why?

Note1: I don't know if this is important but this is a spawn script on PVM.

Note2: The script is written in sh #!/bin/sh but I am using bash...

Edit3: I GOT IT TO WORK BUT I DONT KNOW WHY! - Ok so what I did was rename the hostname (with sudo hostname abc) to the name of the machine I ssh into (e.g. abc). Before the PVM was listing the full name of the machine abc.mycompany.local. Note that both abc.mycompany.local and abc are the same machine.

2

There are 2 answers

3
AudioBubble On

So the var is set. If you just do export GOLD_DIR instead of commented line (without setting the value), will it work?

Also. It's an isolated case? Is it bash there on CentOS? Try to use [[ ]] to check what's working wrong.

0
davidr On

I believe that it might have something to do with the non-interactive nature of the job. Jobs run from shell scripts won't necessarily source /etc/profile, so they might not be picking up your ${GOLD_DIR} variable. (Unless you've explicitly changed its behavior, bash will only source /etc/profile for a login shell.)

Try adding:

. /etc/profile

in the beginning of your script just to see if that changes anything. If not, when you echo the error statement, add in ${GOLD_DIR} somewhere to see if the variable is still available in that statement.