2

I'm submitting to my website using cURL. I make cURL post it in timely fashion.

In below code; first line is curl submit; second line is for queuing:

curl -b cookies.txt \
    -d title="$(sed '1,/sblmtitle/d;/slpstitle/,$d' sedut.html)" \
    -d taxonomy%5Btags%5D%5B1%5D="$(
        sed '1,/sblmkategori/d;/slpskategori/,$d' sedut.html
    )" \
    -d teaser_include=1 \
    -d body="$(sed '1,/sblmkonten/d;/slpskonten/,$d' sedut.html)" \
    -d field_source%5B0%5D%5Burl%5D="$(
        sed '1,/sblmurl/d;/slpsurl/,$d' sedut.html
    )" \
    -d changed= \
    -d form_build_id=form-424f851ad50bd4781c8c25ab7efd5c4c \
    -d form_token=0e7cc7437faf816f1ecd96087286bda9 \
    -d form_id=post_node_form \
    -d op=Save http://www.web.org/submit/post &&
for file in $(ls *.html | sort -r | tail -1); do
    mv $file sedut.html
done

If cURL fails to submit, it will print out sedut.html contents. If cURL success submitting, it will print nothing. In my case, sometime cURL fails to submit.

What I want is don't execute second line if first line fails. But using && as above codes is just not works. I've tested it, cURL submit fails and print out sedut.html contents, but second line still executed.

2
  • Have you checked the curl exit status? Is it different in your fail and not-fail situations?
    – enzotib
    Oct 5, 2011 at 6:50
  • 1
    enzotib@ It giving me 0 even on failed submission Oct 5, 2011 at 11:43

2 Answers 2

3

curl has no idea that it failed - it had well-defined input, it communicated with the server and got back a 200 OK response. Because of this, you can't rely on the exit code of curl (which is what && does).

Instead, we have to use something else to determine success or failure, such as the output generated by curl.

As a side note, it is generally not a good idea to use ls in a script. Here, it will fail if a file happens to have a newline or a space character. It may be unlikely, but but it's possible. See http://mywiki.wooledge.org/ParsingLs for more details. As a rule of thumb, I only use ls in scripts when displaying the results for human eyes. If I'm processing the results in any way, I use find . -0 ...

1

&& and || (as a success/fail tests) must be on the same logical line as the command(s) you want to be executed. Use a line continuation && \ and/or curly brackets { }

Note: As pointed out by amphetamachine, the line continuation issue does not apply in modern sh-compatible shells

true && echo "true"

true && \
   echo "true"

true && \
{  echo "true"; echo "===="; }

(( 123 == 123 )) && {
   echo "true"
   echo "====" 
}
2
  • 1
    In modern sh-compatible shells, && and || don't need a backslash at the end as they are considered statement-ending marks like ; and &. Shell code in makefiles can't do this, however. Oct 5, 2011 at 5:14
  • @amphetamachine: Yes, I just tested it and it's true. thanks... How about that! That makes things a bit easier for general scripting, but it makes my answer somewhat useless for the question :(... but because it does work, that seems to imply that curl is returning a non-zero exit code
    – Peter.O
    Oct 5, 2011 at 5:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .