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
211 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
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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 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.:

803 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