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

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?

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

Again, read the man bash :

1 Like

Thanks zzav for the reply.But Rudi solution seems more simpler than you .:slight_smile:
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.