Find whether the variable holds value or not

when i run a select query in database, it will return the objid based on the condtion. If there is no objid tht meets the condition, I will not get any value returned and the variable will hold nothing... so now how to chk tht the variable is blank or not. I tried the below piece of code:

if [ -z $objid ]; then
echo "No objid returned"
else
<do this>
.
.
.
fi

but the above code throws the following error:
test: argument expected.

Or else please let me know how to check whether the variable holds the numerical or not, this will also serve my purpose.

Please help :frowning:

Thanks,
Harish

That is supposed to work.

Try this.

Replace

if [ -z $objid ]; then

with

if [ x${objid} = x ] ; then

Another way :

if [ -z "$objid" ]; then

Jean-Pierre.

Not sure about your shell, but...

The form "[ ]" is essentially calling the external "test" command, and thus everything is evaluated by the shell first. That's why you get "test: argument expected" when $objid is NULL, since the expression evaluates to "[ -z ]".

When using "[ ]", you really need to quote all values/variables. But, you can use the super-secret double bracket:

if [[ -z $myvar ]]; then.....

WIth the double-brackets, the test is a shell builtin. So nothing needs to be quoted, since it is not evaluated prior to being tested. Plus, you will save dozens of milliseconds by avoiding an external program. :slight_smile:

The [[ is a korn shell special.

Oh. Isn't everybody using ksh? :slight_smile:

It does work with other ksh-ish shells (zsh, bash, etc.)

Thanks all... its working!!!!! :smiley:

The quotes/secret backets solved my issue... Thanks gus2000 for explaining the concept