Dot operator and space, Parameter not set


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dot operator and space, Parameter not set
# 1  
Old 02-24-2012
Dot operator and space, Parameter not set

Hi, i have this script
setenv.sh
Code:
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

Thanks,
-srinivas yelamanchili
# 2  
Old 02-24-2012
Quote:
It's our standard to run with dot and space
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:
Code:
./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.
# 3  
Old 02-24-2012
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
# 4  
Old 02-24-2012
Code:
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:
Code:
sh +u

# 5  
Old 02-24-2012
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.
# 6  
Old 02-24-2012
I resolved this by using ':-' after the variable name
Code:
if [ -z "${VAR_1:-}" ]
then
   echo "is empty"
fi 
echo "done"

This causes the shell not to exit if the variable is not set or null

Thanks,
-srinivas
This User Gave Thanks to ysrini For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can't set parameter to lynx properly

My intention is to go through list of addresses and call google geocode api for each of them. I am using lynx for this, but somehow I can't supply the parameters to it in a proper way. To show that my parameters are OK I just hardcoded one address in my script and put it in my input file, and... (2 Replies)
Discussion started by: migurus
2 Replies

2. Shell Programming and Scripting

Awk/sed - add space between dot and letter

I need to change . into . so that e.g. A.Jbecomes A. JI have tried sed 's/\./\.\ /g' but that didn't work. (9 Replies)
Discussion started by: locoroco
9 Replies

3. Programming

Problem with STL's std::set container parameter to the operator << ()

Hi, I have this following code which gives me error when compiling. The problem is happening at the point where I create a const_iterator inside the overloaded insertion operator (i.e) operator << () function. The template argument version of set is not correct I guess. Could anyone please pitch... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Shell Programming and Scripting

nawk and space in the parameter

Hi, Could you please tell me how nawk command works when there is a asterisk <*> or space with asterisk < *> or <* > in the parameter. I am just trying to read line by line and fetch fourth parameter separated by delimiter (|). But if there is a * or < *> or <* > in the fourth parameter it... (7 Replies)
Discussion started by: nram_krishna@ya
7 Replies

5. SCO

Parameter passing to dot shell script

OS SCO Open Server 6.0 MP4 I am trying to change the value of a enviornment variable thru a script and want to pass a parameter on the commande line, If I hard code the value inside the script the script changes the enviornment variable . mytest where my test is MYVAR=$1 export MYVAR... (6 Replies)
Discussion started by: atish0
6 Replies

6. Shell Programming and Scripting

Set command ending with a "." (dot)

Hi I have a "set" command which ends with a "." (dot), for example: set `grep "\<${pnum}\>" /tstmp/data.txt |sed 's/#//'` . Can somebody help me to understand the purpose of this "set" and "." combination? The problem is that this command does not produce the same result when run on AIX... (2 Replies)
Discussion started by: aoussenko
2 Replies

7. Shell Programming and Scripting

remove space between the operator, script required...

Hi... i need a script to remove the space before and after the operator like( +, -, ||, &&). Ex : Input file apple + manago mango && fresh + apple fresh || fruit Desired output: apple+manago mango&&fresh+apple fresh||fruit (6 Replies)
Discussion started by: vasanth_vadalur
6 Replies

8. UNIX and Linux Applications

set mysql password with host parameter

hi, linux gurus... i'm trying to write a script in ksh called ResetPass that allows a user to change mysql passwords. the script accepts user, password and host like this: ResetPass <user> <password> <host>. here's the code: ***************************************************** mysql... (1 Reply)
Discussion started by: ankimo
1 Replies

9. Shell Programming and Scripting

ksh: dbfFILe: parameter not set

Having the following message returned: FIND REDLOG FILES..... ksh: dbfFILe: parameter not set When I attempt to perform the script below....#!/bin/ksh . $HOME/.profile # Initial Script Prerequisites ORACLE_SID=MDirect ; export ORACLE_SID REDOLOGDIR=$ARCLOGDEST ; export REDOLOGDIR... (2 Replies)
Discussion started by: Cameron
2 Replies

10. UNIX for Dummies Questions & Answers

DEBUG_PROG and opts: parameter not set

I am using a Sun Ultra 30 with 250MG of RAM and 9GIG of hard drive. I connect to the machine via Exceed 6 and have the Oracle8i for Solaris in the cdrom drive. I have read and complied with Oracle's instruction in preparation to installing Oracle. Since I don't have enough space to cp the content... (1 Reply)
Discussion started by: Alexxx14
1 Replies
Login or Register to Ask a Question