So i have something along the lines of:
set -eo pipefail
ssh localhost "ls; exit 1" | cat > output
I thought pipefail would prevent cat to write to ouput (because of the exit code from ssh session), but it seems it will not. I then tried creating a fifo, but i am unsure of how to read the exit status of ssh without consuming the content in the fifo, because it seems it will wait until something "consumes" it.
set -eo pipefail
[[ -e /tmp/sshdump ]] && rm /tmp/sshdump; mkfifo /tmp/sshdump;
ssh localhost "ls; exit 1" > /tmp/sshdump &
wait $! && cat > output
So, how do i:
- avoid make the pipe exit early in the first example (and not reach cat)?
- checking exit code of ssh without consuming the fifo content?
if it matters, a POSIX compatible solution is always appreciated, but bash is fine too. In my particular case i'm using windows and git-bash, so maybe there is som quirks i'm not aware of.
EDIT: full disclosure, i should probably have explained what i was traying to achieve with this from the get go, rather than simplifying it to the point where the question doesn't properly reflect the intent. I was trying to pipe the output of "mysqldump" on a remote ssh shell directly to "wp db import" on my machine without creating intermediate files. I originally thought pipefail would stop the pipeline and thus prevent the import on my end to go through if there was a failure. Why this was desireable to me was simplicity of not creating temporary files, and of course less code.
sshsession completed successfully, when the remote commands finished. The remote$STATUSis not brought back from the remote session. This sounds like an XY problem. What are you trying to accomplish, that this seems desirable?