variable declaration

how to check

  1. If variable is declared or not
  2. If any value if assigned to variable or not.

in UNIX shell script

read man page on test command

Have a read of your man page of test.

Edit: 116@434 was faster! :smiley:

hi, i have gone thru it.
One of my file contains variable and its value (A to E )
param.ksh
----------

IF [ $1 = "MYDB" ]
then
 export A=123
 export B=345
 C=789
 D=
fi

Now this script i am invoking from other shell scropt

 
. param.ksh

here, i need to check that all 5 (A,B,C,D,E) variables should be declared and assigned values to it, in order to proceed further. If any one of the variable is not declared or value is not assigned to it. process should stop and exit

use an and function

if [ $A="" -a $B="" -a ... etc ]
then
echo "error"
exit error_code
else
do something...
fi

if [ $A="" ... ] checks if value is NULL

not tested with more than 2 variables but it should work

@metal005 Your script as posted contains syntax and logic errors. Needs to be an OR test and all variables should be quoted because we need to look at empty variables.

Maybe you meant:

# Error if parameter not set
set -u
# Error if parameter empty
if [ -z "$A" -o -z "$B" -o -z "$C" -o -z "$D" -o -z "$E" ]
then
        echo "One or more parameters is empty"
        exit
fi
1 Like

ofcourse sorry my bad

it has to an or function...
wassnt thinking clear

your way should work great though... good programming