1825801 Members
2246 Online
109687 Solutions
New Discussion

find return code ???

 
MikeL_4
Super Advisor

find return code ???

I am using find to check if a file is greater than a specific size, and if it is executing ll to pull off some information:

find ${file} -size +${FINDSZ}c -exec ll {} \; | awk '{printf("%7s %9d %-35s\n",$3,$5,$9)}' >> ${FIND}

Based on if it was greater than the size I was looking for will determine what I need to do next in the script, but I'm not sure how to determine this in the script???

Thanks

3 REPLIES 3
curt larson_1
Honored Contributor

Re: find return code ???

using $? you always get the return code of the last command executed. in your case that would be awk. by using the pipe you've lost the return code to the find command.

but, if the file your writing to is zero in size before the find and larger then zero afterwards, the find command found some files.

rm $FIND
find ${file} -size +${FINDSZ}c -exec ll {} \; | awk '{printf("%7s %9d %-35s\n",$3,$5,$9)}' >> ${FIND}
if [[ -f $FIND ]] ;then
print "$FIND is created, find found something"
fi
if [[ -s $FIND ]] ;then
print "$FIND exists and is larger then zero"
fi

and your script will run faster if you use xargs
A. Clay Stephenson
Acclaimed Contributor

Re: find return code ???

If you mean by return code trhe value of ${?} then find's value is not going to help very much. Assuming you have no syntax errors, it's pretty much always going to be zero whether anything is found or not BUT because this is part of a pipeline it's really awk's value that you will see rather than find's.

Another approach might be to do this:
typeset -L7 USER
typeset -i10 SZ
typeset FNAME
find ${file} -type f -size +${FINDSZ}c -exec ll {} \; | awk '{printf("%7s %9d %-35s\n",$3,$5,$9)}' | while read USER SZ FNAME
do
echo "User = ${USER} SIZE = ${SZ} File = ${FNAME}"
done

Note that now you don't need a temp file.


If it ain't broke, I can fix that.
Graham Cameron_1
Honored Contributor

Re: find return code ???

First, when piping commands together, $? will always be the value returned from the last (ie the leftmost) command.

Hoever, unless I've misunderstood, it doesn't look like you need find at all.

Find will find all files from a given directory, but if you know the file name, you can get its size from any of "ls -l", "wc -c", "cksum".

eg
SIZE=$(ls -l ${f} |awk '{print $5}')
if [ $"SIZE -gt YOURMAX ]
then
YOUR CODE HERE to do something with ${f}
fi

If you do need to use find, then enclose the processing within a loop.
You don't need to do any other testing, as you know that find will only return the files you want.

for f in $(find ${file} -size +${FINDSZ}c )
do
YOUR CODE HERE to do something with ${f}
done

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.