If condition to check null variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition to check null variable
# 1  
Old 07-11-2013
If condition to check null variable

Guys,

Please help me on the below

Code:
 
sample.cfg
var=NULL

Code:
 
sample.sh
#!/bin/sh
. /sample.cfg
if [ $var -eq NULL ];then
1 st command here
else
2 nd command here 
fi

Now i want the first command to be exectuted if the variable is NUll. If i specify the variable name, the second command to be executed.
Please advise

Thanks
# 2  
Old 07-11-2013
put NULL in double quotes.. and if you are equating string better use = than -eq
# 3  
Old 07-11-2013
Code:
#!/bin/sh

. ./sample.cfg

if [ "$var" = "NULL" ]; then
        echo first
else
        echo second
fi

exit 0

# 4  
Old 07-11-2013
Be aware:-
Code:
#!/bin/bash
nulltest()
{
if [ ${#var} -eq 0 ]
then
	echo "NULL, empty string..."
else
	echo "Not an empty string..."
fi
}
var=""
nulltest
var=NULL
nulltest

Gives results:-
Code:
Last login: Thu Jul 11 07:58:36 on ttys000
AMIGA:barrywalker~> ./null.sh
NULL, empty string...
Not an empty string...
AMIGA:barrywalker~>


Last edited by wisecracker; 07-11-2013 at 11:01 AM.. Reason: Missed out the braces...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking for null condition in a UNIX variable

i have this code for i in `cat sql_output.txt` do -- some script commands done sql_output.txt has 1 column with employee_ids If the sql_output.txt is null then the do loop should not execute. How can i implement this. for i in `cat sql_output.txt` If i is null or empty then ... (5 Replies)
Discussion started by: rafa_fed2
5 Replies

2. Shell Programming and Scripting

Check null value in xml

Hi, I have a log file which is having some xml tags. I need to check the value for a particular xml field is null or not and if it is null i have to add current time as the value for that xml field. I tried below code to check whether the word count is 0. But even if the xml field is null it... (16 Replies)
Discussion started by: Neethu
16 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

Check for null

Hi Champs!!! im a newbie in unix, need ur expert help for my problem... I need to search if there are any "NULL" entries in the string String without Null Str1: 203652|1000003653|tellt|RUPV|4649|1|07/28/2011 01:56:12 String with Null (RUPV is removed) Str2:... (5 Replies)
Discussion started by: guruprasad7
5 Replies

5. Shell Programming and Scripting

check for null

hi, i have 3 lines of output , if second line exists then only condition within the if loop has to exeute other wise it has exit from loop. i had tried like this but not getting please help me ... Code: if ; then echo "success" else echo "" Use code tags please,... (8 Replies)
Discussion started by: sreelu
8 Replies

6. Shell Programming and Scripting

csh if loop variable condition check peroblem

Hi, I have variables like var1 var2 var3 var4 in if loop i am trying to check the condition if(variable == "var") then echo $variable endif (0 Replies)
Discussion started by: vasanth.vadalur
0 Replies

7. UNIX for Dummies Questions & Answers

How to compare null and space using single if condition

Hi I have a input file with many fields and each filed will be with in double quotes(""). i want to check fields contains balnk,null or space using condition using if. when i write code as below for if condition its not working a=`awk -F ',' '{gsub("\"", "", $1);'NF==0';printf $1}'... (3 Replies)
Discussion started by: jayakumarrt
3 Replies

8. Shell Programming and Scripting

how to check null variable

korn shell If then update_smartcare_user_password "$u_id" else echo "Not a database user" fi i get this error Syntax error at line *** : `then' is not expected. what should i do. I want to check whether $a is null or not. (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

9. Shell Programming and Scripting

How can find Null value in If condition

Hi, i wrote If Conditions in my script, it's returns null and some values. but i am unable to find when Null value getting. bec we need modification according null vales. pls help me on this. (2 Replies)
Discussion started by: koti_rama
2 Replies

10. Shell Programming and Scripting

check for NULL variable

Hello I want to check for NULL variable.. but this is not working..please help thanks in advance esham (2 Replies)
Discussion started by: esham
2 Replies
Login or Register to Ask a Question