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
215 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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 …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

724 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