How to check whether a command such as curl completed without error?

211

Solution 1

The simplest way is to store the response and compare it:

$ response=$(curl -X POST [email protected] server-URL);
$ if [ "Upload successful" == "${response}" ]; then … fi;

I haven't tested that. The syntax might be off, but that's the idea. I'm sure there are more sophisticated ways of doing it such as checking curl's exit code or something.

update

curl returns quite a few exit codes. I'm guessing a failed post might result in 55 Failed sending network data. So you could probably just make sure the exit code was zero by comparing to $? (Expands to the exit status of the most recently executed foreground pipeline.):

$ curl -X POST [email protected] server-URL;
$ if [ 0 -eq $? ]; then … fi;

Or if your command is relatively short and you want to do something when it fails, you could rely on the exit code as the condition in a conditional statement:

$ if curl --fail -X POST [email protected] server-URL; then
    # …(success)
else
    # …(failure)
fi;

I think this format is often preferred, but personally I find it less readable.

Solution 2

You might be able to use curl's --fail option, though you should test it once first.

man curl

-f, --fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will pre‐ vent curl from outputting that and return error 22.

          This method is not fail-safe and there are occasions where  non-
          successful  response  codes  will  slip through, especially when
          authentication is involved (response codes 401 and 407).

In this way you could just do:

args="-X POST [email protected] server-URL"
curl -f $args && echo "SUCCESS!" ||
    echo "OH NO!"
Share:
211

Related videos on Youtube

Wes
Author by

Wes

Updated on September 18, 2022

Comments

  • Wes
    Wes over 1 year

    I'm passing the function:

    fillArrays(arrays);
    

    Now, arrays is a 2 dimensional array. I declared the arrays like so:

    int **arrays = new int*[NUM_OF_ARRAYS];
        arrays[0] = new int[128];
        arrays[1] = new int[512];
        arrays[2] = new int[2048];
        arrays[3] = new int[8192];
        arrays[4] = new int[32768];
        arrays[5] = new int[131072];
        arrays[6] = new int[524288];
        arrays[7] = new int[2097152];
    

    Am I passing arrays into the following function correctly?

    void fillArrays(int **arrays) {
        const int NUM_OF_ARRAYS = 8;
        for(int i = 0;i < NUM_OF_ARRAYS;i++) {
            switch(i) {
            case 0:
                for(int j = 0;j < 128;j++)
                    arrays[i][j] = 1 + rand() % 2000;
                break;
            case 1:
                for(int j = 0;j < 512;j++)
                    arrays[i][j] = 1 + rand() % 5000;
                break;
            case 2:
                for(int j = 0;j < 2048;j++)
                    arrays[i][j] = 1 + rand() % 10000;
                break;
            case 3:
                for(int j = 0;j< 8192;j++)
                    arrays[i][j] = 1 + rand() % 30000;
                break;
            case 4:
                for(int j = 0;j < 32768;j++)
                    arrays[i][j] = 1 + rand() % 100000;
                break;
            case 5:
                for(int j = 0;j < 131072;j++)
                    arrays[i][j] = 1 + rand() % 200000;
                break;
            case 6:
                for(int j = 0;j < 524288;j++)
                    arrays[i][j] = 1 + rand() % 1000000;
                break;
            case 7:
                for(int j = 0;j < 2097152;j++)
                    arrays[i][j] = 1 + rand() % 30000000;
                break;
            }
        }
    }
    

    Please let me know if you need any further information, thanks!

    • LeeNeverGup
      LeeNeverGup over 11 years
      How exacly did you declare arrays?
    • TravisG
      TravisG over 11 years
      Very tempted to give "Yes, you do pass arrays correctly" as an answer :P
    • Admin
      Admin over 11 years
      @LeeNeverGup I added my declarations to the question.
    • Fiktik
      Fiktik over 11 years
      OT: Get rid of the outer loop - you loop over code that immediately disambiguates every iteration into separate code branch. This is pointless.
    • StoryTeller - Unslander Monica
      StoryTeller - Unslander Monica over 11 years
      I'm sorry, but are you faced with some sort of problem that you think resulted from passing the array? Is there another point to this question?
    • Admin
      Admin over 11 years
      @Fiktik I don't understand, I want to assign each element of the 2d array, why does that not require an outer loop?
    • SingerOfTheFall
      SingerOfTheFall over 11 years
      @sir_, you can get rid of it and the switch by replacing arrays[i][j] with arrays[0][j],arrays[1][j], and so on
    • Fiktik
      Fiktik over 11 years
      @Sir_Blake_ Your outer loop iterates 8 times, you know this when writing the code and it will never change. Inside the loop, you choose which iteration are you in and do something special for each iteration. This is exactly the same as if you just did your eight different steps one after each other. Meaning, get rid of the loop, get rid of the switch and just have your eight inner loops one after each other. Of course you have to replace the indexing as SingerOfTheFall suggests. It would make your code more readable, and possibly more performant (optimizing compiler will do this anyway though)
  • SingerOfTheFall
    SingerOfTheFall over 11 years
    they are accessible inside his function
  • Artie
    Artie over 11 years
    @SingerOfTheFall, I believe that is what he said.
  • cyqsimon
    cyqsimon about 3 years
    This is probably the best practice in a scripting setting. For cURL exit codes see here: everything.curl.dev/usingcurl/returns