Compare the output of find command

Hi All,

I am trying to run find command in a script to list out certain files based on a patter. However, when there is no file in the output, the script should exit.

Tried a couple of operators (-n, -z) etc but the script does not work.

I am confused whether a null string is returned or something else since it seems null string operators are not working.

Environment: SunOS 5.10

Can someone please guide me.

a=$(find . -name '*.lis')
if [ ! -z "$a" ] ; then  
 for fname in $a    # have some files
  do
  echo $fname
 done
else
  echo 'no files found'
fi

Start with that. Without seeing your code, make sure any [ or ] character (if condition ) is surrounded by spaces. This will work in /bin/sh the POSIX shell. More Questions? please post your code - this was a guess.

1 Like

Thanks for the reply Jim,

Your code works absolutely fine but mine doesn't.

Can you tell me where is the mistake. I am not able to figure out.

There is no file danish.lis in the directory

#!/bin/bash

for i in `find . -name "danish.lis"`
do
if [ -z "$i" ]
then
echo "File not found"
else
echo "file found"
fi
done

gives no output..

Your code prints no files found

The find command doesn't return anything, so the variable i cannot assume any value and the do ... done branch is not executed.

1 Like