set a variable and detect error status...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set a variable and detect error status...
# 1  
Old 06-23-2010
Question set a variable and detect error status...

Hi there,

I'm driven crazy by a new problem.
It seems very complex to me and I see no way to come around.

In my script, I receive the path to a tgz file and I want to output the md5 sum of the file inside the tgz. I want a way to detect if tar fails.

I'm using the following command
Code:
tar -xzOf "$1" 2>/dev/null | md5sum | cut -b-32

  • If the tgz contains a regular file, this command returns its md5 sum.
  • If the tgz contains an empty file, it returns d41d8cd98f00b204e9800998ecf8427e.
  • If the tgz is corrupted, it will also return d41d8cd98f00b204e9800998ecf8427e.
Invoking the error code will not help because it will return the one of the last command which is cut.
How would you do that? I need to output the md5sum of the zipped file (empty or not) and I want something else (empty, error code, whatever) if the tgz is corrupted.

Can you help me?
Cheers
Santiago
# 2  
Old 06-23-2010
Code:
tar tzf "$1" > /dev/null 2>/dev/null; if [ $? == "0" ]; then tar -xzOf "$1" 2>/dev/null | md5sum | cut -b-32; else echo "error - tar corrupted?"; fi


Last edited by bartus11; 06-23-2010 at 01:33 PM.. Reason: minor tweak
# 3  
Old 06-23-2010
Thanks bartus11
Your script works indeed.
The thing is that I'm working with big files and I wanted to avoid using tar twice.
A simple tar -tzf can take up to 4 seconds. Files are processed one by one and your script would double the time necessary to complete all files.
If you find any other way to avoid that, it would be great.
Thanks anyway for your proposal.
Santiago

---------- Post updated at 18:52 ---------- Previous update was at 18:36 ----------

Actually, I need to invoke tar twice in my script and I wanted to avoid invoking it three times.
First time I get the md5sum, second time, I read the statistics from tar -t.
Then I insert those informations in a database.
So here is what I did:
Code:
file_md5=$(tar -xzOf "$1" 2>/dev/null | md5sum | cut -b-32)
(tar -tzvf "$1" 2>/dev/null || echo error) | while read -r file_mod file_owner dummy file_date file_time name; do
	query="UPDATE uploaded
	SET file_mod = '${file_mod:1}',
	file_owner = '${file_owner%%/*}',
	file_group = '${file_owner#*/}',
	file_time = '$file_date $file_time',
	file_md5 = '$file_md5',
	name = \"$name\",
	status = IF('$file_mod' == 'error', 3, status)
	WHERE id = $id"
done

Thx for your help
# 4  
Old 06-23-2010
You can untar that file to /tmp and then calculate md5 checksum:
Code:
tar -xzOf "$1" 2>/dev/null > /tmp/tar.tmp && md5sum /tmp/tar.tmp | cut -b-32

This code won't produce any output if tar file is corrupted, as md5sum won't even lauch in case tar fails.
# 5  
Old 06-23-2010
Quote:
Originally Posted by chebarbudo
Invoking the error code will not help because it will return the one of the last command which is cut.
How would you do that?
With bash you can check the $PIPESTATUS.

Regards
# 6  
Old 06-23-2010
Good idea bartus11
Thanks Franklin52, this will be very usefull!
# 7  
Old 06-23-2010
Alternatively, in ksh93 and bash you can use pipefail, so that the RC at the end of pipes is the value of the last non-zero command to fail or zero if no command has failed.
Code:
set -o pipefail
tar -xzOf "$1" 2>/dev/null | md5sum | cut -b-32 || echo "error - tar corrupted?" >&2
set +o pipefail

output:
Code:
d41d8cd98f00b204e9800998ecf8427e
error - tar corrupted?


Last edited by Scrutinizer; 06-24-2010 at 03:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Detect error and exit

Hi, I am using Jenkins and have integrated JMeter for testing. JMeter is generating a file with results properly using shell script. I have to validate the results and exit the shell with non-zero so my jenkins job gets failed. I tried multiple ways to grep the output file and read the results... (2 Replies)
Discussion started by: sitaram
2 Replies

2. AIX

How to detect the network cable status with c programming on AIX

Hello, Is there any API or any other approach to detect whether the network cable is connected to the network adapter, say, en0, en1 or en2? The OS is AIX6.1. Thank you. (4 Replies)
Discussion started by: zephyrbj
4 Replies

3. Shell Programming and Scripting

script to detect a file from inserted usb and puts into a Variable

There is a same named log file that I have on my 2 different android phones. When I plug it into my computer, it appears in the media folder, For example the first android phone: /media/F6BA-0AF5/folder/A.log I want to put that into a variable to be manipulated.... (3 Replies)
Discussion started by: tobenguyen
3 Replies

4. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

5. Shell Programming and Scripting

how to detect port open status?

I write a script which will stop an application, then restart it. Sometimes it is succesful, sometimes not. The problem is, when stop the application, some ports are still listenning (or not released). When start the application, it reports that ports are used, and can't continues. I use... (1 Reply)
Discussion started by: rdcwayx
1 Replies

6. Windows & DOS: Issues & Discussions

Autosys unable to set the job to the status of FAILED

Hi The Autosys job that call DOS batch file which in turn calls java and this java program has successful completion though there are series of exception occurred in the data it handles and want the job to be set to Fail in its autosys, how could it be done? a.bat { java sample.class ... (1 Reply)
Discussion started by: muthupus
1 Replies

7. UNIX for Dummies Questions & Answers

Environment Status Variable

Hi all, understood that upon executing a script, we can get various in-built variable like: $? => Exit status $$ => Process PID ${0##*/} => Process ID Name Is there any readily available lookup table as to what are the available status variablee, and what do they mean... Thanks in... (2 Replies)
Discussion started by: srage
2 Replies

8. UNIX for Dummies Questions & Answers

$? = Exit status variable

hi, exit status variable $?, returns some digits. 0 ---> succes. 1..126 Failure (the program itself will decide what the numbers mean) 127 Command not found 128..254 The program did not exit normally. (E.g., it crashed, or received a signal) 255 Invalid exit code well, if $?... (4 Replies)
Discussion started by: dummydba
4 Replies

9. Red Hat

Installing RedHat 8.0 onto Dell PowerEdge SC1425 - hdc: status error: status = 0x58

I have successfully installed RedHat 8.0 onto a Dell PowerEdge SC1425 today. This server has two SATA hard drives, and an IDE DVD-ROM drive. Using the following kernel parameters, i successfully installed across both hard drives from CD: ide0=0x1f0,0x3f6,14 vga=791 resolution=1024x768 expert... (5 Replies)
Discussion started by: fishsponge
5 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question