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
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
213 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Fine Tune your automatic Updates for Ubuntu / Debian
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…

829 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