In this article we will see how to validate/check whether a variable is NULL or empty. Let see with the help of examples.Lets say we have a variable V_Error_Msg as given below; I will explain all the solution on this variable.
V_Error_Msg="ORA-00942"
Using ==/= Operator
if [ ${V_Error_Msg} = "" ];
then
echo "V_Error_Msg is Empty/NULL"
else
echo "V_Error_Msg is Not Empty/NULL"
fi
[OR]
if [ ${V_Error_Msg} == "" ];
then
echo "V_Error_Msg is Empty/NULL"
else
echo "V_Error_Msg is Not Empty/NULL"
fi
Output: Both will produce same output in some shells equal(=) to operator do the needful and in some double equal(==) do the needful.
V_Error_Msg is Not Empty/NULL
Using ! Operator
if [ ! "${V_Error_Msg}" ];
then
echo "V_Error_Msg is Empty/NULL"
else
echo "V_Error_Msg is Not Empty/NULL"
fi
Output:
V_Error_Msg is Not Empty/NULL
Explanation: If V_Error_Msg holds any value then if return true, means variable is not empty and if V_Error_Msg is NULL then if block return false. Here variable V_Error_Msg is not empty hence it has returned TRUE and ! of TRUE is FALSE hence "V_Error_Msg is Not Empty/NULL" has been printed on terminal.
Using -z Switch
$ [ -z "${V_Error_Msg}" ] && echo "V_Error_Msg is Empty/NULL" || echo "V_Error_Msg is Not Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is empty then(&&) TRUE else(||) FALSE
$ [ ! -z "${V_Error_Msg}" ] && echo "V_Error_Msg is Not Empty/NULL" || echo "V_Error_Msg is Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is Not empty then(&&) TRUE else(||) FALSE
Using -n Switch
$ [ ! -n "${V_Error_Msg}" ] && echo "V_Error_Msg is Empty/NULL" || echo "V_Error_Msg is Not Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is empty then(&&) TRUE else(||) FALSE
$ [ -n "${V_Error_Msg}" ] && echo "V_Error_Msg is Not Empty/NULL" || echo "V_Error_Msg is Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is empty then(&&) TRUE else(||) FALSE
Note: Please observe carefully, there is a space between operator and operand. If we will not provide the space it will through an error
sh: test: argument expected
Keep Learning, Keep Practising, keep Sharing.
V_Error_Msg="ORA-00942"
Using ==/= Operator
if [ ${V_Error_Msg} = "" ];
then
echo "V_Error_Msg is Empty/NULL"
else
echo "V_Error_Msg is Not Empty/NULL"
fi
[OR]
if [ ${V_Error_Msg} == "" ];
then
echo "V_Error_Msg is Empty/NULL"
else
echo "V_Error_Msg is Not Empty/NULL"
fi
Output: Both will produce same output in some shells equal(=) to operator do the needful and in some double equal(==) do the needful.
V_Error_Msg is Not Empty/NULL
Using ! Operator
if [ ! "${V_Error_Msg}" ];
then
echo "V_Error_Msg is Empty/NULL"
else
echo "V_Error_Msg is Not Empty/NULL"
fi
Output:
V_Error_Msg is Not Empty/NULL
Explanation: If V_Error_Msg holds any value then if return true, means variable is not empty and if V_Error_Msg is NULL then if block return false. Here variable V_Error_Msg is not empty hence it has returned TRUE and ! of TRUE is FALSE hence "V_Error_Msg is Not Empty/NULL" has been printed on terminal.
Using -z Switch
$ [ -z "${V_Error_Msg}" ] && echo "V_Error_Msg is Empty/NULL" || echo "V_Error_Msg is Not Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is empty then(&&) TRUE else(||) FALSE
$ [ ! -z "${V_Error_Msg}" ] && echo "V_Error_Msg is Not Empty/NULL" || echo "V_Error_Msg is Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is Not empty then(&&) TRUE else(||) FALSE
Using -n Switch
$ [ ! -n "${V_Error_Msg}" ] && echo "V_Error_Msg is Empty/NULL" || echo "V_Error_Msg is Not Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is empty then(&&) TRUE else(||) FALSE
$ [ -n "${V_Error_Msg}" ] && echo "V_Error_Msg is Not Empty/NULL" || echo "V_Error_Msg is Empty/NULL"
Output:
V_Error_Msg is Empty/NULL
Explanation: Syntax reads, if V_Error_Msg is empty then(&&) TRUE else(||) FALSE
Note: Please observe carefully, there is a space between operator and operand. If we will not provide the space it will through an error
sh: test: argument expected
Keep Learning, Keep Practising, keep Sharing.
No comments:
Post a Comment