Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:52:10+00:00 2026-06-06T03:52:10+00:00

I am writing a bash script for a backup. The script is going to

  • 0

I am writing a bash script for a backup.
The script is going to run a curl and this is going to return a certain code.

Depending on the result of this code:

  • the script must continue running (if the return code is ok, like 200)
    or
  • the script must return a not ok status (if the return code is not ok, like 400) and end the script without doing anything

How can the return be read out from the curl ??
Simple script for most of you but … 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:52:11+00:00Added an answer on June 6, 2026 at 3:52 am

    The Problem

    The curl program is shell-friendly, which means its exit status reflects the status of curl, not the HTTP status code.

    The Solution

    You can make a second call to the URL for a status code, use the write-out flag to append the status code to your output, or parse the headers. Here are some examples.

    The first option is naive, in that you are making two separate calls, so the status code may not be the same between calls. Nevertheless, it could be useful in some cases.

    # Make a second call to get the status code.
    curl --verbose http://www.google.com 2>&1 |
    sed -rn 's!^< HTTP/.* ([[:digit:]]+).*!\1!p'
    

    The better way to do this is to append the status code to standard output, and then strip it out after you capture it. For example:

    response=$(curl --silent --write-out "\n%{http_code}\n" http://google.com)
    status_code=$(echo "$response" | sed -n '$p')
    html=$(echo "$response" | sed '$d')
    

    Sample Output

    Using the example above, you can use these results any way you like. As one example, to view the HTML and the status code separately, you can do something like this:

    $ echo "$html"; echo; echo "HTTP Status Code: $status_code"
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>301 Moved</TITLE></HEAD><BODY>
    <H1>301 Moved</H1>
    The document has moved
    <A HREF="http://www.google.com/">here</A>.
    </BODY></HTML>
    
    HTTP Status Code: 301
    

    Branching

    Now that you have the status code, you can branch based on the value using an if/then or case statement. For example:

    case "$status_code" in
        200) echo 'Success!'
             ;;
          *) echo 'Fail!'
             exit 1
             ;;
    esac
    

    Note that you’ll have to set your own exit status, and that you can’t just re-use the HTTP status code. A shell exit status must be between 0-255, and many HTTP status codes are outside that range.

    See Also

    • How can I get an HTTP status code with an AWK one-liner?
    • https://superuser.com/questions/272265/getting-curl-to-output-http-status-code
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing this Bash script: count=0 result for d in `ls -1 $IMAGE_DIR
Im writing a bash-script to perform an offsite backup, using rsync over SSH. I'm
I am writing a bash script to run an integration test of a tool
I am currently writing a bash script and when i run the useradd command
I am writing a bash shell script, run where the user is expected to
I am writing a bash script that, when run from directory B , mirrors
I'm writing a bash script called 'run' that tests programs with pre-defined inputs. It
I am writing a bash script that calls some apps that run in their
I am writing a bash script that modifies a file that looks like this:
I'm writing a bash script that is intended to execute some command, and depending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.