Wednesday, 23 December 2015

UNIX NVL Functionality

In this article we will see, how can we implement oracle NVL like functionality in UNIX.
If Unix/Linux variable is not null/empty, display that value else display an user defined value.
Let see how can we implement and use it in real time.


Scenario:
We have a metadata file which has configuration entries. the content of the file are shown below.


$ cat F_Execution_Meta_Data
EXECUTION_MODE=FULL


#!bin/sh
V_Error_Code_Tmp=`grep -w "EXECUTION_MODE" F_Execution_Meta_Data | cut -d"=" -f2`
if [ ${V_Error_Code}=="FULL" ]
then
  #--* Perform your Operation, If condition is TRUE;
else
  #--* Perform your Operation, If condition is FALSE;
fi


Above will work perfectly fine till we have value available in Meta Data, if no value specified [EXECUTION_MODE=] then grep will return NULL/empty which will result  into If condition failure. We will get below error:
Error: Null_Check.sh[2]: test: argument expected

How to handle such scenario or what should be the basic standard which we should incorporate in our code. Lets have a look multiple solutions.

Solution 1: Using -n switch

#!bin/sh
V_Error_Code=`grep -w "EXECUTION_MODE" F_Execution_Meta_Data | cut -d"=" -f2`
if [ ! -n "${V_Error_Code}" ]
then
 V_Error_Code="Z"
fi
if [ ${V_Error_Code}=="FULL" ]
then
  #--* perform your Operation, If condition is TRUE;
else
  #--* perform your Operation, If condition is FALSE;
fi


-n the length of STRING is NonZero
! -n the length of STRING is Zero


instead of writing complete if block, you can write the same in a single line.
$ [ ! -n "$V_Error_Code" ] && V_Error_Code="Z"


Explanation: Now if grep returns NULL, first if block will catch it and assign a value to the variable V_Error_Code which will then be used in second if block. It will makes sure your code would not fail due to test: argument expected error.

Solution 2: Using -z switch

#!bin/sh
V_Error_Code=`grep -w "EXECUTION_MODE" F_Execution_Meta_Data | cut -d"=" -f2`
if [  -z "${V_Error_Code}" ]
then
 V_Error_Code="Z"
fi

if [ ${V_Error_Code}=="FULL" ]
then
  #--* perform your Operation, If condition is TRUE;
else
  #--* perform your Operation, If condition is FALSE;
fi


-z the length of STRING is zero
! -z the length of STRING is Nonzero


instead of writing complete if block, you can write the same in a single line.
$ [ -z "$V_Error_Code" ] && V_Error_Code="Z"



Explanation: have same mechanism as in case of first solution, only difference is -z switch which works opposite to -n switch.

Solution 3: Using Parameter substitution ( Simple and best one)

#!bin/sh
V_Error_Code_Tmp=`grep -w "EXECUTION_MODE" F_Execution_Meta_Data | cut -d"=" -f2`
V_Error_Code=${V_Error_Code_Tmp:-Z}
if [ ${V_Error_Code}=="FULL" ]
then
  #--* perform your Operation, If condition is TRUE;
else
  #--* perform your Operation, If condition is FALSE;
fi


Explanation: Observe the syntax clearly, ${V_Error_Code_Tmp:-Z}, it reads, if value of V_Error_Code_Tmp is NULL then assign Z to the variable or if not null then assign the same value to the variable.

Conclusion: We should always use this functionality when ever we are using a variable
in if for some condition test to make sure our if block doesn't get failed. Keep learning,
keep designing & keep the coding standard high..!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...