Tuesday, 12 January 2016

How To Check Whether A Variable Is A Number Or String Type In Unix/Linux

In this article we will see how to check whether a variable is holding a number or a string. We will use all the concepts which we have already discussed. Let see with the help of examples. Please go through blow posts for detailed explanation:

Before discussing the solution, let me once again explain you single liner syntax of if-else without using if-else block .

[[  Operation ]] && "TRUE" || "FALSE"

Explanation:
1. [[ ]] is used to define the condition like we used to do in if.
2. If condition is TRUE than && part will execute.
3. If condition is FALSE than || part will execute.

We can summarise whole as below:

if [[  Condition ]]
then
         "TRUE"
else
         "FALSE"
fi

&& -> If part
|| -> else Part

$ [[ ${V_Count} -eq 1 ]] && echo " True" || echo "False"

Output: It will return True if value of variable V_Count is 1 or else will return false.

Let see the solution now, we have a variable V_Number which is holding a Number 1234 as shown below.

V_Number="1234"

$ [[ ! -z `echo ${V_Number} | grep "^[0-9]*$"` ]] && echo "A Valid Number" || echo "Not a Valid Number"

Output: A Valid Number


1. Regular expression ^[0-9]*$ looking for number.
  ^ matching the expression at the start position.
  [0-9] is class representation, check for a character in between      0- 9.
  * matches the zero or more occurrence of the previous character.
  $ matching the expression at the position. 

2. At 1st position =1 [0-9] (In the range 0 to 9)
   At 2nd position =2 [0-9] (In the range 0 to 9)
   At 3rd position =3 [0-9] (In the range 0 to 9)
   At 4th position =4 [0-9] (In the range 0 to 9)

   ^[0-9]*$ will parse 1234 successfully but will not pass 123rt4.
   
   At 1st position =1 [0-9] (In the range 0 to 9)
   At 2nd position =2 [0-9] (In the range 0 to 9)
   At 3rd position =3 [0-9] (In the range 0 to 9)
   At 4th position =r [0-9] (Not In the range 0 to 9)
   At 5th position =t [0-9] (Not In the range 0 to 9)
   At 6th position =4 [0-9] (In the range 0 to 9)

Remember, * matches the zero or more occurrence of the previous character, here previous character of r is 3 which is number but r does not fall in the range [0-9] hence regular expression has not parsed it. 

3. -z switch check for emptiness, if grep parse the variable (V_Number) successfully it will return the number to -z which is non empty(Output of highlighted part), -z becomes FALSE and negation of (!) -z is true hence executed if (&&) part. You may write the same without negation (!), in that case else (||) case will execute.

$ [[ -z `echo ${V_Number} | grep "^[0-9]*$"` ]] && echo "Not A Valid Number" || echo "A Valid Number"

[OR]

$ [[ -z `echo ${V_Number} | tr -d [:digit:]` ]] && echo "A Valid Number" || echo "Not a Valid Number"

Output:A Valid Number


-d switch of tr command is used to delete the matching pattern. Here it is deleting all the numbers ([:digit:]), so if a variable is holding all the digit, it will delete all and will return NULL/empty. -z switch is used to check whether returned value is NULL(empty) or NOT NULL(non-empty). If it is NULL then execute && block ( A valid Number) or else || block (Not A Valid Number)

[OR]

$ [[ `echo ${V_Number} | tr -d [:digit:] | wc -w` -eq 0 ]] && echo "A Valid Number" || echo "Not a Valid Number"

Output:A Valid Number


Same as above but here instead of using -z switch we are using wc command. wc -w is used to count number of words. If highlighted part is equals to 0 then it will execute && block ( A valid Number) or else || block (Not A Valid Number)

[OR]

$ [[ `echo ${V_Number} | sed 's/^[0-9]*//' | wc -c` -eq 1 ]] && echo "A Valid Number" || echo "Not a Valid Number"

Output:A Valid Number
Here we have used sed command to replace numbers with space. wc -c command is used to count number of character. If highlighted part is equals to 1 (space is also a character) then execute && block ( A valid Number) or else || block (Not A Valid Number)

We can set the value to the variable as well and can use the variable ( as shown below) anywhere in the shell script.

$ [[ ! -z `echo ${V_Number} | grep "^[0-9]*$"` ]] && V_IsNumber="Y" || V_IsNumber="N"

[OR]

$ [[ -z `echo ${V_Number} | tr -d [:digit:]` ]] && V_IsNumber="Y" || V_IsNumber="N"

[OR]

$ [[ `echo ${V_Number} | tr -d [:digit:] | wc -w` -eq 0 ]] && V_IsNumber="Y" || V_IsNumber="N"

[OR]

$ [[ `echo ${V_Number} | sed 's/^[0-9]*//' | wc -c` -eq 1 ]] && V_IsNumber="Y" || V_IsNumber="N"


Output: Y
It is working same as explained earlier, only difference is, here we are not printing the message instead assigning the value to the variable which we can use in the script as per the requirement.

Conclusion: Some time we face situation where we have to pass either string or number to a script or a function. It might possible that mistakenly we provide number in place of string or string in place of number, to avoid such possibilities in the code we can use above method to validate the parameters so that manual mistake could be avoided. 

Keep Reading, Keep Learning, Keep Sharing...!!! 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...