ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I guess it's rather hard: really, somecommand will very often get sigpipe after head exits. So its exit status is mangled. You can create a temporary file with mktemp, redirect somecommand to it, remember exit status, show two first lines of a file to user and then remove the file.
Like raskin said, this is not easy to solve without some extra work. Maybe this will help:
somecommand 1>/dev/null 2>&1 && somecommand | head -n 2
If the first part (somecommand 1>/dev/null 2>&1) fails, the rest (somecommand | head -n 2) is not executed and accordingly the exit status is 1. If the first part works, the second part is executed (with always has exit status 0, as you stated).
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.
But: The pipefail option depends on the bash version you are using. My version uses the ! to toggle the way the exit status is handled inside a pipe, pipefail is not recognized or mentioned in the manpage.
This:
! somecommand | head -n 2
Does the trick on my box.
$ bash --version
GNU bash, version 2.05b.0(1)-release (i686-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
druuna: your suggestion does work and is quite clever, but unfortunately the head command only filters the second command after the && so I can't catch and limit the output.
spirit receiver: I read the man bash again and found the references for pipefail, then at the beginning of the script I did "set -o pipefail" and but later in the script it seems to always return status of 141 for the pipe command regarless of whether it works or not!
druuna: your suggestion does work and is quite clever, but unfortunately the head command only filters the second command after the && so I can't catch and limit the output.
The first part (before &&) is there to check if the second part should be executed, the second part can be changed/edited or something else completely:
ls [0-9]* 1>dev/null 2>&1 && echo "Yep, present"
The echo part will only be executed if the first part is true, otherwise the exit status will be 1.
ls [0-9]* 1>/dev/null 2>&1 && ls [0-9]* | grep 31 | head -n 2
All after && will only be executed if the first part is true, otherwise the exit status will be 1.
The first part (before &&) is there to check if the second part should be executed, the second part can be changed/edited or something else completely:
ls [0-9]* 1>dev/null 2>&1 && echo "Yep, present"
The echo part will only be executed if the first part is true, otherwise the exit status will be 1.
ls [0-9]* 1>/dev/null 2>&1 && ls [0-9]* | grep 31 | head -n 2
All after && will only be executed if the first part is true, otherwise the exit status will be 1.
But using pipetail (or !) is more elegant
I fully understand what you are saying but in practice the |head fails to catch the output from the first command which is the whole point. If that was what I wanted to do, I could leave the pipe off altogether and just collect the exit status as normal...
I told you! When you run like this, head gets two lines - and exits. Ah oh who will collect somecommand's output? Nothing? Then somecommand gets SIGPIPE - broken pipe (it is, as head is dead). And surely it exits, as it has no signal handler there. Exit status = 128 + signal nr = 128 + 13 = 141 . Everything as predicted.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.