Link to home
Start Free TrialLog in
Avatar of Stanley Lai
Stanley LaiFlag for Hong Kong

asked on

parameter in sh


I have 2 sh.

1. env.sh with the following content to define global variables for other script.

if [ $1 = "PROD" ]
then
      ENV=PROD; export ENV   # JCHOW 07/21/2005
fi


2. then it was called by calling.sh like this ...

# Load the environment and functions
[ -f ./env.sh ] && . ./env.sh . "DEV"

echo $ENV


3. when i run calling.sh, i got errors like
./calling.sh: test: argument expected.
how to fix it?
Avatar of ozo
ozo
Flag of United States of America image

is

# Load the environment and functions
[ -f ./env.sh ] && . ./env.sh . "DEV"

echo $ENV

exactly what calling.sh looks like?
Avatar of Stanley Lai

ASKER



calling.sh looks like this

#!/bin/sh
# Load the environment and functions
[ -f ./envrpl.sh  ] && . ./envrpl.sh . "DEV"
echo $ENV
exit

I modified calling.sh like this, i got no error but i got nothing with $ENV. I expect $ENV gives me PROD.

#!/bin/sh
# Load the environment and functions
[ -f ./env.sh  ] && ./env.sh DEV
echo $ENV
exit
Avatar of yuzh
yuzh

Modify your script to make it looks like:

#!/bin/sh

if [ -f /path-to/envrpl.sh  ]  ; then
   . /path-to/envrpl.sh  "DEV"
   # a DOT space then script + arg
   echo $ENV
fi
exit

#Use full path to the script to make it more portable.
[ -f ./env.sh ] && . ./env.sh  "PROD"
echo $ENV

no luck.

If i dont have $1 defined inside $envrpl.sh. $ENV echoed as expected with . /path-to/envrpl.sh  

However if i have $1,
 . /path-to/envrpl.sh  "DEV" gives me error as below
test: argument expected
What else is in envrpl.sh besides
if [ $1 = "PROD" ]
then
     ENV=PROD; export ENV   # JCHOW 07/21/2005
fi
really just that.

have you ever tried calling . /path-to/envrpl.sh  "DEV"

It doesnt take parameter like this with a dot in front.

BTW, what does a dot in front mean?
if envrpl.sh contains

if [ $1 = "PROD" ]
then
     ENV=PROD; export ENV   # JCHOW 07/21/2005
fi

then

. /path-to/envrpl.sh  "PROD"

will set ENV

. /path-to/envrpl.sh  "DEV"

will not

and

. /path-to/envrpl.sh

will give an error unless $1 is set




>>BTW, what does a dot in front mean?

It mean "source", since your script is setting up ENV vars.

man source
to learn more details

PS: csh/tcsh use "source", for sh/ksh/bash use .   a DOT+  space then scriptname


ozo,
. /path-to/envrpl.sh  "PROD"

will NOT set ENV and output with error parameter expected.

please try run it in unix and you will know what i am talking about.
ozo and yuzh??????please help!!!
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
passing parameters to sourced scripts is unreliable, it depends on OS and/or shell
you better don't use it
The only reliable way to pass values to a sourced script is using shell and/or environment variables itself.
I slighly disagree 'cause passing arguments to sourced scripts is no standard and depends on the used shell, see http:/Q_21520411.html also.

No need to change the grading, but future readers should take care ...