LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-20-2017, 04:45 PM   #1
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Rep: Reputation: 17
how to trap or capture an error from a hash check


Friends,

I have a script , working nicely , where i
do a md5sum check on a file and I print to a log

md5sum -c mypath/myFile.mp4 | tee -a myLogPath/mylog.txt

my actual command with my Vars is this
md5sum -c $pathdeliver/$f | tee -a $mypathdeliver/hashCheck_$ClientDrv_$nTexDrv.txt

as you can see I am using a piped into tee to save the output into a file
and as you know one expects this to output to the terminal and print in the txt file something like

myFile.mp4: OK

Here is what i wish to do ...and I am stumped as to how to do it

IF
the check is NOT ok

where the output would be
myFile.mp4: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

I would like to write a diffrent txt file . call it an error log

so if FAILED then I print something like
filename and failed into a error_Hash.txt

so this error_Hash.txt would only be made if there is a fail

something like that

thanks much
 
Old 12-21-2017, 09:49 AM   #2
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
i have tried the following
#!/bin/bash

cd /media/C_001

#md5sum -c /media/C_001/UNTA_AR0776-686057-LT630-25.mkv.md5 | tee result.txt
md5sum -c UNTA_AR0776-686057-LT630-25.mkv.md5 | tee result.txt
# md5sum -c /media/C_001/UNTA_AR0776-686057-LT630-26.mkv.md5 # | tee result.txt
# md5sum -c UNTA_AR0776-686057-LT630-26.mkv.md5
# | tee result.txt
if [ $? -eq 0 ]; then
echo all is well
else
echo something went wrong
fi

that is not working



what i am presently doing and I think ...works well enough

is to utilize the file that I am doing a tee into
read the last line
check for FAIL

like so

#!/bin/bash

cd /media/C_001


for f in *.md5; do

echo working on $f

md5sum -c /media/C_001/$f | tee -a /media/C_001/result.txt

lastLn=$( tail -n 1 /media/C_001/result.txt )
echo my last line was $lastLn

if [[ "$lastLn" =~ "FAILED" ]]; then
echo "the hash is bad"
echo $lastln F
echo $lastln FF
echo $f Fail >> /media/C_001/err_log.txt
else
echo "all is well"
fi

done



.......

maybe
 
Old 12-21-2017, 10:16 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
looks like you need something like this:
Code:
set -o pipefail
...
md5sum -c /media/C_001/$f 2>/tmp/errlog | tee -a /media/C_001/result.txt
# check ${PIPESTATUS[0]}
see man bash about it
 
1 members found this post helpful.
Old 12-21-2017, 03:14 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The classic method:
save the md5sum output in a variable,
check exit status,
print the variable.
Once you have a variable you can print it twice instead of duping with tee. And a print can be efficiently directed to an output stream (that is associated with a file once when the loop started)

Code:
for f in *.md5; do

  echo "working on $f"

  if
    output=$(md5sum -c /media/C_001/"$f")
  then
    : good
    printf "%s\n" "$output" >&3
  else
    : bad
    printf "%s\n" "$output" >&4
  fi
  printf "%s\n" "$output"

done 3>/media/C_001/result.txt 4>/media/C_001/err_log.txt
 
2 members found this post helpful.
Old 12-22-2017, 03:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I'm not really sure this is the best approach: you still lost the error message of md5sum.
 
Old 12-22-2017, 04:20 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You mean the stderr (descriptor 2) is not captured?
No problem, simply direct to the error stream
Code:
    output=$(md5sum -c /media/C_001/"$f") 2>&4
Or
Code:
    output=$(md5sum -c /media/C_001/"$f" 2>&4)
Explanation for the latter: The (subshell) gets the file descriptors cloned in the same way as the variables are cloned.
Of course you can also merge stderr into the output variable
Code:
    output=$(md5sum -c /media/C_001/"$f" 2>&1)
 
1 members found this post helpful.
Old 12-22-2017, 10:39 AM   #7
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
Thank you all for the wisdom
I have learned much

Thank you

best wishes
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DHCP FAILOVER ERROR: dhcpd[4002] trap divide error rip:2b73d0e64b5e rsp:7fff8f7c1560 error:0 businesscat Linux - Server 2 06-12-2017 02:12 AM
[SOLVED] How To Manually Duplicate SHA-512 hash with a known password & hash SlowLearner Linux - Security 11 05-31-2017 06:05 AM
[SOLVED] What is the best way to trap an error in a script AntBla Linux - Newbie 8 02-21-2015 09:40 PM
generate snmp trap to check High CPU Usage...... dashang.trivedi Linux - Networking 2 03-17-2011 02:51 AM
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration