Ross Hettel
Ross Hettel

Reputation: 1129

Get curl error description in bash script?

I have a few cron jobs that run and rely on curl to do some actions. If they fail, I send myself a mail with the curl exit code. To figure out what went wrong, I then query the curl exit code and wind up on this page: https://curl.haxx.se/libcurl/c/libcurl-errors.html.

Is there a way in my bash script to look up the error description and send myself that instead of just an error code?

Upvotes: 1

Views: 323

Answers (2)

ccarton
ccarton

Reputation: 3666

You can just copy all of those error messages into your bash script as an array:

curl_codes=(
  "All fine. Proceed as usual."
  "The URL you passed to libcurl used a protocol that this libcurl does not support."
  "Something else")

[ $exit_code -eq 0 ] || echo "Error $exit_code: ${curl_codes[$exit_code]}"

Upvotes: 1

laurent
laurent

Reputation: 90756

One way is to use the --log-file flag to create a log file and then to parse this log. This is what I do in this script, in particular see below the grep commands I'm using to lookup for error messages.

if [ -n "$(grep "rsync error:" "$LOG_FILE")" ]; then
    # Handle errors
elif [ -n "$(grep "rsync:" "$LOG_FILE")" ]; then
    # Handle warnings
fi

Upvotes: 1

Related Questions