Wednesday, January 31, 2024

[SOLVED] curl --fail without suppressing stdout

Issue

I have a script that uploads a file to a WebDav server using curl.

curl --anyauth --user user:password file http://webdav-server/destination/

I want two things at the same time:

  • have the script output to stdout (which is directed to a log file)
  • detect whether the upload succeeded

As far as I know, curl returns an exit code of 0 even in 401(unauthorized) or 407(conflict) situations. The --fail option can be used to change this behavior, but it suppresses stdout.

What would be the best workaround for this? tee and grep?


Solution

curl writes its output to stderr(2), the error stream instead of stdout(1). Redirect it on the command using 2>&1

curl --fail --anyauth --user user:password file http://webdav-server/destination/ 2>&1 > logFile
retval=$?
if [ $retval -eq 0 ]; then
    echo "curl command succeeded and the log present in logFile"
fi


Answered By - Inian
Answer Checked By - Candace Johnson (WPSolving Volunteer)