Solved

How do I get the error code of Program 1 if it's being piped thru Program 2?

Posted on 2008-11-03
8
214 Views
Last Modified: 2010-04-22
In bash, I'm using a line like this:
program1 | program2

I want to get the error code program1 returns but "echo $?" will  always return 0 because program2 always succeeds.

Any ideas?
0
Comment
Question by:jasonzx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Expert Comment

by:jools
ID: 22868304
depends what you want to do, if you only want to execute program 2 if program 1 is ok you can use `&&`
or separate the programs;

program1 > /tmp/log1 2>&1
echo $?
[whatever]
cat /tmp/log1 | program2


give us a bit more info for your overall goal...

0
 

Author Comment

by:jasonzx
ID: 22868394
I'm using an app called ntfsresize to resize a partition. The output of the command is being piped thru grep and tr to extract the % complete. All of this is wrapped up in a dialog line which displays a progress bar on the screen. ntfsresize sometimes fails(ie: if the volume is marked dirty). I want to know when it fails so I can tell the user to reboot and run chkdsk from Windows.
0
 
LVL 19

Expert Comment

by:jools
ID: 22868513
Would it be better to just let the process run so you can check the halt code which is the bit that matters.

or do something like;

echo "Please Wait...this may take some time so go and get a cuppa!"
ntfsresize <whatever-you-have-here> > /tmp/logfile 2>&1
ERR=$?
if [[ $ERR -ne 0 ]]
then
   echo Woopsy!
else
   echo Yippeee!
fi


You could probably do something with `wait` and check the logs but why bother???

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:jasonzx
ID: 22869016
Maybe I missed something. How does that work if ntfsresize is being piped? ERR would still be equal to 0 since the other commands in the pipe succeeded.
To clarify a bit, here's the exact line I'm working with:
$DIALOG --gauge "Resizing NTFS partition" 7 75 < <(ntfsresize -s $size_b $winlocation | tr '015' '\n\r' | grep "percent completed" | sed -e 's/[.][0-9][0-9] percent completed//')
0
 
LVL 19

Accepted Solution

by:
jools earned 500 total points
ID: 22869165
I was saying forget the fluffy stuff, the important bit is to find out if it completed successfully.

Unless there is something you can do if the output is put to a log file but I dont have access to a system I can try it on at the moment. Does the log file report the error or success at the end?

#!/bin/bash
ntfsresize -s $size_b $winlocation > /tmp/logfile 2>&1 &
$DIALOG --gauge "Resizing NTFS partition" 7 75 << (cat /tmp/logfile | tr '015' '\n\r' | grep "percent completed" | sed -e 's/[.][0-9][0-9] percent completed//')
RESULT=`cat /tmp/logfile | grep "ERROR"`
if [[ $RESULT = "ERROR" ]]
then
   echo "ohdear"
fi

RESULT=`cat /tmp/logfile | grep "OK"`
if [[ $RESULT = "OK" ]]
then
   echo "OK"
fi
0
 

Author Comment

by:jasonzx
ID: 22869511
Ok. I see what you're saying but I'd rather get the actual error code in-case the program throws something at me like '0 ERRORS' or in an instance or two uses 'failed' instead of 'error', etc.
Modified it a bit so this gives me what I want but it feels like a bit of a hack. Will leave the question open for a while longer to see if anyone can come up with something a little more elegant.

$DIALOG --gauge "Resizing NTFS partition" 7 75 < <(ntfsresize -bff -s $size_b $winlocation | tr '015' '\n\r' | grep "percent completed" | sed -e 's/[.][0-9][0-9] percent completed//';echo ${PIPESTATUS[0]}>/tmp/retval)
RET=`cat /tmp/retval`
if [ $RET = "0" ]
....etc

0
 
LVL 48

Expert Comment

by:Tintin
ID: 22869527
In bash you have the PIPESTATUS variable (actually an array) which you can use to tell the exit status of any commands in a pipe.  So in your case, you can do

program1 | program2

if [ $PIPESTATUS -ne 0 ]
then
    echo "program 1 failed"
fi
0
 
LVL 3

Expert Comment

by:bishillo
ID: 22869557
Try this:

{
program1
if [ $? != 0 ]
then
   ... all the checking you want ...
fi
} | program2
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question