Capture error before pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture error before pipe
# 1  
Old 11-23-2017
Capture error before pipe

Hi,

I have a script that runs a tar command to standard out then pipes to a gzip:
Code:
tar cfE - * | gzip -c > OUT.gz

At the moment, even if the tar fails (e.g. because of lack of disk space), the gzip still runs successfully.

Is there a way to make the whole line exit with a non-zero error status if the tar fails? I have to run it on a single line, because it is tar'ing a large directory.

Thanks


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 11-23-2017 at 09:06 AM.. Reason: Added CODE tags
# 2  
Old 11-23-2017
In this case, are you looking for errors such as tar being unable to read files etc.?

You might get away with something like this:-
Code:
tar cfE - * 2>/tmp/tar.log | gzip -c > OUT.gz
gzip_RC=$?

tar_RC=$(wc -l < /tmp/tar.log)


if [ $gzip_RC -ne 0 ]
then
   echo "The gzip failed RC=$gzip_RC"
fi


if [ $tar_RC -ne 0 ]
then
   echo "The tar failed RC=$tar_RC"
fi

You don't tell us your OS, so what does your manual page say the E flag is for?




Kind regards,
Robin
# 3  
Old 11-23-2017
Me too, I'd like to know more about the E option . . .
If your shell (which you fail to mention, btw) were bash, did you consider the
Code:
echo ${PIPESTATUS[@]} 
2 0

shell variable array?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-23-2017
Sorry - I'm using Solaris 10 and 11. The man page explains it better than I can:
Quote:
The E flag is required whenever the larger files and/or files with longer names, or whose UID/GID exceed 2097151
I use it because there is a limit on the number of files that tar cf accepts, that the E flag extends.

It hadn't occurred to me to send standard error to another file; that sounds like the best solution for my case, because I am somewhat limited in what I can run. What I am actually doing here is running an Ingres database backup and changing the backup template file.

Thanks for your help - I'll give it a go.

Jackie

Last edited by rbatte1; 11-23-2017 at 11:12 AM.. Reason: Changed " quote to be QUOTE tags
# 5  
Old 11-23-2017
With bash3 or up, zsh and a recent ksh93, you can also use pipefail:
Code:
$ set -o pipefail; false | true ; echo "$?"
1

On Solaris 10, what shells/versions do you have available?
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-23-2017
Quote:
Originally Posted by Catullus
Sorry - I'm using Solaris 10 and 11. The man page explains it better than I can:

I use it because there is a limit on the number of files that tar cf accepts, that the E flag extends.
In that case, why not just tar -cf - . ? Captures the entire current directory without having to pass in a literal 100,000 files.

Or perhaps even tar -zcf out.gz .
# 7  
Old 11-24-2017
You did not tell us which shell you are using to execute the pipe. At least with bash and Zsh, you have access to the exit status of each process in the pipe. For bash, search for PIPESTATUS in the man-page. For Zsh, it is pipestatus.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to capture the error code a use it in error table?

Hello Everyone, I have written a file validation script in unix to compare the data and trigger file.My requirement is if the file validation fails,I need to upate the error details in a table ex:Below is the one of many validation checks i am doing if then echo "Actual count between... (3 Replies)
Discussion started by: karthik adiga
3 Replies

2. UNIX for Dummies Questions & Answers

How to Capture Informatica Error logs?

HI Team , Is there a way to capture information logs ( session or workflow ) and grep only ERROR or FAIL messages and send to email distro using mailx or sendmail option. I have around 200 sessions running on daily basis and this script has to run every day , and capture only error... (0 Replies)
Discussion started by: Perlbaby
0 Replies

3. Shell Programming and Scripting

Capture output of open pipe to a file in perl

Hi, I am trying to capture the output of the an open pipe in perl. but I am not sure how to do this. can some one please help me do that? Below is the script I am using (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

4. UNIX for Dummies Questions & Answers

Capture crash out error report...

Google is not helpful here. It is probably trivial but I can't get my head around it. How do I capture an error which crashes out due to say a syntax error to a file? (Note that I manually reset error.log to zero length.) Consider this junk code:- #!/bin/sh # Launch as ./trap.sh... (2 Replies)
Discussion started by: wisecracker
2 Replies

5. Shell Programming and Scripting

Not able to capture sftp error in Korn Shell

I am not able to capture error condition in sftp in Korn Shell #!/bin/ksh sftp batch@uat >abc 2>&1 << ENDFILE cd public put /data/WELCOME_55 ENDFILE ret_val=$? if ] then print file "copied successfully" else print file "NOT copied successfully" fi return 0 Now the... (9 Replies)
Discussion started by: Soham
9 Replies

6. Shell Programming and Scripting

Not able to capture error while using dialog

I am not able to capture errors while I am using dialog. For example: dialog --gauge "Verifying file..." 10 75 < <( while read LINE do hash=$(echo $LINE | cut -f1 -d' ') directory=$(echo $LINE | cut -c 34-) PCT=$(( 100*(++i)/n )) echo $PCT md5deep -a $hash $directory >... (3 Replies)
Discussion started by: yamanoorsai
3 Replies

7. UNIX for Dummies Questions & Answers

Capture Error In SQL Plus

Hi I am trying to fetch the data from Oracle Table More my_query.sql | sqlplus -s 'scott/tiger@OrcaleSID ' | sed 's///g;s///g' > sample_file Now if the table passed in the my_query.sql is wrong or there is some other error insied SQL how i can caputure that i tried More... (1 Reply)
Discussion started by: max_hammer
1 Replies

8. Shell Programming and Scripting

database instance Error capture

I wrote a script to capture some rows from the DB. However I want to capture the errors if the DB instance is down which usually produces errors like below. What should be my approach to kill script if the DB instance is down: DATABASE ERRORS: Msg 937, Level 14, State 1: Server... (2 Replies)
Discussion started by: moe458
2 Replies

9. Shell Programming and Scripting

Capture Schell script error

I work on AIX 5.x. I have a script which does lot of processing & calls multiple child scripts. How do I capture the error of the parent script if it fails? Thanks Sumeet (3 Replies)
Discussion started by: sumeet
3 Replies

10. Shell Programming and Scripting

sqlplus -s error capture

Hi folks a wee problem that requires some help. I am writing a script that allows me to automate the changing of a password in sqlplus the problem i encounter is that the code contains a case statement that allows the helpdesk to select whether to allow or deny access by switching the... (1 Reply)
Discussion started by: w33man
1 Replies
Login or Register to Ask a Question