cozyconemotel
cozyconemotel

Reputation: 1161

curl --fail without suppressing stdout

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:

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?

Upvotes: 2

Views: 3615

Answers (2)

Coderer
Coderer

Reputation: 27264

In case you're finding this question in the future, you can now use the new fail-with-body flag.

Upvotes: 0

Inian
Inian

Reputation: 85560

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

Upvotes: 4

Related Questions