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
207 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
8 Comments
 
LVL 19

Expert Comment

by:jools
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jasonzx
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Accepted Solution

by:
jools earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Try this:

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now