Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test if a variable exist

Status
Not open for further replies.

cantubas

Programmer
Jan 8, 2004
45
FR
i want to know if a variable exist.
i do
if [[ -z $no_pex08 ]]
then
/gv1/gvr/bin/pex08
fi

but if someone do "no_pex08=" that is not ok.
 
Have you tried this ?
if [ -z "$no_pex08" ]; then
echo "Variable not set or empty"
fi

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
i want to know if the variable is set. I don't want to know if the variable is empty.

sory PHV but jour solution is not better than mine.
 
OK, take a look at set -u

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
the only solutions i have found is:
if [[ $(set | cut -d"=" -f1 | grep "^no_pex08$" | wc -w) = 1 ]]
then
echo no_pex08 exist
else
echo no_pex08 don't exist
fi
 
A shorter way:
if [ -n "$(set | awk '/^no_pex08=/')" ]; then
echo no_pex08 exist
else
echo no_pex08 don't exist
fi

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Without using subprocesses nor pipes, the following should allows you to select between the three cases
1) unset,
2) set but null,
3) set and non null.

Code:
case "${no_pex08-un}${no_pex08:-set_and_null}${no_pex08:+set}" in
    unset_and_null) echo "is unset";;
    set_and_null) echo "is set but null";;
    *set) echo "is set and non null";;
esac

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top