Checking for null condition in a UNIX variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking for null condition in a UNIX variable
# 1  
Old 07-01-2013
Checking for null condition in a UNIX variable

i have this code
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.

Code:
for i in `cat sql_output.txt`
If i is null or empty
then 
echo "no ids to execute"
else
do
-- some script commands
done


i should be checked for null condition only once..at the very beginning if is not null then it for loop should be executed
# 2  
Old 07-01-2013
What shell do you use? Read its man page!

You will find sth like [ ! -s sql_output.txt ] will do what you need.
# 3  
Old 07-01-2013
Quote:
Originally Posted by RudiC
What shell do you use? Read its man page!

You will find sth like [ ! -s sql_output.txt ] will do what you need.
i am using bash.
-s checks if file is emty or not?
# 4  
Old 07-01-2013
Code:
cat sql_output.txt|while read line
do
  typeset -i charCount=`echo $line|wc -c`
  if [[ $charCount -eq 1 ]]; then
    echo "no ids to execute"
  else
    echo "some scipts command"
  fi
done


Last edited by Scott; 07-01-2013 at 07:27 AM.. Reason: Please use code tags
# 5  
Old 07-01-2013
Quote:
Originally Posted by rafa_fed2
i am using bash.
-s checks if file is emty or not?
Again, read the man bash:
Quote:
-s file
True if file exists and has a size greater than zero.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 07-01-2013
Quote:
Originally Posted by zzavilz
Code:
cat sql_output.txt|while read line
do
  typeset -i charCount=`echo $line|wc -c`
  if [[ $charCount -eq 1 ]]; then
    echo "no ids to execute"
  else
    echo "some scipts command"
  fi
done

Thanks zzav for the reply.But Rudi solution seems more simpler than you .Smilie
I implemented that

what does while read line and typeset do in your code.

i think your code will be useful where there are some lines prenet along with ids.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition to check null variable

Guys, Please help me on the below sample.cfg var=NULL sample.sh #!/bin/sh . /sample.cfg if ;then 1 st command here else 2 nd command here fi (3 Replies)
Discussion started by: AraR87
3 Replies

2. Shell Programming and Scripting

Condition checking in UNIX

i have a script where i have to find the age of a file, if then echo "dnb file is present for the monthly load" >> $RUNLOG dnb="1" else echo "dnb file has not arrived yet" > $ERRLOG dnb="0" fi i know the file is available so... (3 Replies)
Discussion started by: lovelysethii
3 Replies

3. Shell Programming and Scripting

Checking the variable in UNIX

Hi, I have a file abc.txt as ABC,TYU,1.2566 AHG,GJJ,1.898 hgh,FGA,1.854 My program is reading each line and storing the values variables base_cy, quo_cy, ra_amt Need to validate each of them as in: base_cy and quo_cy should be a 3 character alphabet among A-Z, if it is lower case ... (1 Reply)
Discussion started by: infyanurag
1 Replies

4. Shell Programming and Scripting

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 (6 Replies)
Discussion started by: Ananthdoss
6 Replies

5. 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

6. 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

7. 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

8. 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

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

Condition checking

Dear all That's another problem from me, i wanna do a lot of if statement checking for correct input by user, will be prompt input again if the input not meet the requirement defined by If or while statement... like this one .... while I know it's less effiency write the program... (14 Replies)
Discussion started by: trynew
14 Replies
Login or Register to Ask a Question