detecting corrupted file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

detecting corrupted file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-24-2003
starless starless is offline
Registered User
 
Join Date: Sep 2003
Last Activity: 6 February 2005, 9:21 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
detecting corrupted file

Hello,
Newbie question:
How can I detect a corrupted file from a script (ksh)?
Thank you,
Martin
Sponsored Links
    #2  
Old 09-24-2003
google's Avatar
google google is offline Forum Advisor  
Advisor
 
Join Date: Jul 2002
Last Activity: 17 June 2009, 5:17 PM EDT
Location: Atlanta
Posts: 740
Thanks: 0
Thanked 2 Times in 2 Posts
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?
Sponsored Links
    #3  
Old 09-24-2003
starless starless is offline
Registered User
 
Join Date: Sep 2003
Last Activity: 6 February 2005, 9:21 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Sure! :-)
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
    #4  
Old 09-24-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 17 April 2014, 1:42 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
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?
Sponsored Links
    #5  
Old 09-24-2003
starless starless is offline
Registered User
 
Join Date: Sep 2003
Last Activity: 6 February 2005, 9:21 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #6  
Old 09-24-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 17 April 2014, 1:42 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
By the way, this should work....
Code:
if cat somefile > /dev/null 2>&1 ; then
         echo file is ok
else
          echo file is corrupt
fi

Sponsored Links
    #7  
Old 09-24-2003
starless starless is offline
Registered User
 
Join Date: Sep 2003
Last Activity: 6 February 2005, 9:21 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
That's exactly where I was...
And it does!
TY
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
WTMPX File corrupted drestarr96 Solaris 6 01-24-2012 04:54 AM
Find out if PDF file is corrupted sdohn Shell Programming and Scripting 1 08-30-2009 02:24 PM
may a corrupted .gz file be repaired? JohnTinker UNIX for Dummies Questions & Answers 3 03-19-2008 03:03 PM
Could be a corrupted file? kaugrs HP-UX 1 01-16-2008 11:22 AM
corrupted tar file klintsovi UNIX for Dummies Questions & Answers 1 07-21-2001 09:50 AM



All times are GMT -4. The time now is 02:28 PM.