LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-28-2013, 05:45 AM   #1
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 555

Rep: Reputation: 75
Bash : preserve return value through pipe


The answer is eluding me, my apologies for this silly question :-)

I have a function logger that will log to screen or file based on some variables. This function kind of works, but provides a nasty thing: the return value of the calling command is lost (as one could expect);

I tried to solve it like this:
Code:
#!/bin/bash

logger()
{
        RETVAL=$?
        echo "RETVAL=$RETVAL"
        return $RETVAL
}

false | logger || echo $?
Just as a simple test, to see if I can pipe the output of one command to a new command and keep the return value... The above does not work... Anyone who has a nice thought how I can make my logger function return the return value of the first command?
 
Old 03-28-2013, 06:01 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Sorry, I can't see why you're piping, this works:
Code:
#!/bin/bash

logger()
{
        RETVAL=$?
        echo "RETVAL=$RETVAL"
        return $RETVAL
}

false
logger
echo $?
 
Old 03-28-2013, 06:19 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Recent versions of bash provide a PIPESTATUS array variable, which holds all the exit codes of the most recent command chain.

"$?" will only hold the exit status of the most recently executed command, which will be the last one in the pipe chain.

Edit: For details, always check the documentation. From the bash man page:

Quote:
The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.

Last edited by David the H.; 03-28-2013 at 06:25 AM. Reason: as stated
 
1 members found this post helpful.
Old 03-28-2013, 06:33 AM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 555

Original Poster
Rep: Reputation: 75
Normally my logger() function would print the output of the provided command to screen, logfile or both (depending on a variable); So the implementation written above was only to illustrate what I tried.

In the specific case I try to do:

Code:
svn export ${SVN_URL} | logger
Given the PIPESTATUS[] array, I should be able to:
Code:
SVN_URL="https://bogus.url"
get_from_svn()
{
    svn export ${SVN_URL} | logger | return ${PIPESTATUS[0]}
}

main()
{

   get_from_svn || (echo "Something went wrong." ; return $?)
}

main $@
however, I seem to do something wrong yet... Thanks for pointing me to PIPESTATUS[] array, I totally forgot about that one.
 
Old 03-28-2013, 06:39 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 555

Original Poster
Rep: Reputation: 75
Ah, finally got it :-)

Code:
#!/bin/bash
# do something
#

logger()
{
        read string
        printf "[LOG] %s\n" "${string}"
}

get_from_svn()
{
        svn export https://bogus.url 2>&1 | logger
        STATUS=${PIPESTATUS[0]}
        echo "Status: ${STATUS}"
        return ${STATUS}
}

main()
{
        get_from_svn
        echo "Returned: $?"
}

main
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: pass a return value down a pipe? hydraMax Programming 8 01-04-2013 07:10 AM
[bash] sudo: preserve non-std file descriptors DieZwiebel Linux - Software 1 02-27-2012 09:41 AM
kernel_sendmsg function send tcp package would return broken pipe error asert Linux - Kernel 1 06-01-2008 04:07 PM
bash script to preserve only first digits alenD Linux - Software 6 04-14-2008 12:51 PM


All times are GMT -5. The time now is 11:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration