if [ -z "${VAR_1}" ]
then
echo "is empty"
fi
echo "done"
The following is the result when i run the script from command without and with a dot and space operator
$ setenv.sh
is empty
done
$ . setenv.sh
sh: VAR_1: Parameter not set.
$
It's our standard to run with dot and space, and ever since i added code to check if the variable is empty it throws out this error and doesn't run the next lines
The only time you would run a script in this manner is if you run the script from another script and want it to run in the same shell as the parent. Typically this is for when you want to set parameters in the parent script.
If you actually run your example script from a parent script it will work. It will only fail like this if you run the script from the command line.
The normal way to run a script from the command line is:
./scriptname
In the commercial world you would add the operational scripts directory to $PATH and call the script by name or always use the absolute path to call the script.
Thanks Methyl
We run jobs through autosys scheduler and it first runs the setenv.sh file (to set the variables) and then runs the actual job
The developers also run the setenv.sh file from command line with dot and space and then run their other scripts that use variables set by the setenv.sh
If they don't use the dot and space the variables are not exported to the session
and in the updated setenv.sh, it terminates when checking for this new variable
set +u
if [ -z "${VAR_1}" ]
then
echo "is empty"
fi
echo "done"
This would work but may not conform to local standards because any subsequent undefined variables would not error.
Its the same effect as starting the Shell as:
dot-space is the only way this kind of script can be expected to work.
If you run it as a shell script, it will create a whole new shell for it. This shell is independent. It will set all the right variables, in its own shell, then -- leaving your original shell unmodified.
If you run it with dot-space, that instructions your own shell to run this code, rather than a new one.