Search code examples
shellcommandlsxargs

SHELL echo $? in the middle of command


Hellou. I like to ask you, how can I find out if some action in command working well. For example when i have command1 | xargs -ls -lA | command2 | command3

How can I find return number of function xargs ls -lA where may be problem.

Or better, how can I redirect errors out of STDOUT (Cause when im sreaching root directory, There are ls: cannot access errors) thank you


Solution

  • It's unclear what you are trying to achieve, so I'll try to answer a few possibilities. I'll also limit the answers to Bash, since that is the shell of most *nix users today:

    • You would like the pipeline to stop when there is an error in any of the four commands. You can't really do that unless you're willing to slow the pipeline down by reading each line of output from command1, and then using either break or set -o errexit to return to the appropriate level on failure.
    • You would like to record error output for the ls command. You can do this using redirection: command1 | xargs -ls -lA 2>ls.log | command2 | command3
    • You would like to know, once the pipeline is finished, whether any of the commands failed. You can do this by checking the contents of PIPESTATUS.