detecting corrupted file

Hello,
Newbie question:
How can I detect a corrupted file from a script (ksh)?
Thank you,
Martin

Hmm, what would you define as a corrupted file? How would you know if something is corrupt? Since everything in Unix is just a file, I dont think you can say that one file is corrupt vs another file without evaluating its attributes. If its a binary, then running it would tell you that its corrupt when it fails. Capture the return code after its execution. Anything non-zero would imply an issue.
Can you clarify what you mean?

Sure! :slight_smile:
This file is a plain text file used for logging (ham.log)
I can tell it is corrupted because:
1) cat ham.log returns: ham.log: Corrupted file system detected.
2) The app that uses it to log doesn't start unless I delete it (it's then able to start and creates another ham.log file)
It gets corrupted when the computer loses power. (the application needs not to be logging at this precise time, but it's running)
Thanks,
Martin

I have never heard of behavior like that. What version of unix are you using?

I would try exactly what you did from the command line in a script. I assume that you are using ksh as your login shell. Since the cat command is detecting the problem, go with that. A well written command will return an exit code. See if your cat does. From the command line:
cat goodfile >/dev/null 2>&1
echo $?
cat badfile > /dev/null 2>&1
echo $?

Are the exit codes different?

I'm using QNX 6.2.1 w/ ksh.
My cat effectively returns 1 when this logfile is corrupted, and 0 when it isn't. That'll be perfect!

Thanks for your help!

By the way, this should work....

if cat somefile > /dev/null 2>&1 ; then
         echo file is ok
else
          echo file is corrupt
fi

That's exactly where I was...
And it does!
TY