My goal is to call a command, get stderr in a variable, but keep stdout (and only stdout) on the screen. Yep, that's the opposite of what most people do :)
For the moment, the best I have is :
#!/bin/bash
pull=$(sudo ./pull "${TAG}" 2>&1)
pull_code=$?
if [[ ! "${pull_code}" -eq 0 ]]; then
error "[${pull_code}] ${pull}"
exit "${E_PULL_FAILED}"
fi
echo "${pull}"
But this can only show the stdout in case of success, and after the command finish. I want to have stdout on live, is this possible ?
EDIT
Thanks to @sebasth, and with the help of Redirect STDERR and STDOUT to different variables without temporary files, I write this :
#!/bin/bash
{
sudo ./pull "${TAG}" 2> /dev/fd/3
pull_code=$?
if [[ ! "${pull_code}" -eq 0 ]]; then
echo "[${pull_code}] $(cat<&3)"
exit "${E_PULL_FAILED}"
fi
} 3<<EOF
EOF
I admit this not really "beautiful", seems tricky, and I don't really understand why the heredoc is needed...
Is this the better way to achieve that ?