NULL checking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NULL checking
# 1  
Old 10-11-2011
NULL checking

Hi I need to check the output of a string to null. For eg the output I get for $KIT is empty. So want to echo something when the output is empty.
How can I do that?


Thanks in advance
# 2  
Old 10-11-2011
Code:
if [ "X$KIT" =="X" ] ; then
   echo Kit is undefined
fi

# 3  
Old 10-11-2011
Or:

Code:
[ -z "$KIT" ] && your code when empty

# 4  
Old 10-11-2011
Just [ "$KIT" ] should also work I guess!?
# 5  
Old 10-11-2011
Quote:
Originally Posted by anchal_khare
Just [ "$KIT" ] should also work I guess!?
Not if kit=0
# 6  
Old 10-11-2011
Yeah. But in that case its not "null".
# 7  
Old 10-11-2011
Yep,
with most modern shells.

It may not work with the old Bourne shell in some corner cases:

Code:
bash-2.03$ uname -sr
SunOS 5.8
bash-2.03$ sh -c 'v=-d; [ "$v" ] && echo ok'
sh: test: argument expected
bash-2.03$ bash -c 'v=-d; [ "$v" ] && echo ok'
ok
bash-2.03$ ksh -c 'v=-d; [ "$v" ] && echo ok'
ok

See bit.ly/pjR2c6 for more:
Code:
The two commands:

  test "$1" 
  test ! "$1"    

could not be used reliably on some historical systems. Unexpected results would occur if such a string expression 
were used and $1 expanded to '!', '(', or a known unary primary. Better constructs are:

  test -n "$1"
  test -z "$1"

This User Gave Thanks to radoulov For This Post:
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. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

3. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

4. Programming

Checking an array for NULL in C++

Hello All, I have this question that how to check for an array for NULL? For eg. #include<iostream.h> using namespace std; int main() { char arr; if(arr == NULL) { cout<<"NULL"; } } Thanks a lott in advance!!! (3 Replies)
Discussion started by: mind@work
3 Replies

5. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

6. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

7. Programming

C++ segmentation fault while checking for null pointer

void disptree(node *ptr) { if ((ptr->left) !=NULL) disptree(ptr->left); cout<<"Position:"<<ptr->pos<<" Data:"<<ptr->data<<endl; if ((ptr->right)!=NULL; disptree(ptr->right); } i'm getting a segmentation fault at the red line. i cannot understand what's the problem.... (3 Replies)
Discussion started by: vijaymrt
3 Replies

8. Shell Programming and Scripting

Checking for null value

Dear All, I'm glad that I've also joined in this forum. Good work fellas... :b: Keep going...! :) I am having a .csv file where I am reading the values with comma(,) as delimiter. After reading each and every values, I would like to check whether it is empty (say null or blank spaces). How... (4 Replies)
Discussion started by: ganapathi.t
4 Replies

9. Shell Programming and Scripting

compare null with non-null

I've got a very peculiar situation. I'm trying to find out if we can compare null fields with non-null. I've output csv files from SQL and Oracle. I need to compare each field from the files, and then find out any differences. The files usualy have over 500 fields, and send the resule to DBA.... (8 Replies)
Discussion started by: nitin
8 Replies

10. UNIX for Advanced & Expert Users

Null Value

Hello All, I have a file of the format **** 123 abc ABC 456 bcd BCD 789 def 112 ghi GHI 223 jkl 344 mno MNO **** I am trying to extract the lines that have no values in the third field (in this case, this would be 789 def 223 jkl Can anyone please help??... (1 Reply)
Discussion started by: Khoomfire
1 Replies
Login or Register to Ask a Question