Shell-Script: Get cURL error, if wrong user











up vote
1
down vote

favorite












My shell script looks like this:



#!/bin/bash
curl -f -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt
res=$?
if test "$res" != 0; then
echo "the curl command failed with: $res"
else
echo "Success $res"
fi


I use this to ulpad a file...



Now my problem is, that I can't get all errors.
As an example if I enter a wrong URL (the right URL would be http://192.168.0.100:5005/home/test.txt), the upload fails, but the exit code still is 0.



Here is the output with a wrong URL:



<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
Success 0


How can I get those errors as well?



I also tried the same thing with cURL and and ftp target, there it works with all errors.










share|improve this question
























  • I never used -f. Based on your description, it is worth to extract the HTTP status from the output with serverResponse=$(curl -f ...) ... ... if [ ! -z $(printf '%sn' "${serverResponse}" | sed -n 's/^.*title.([0-9][0-9][0-9]) .*$/1/gp' ] ; then printf "Failuren"; fi
    – Jay jargot
    Nov 20 at 13:04

















up vote
1
down vote

favorite












My shell script looks like this:



#!/bin/bash
curl -f -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt
res=$?
if test "$res" != 0; then
echo "the curl command failed with: $res"
else
echo "Success $res"
fi


I use this to ulpad a file...



Now my problem is, that I can't get all errors.
As an example if I enter a wrong URL (the right URL would be http://192.168.0.100:5005/home/test.txt), the upload fails, but the exit code still is 0.



Here is the output with a wrong URL:



<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
Success 0


How can I get those errors as well?



I also tried the same thing with cURL and and ftp target, there it works with all errors.










share|improve this question
























  • I never used -f. Based on your description, it is worth to extract the HTTP status from the output with serverResponse=$(curl -f ...) ... ... if [ ! -z $(printf '%sn' "${serverResponse}" | sed -n 's/^.*title.([0-9][0-9][0-9]) .*$/1/gp' ] ; then printf "Failuren"; fi
    – Jay jargot
    Nov 20 at 13:04















up vote
1
down vote

favorite









up vote
1
down vote

favorite











My shell script looks like this:



#!/bin/bash
curl -f -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt
res=$?
if test "$res" != 0; then
echo "the curl command failed with: $res"
else
echo "Success $res"
fi


I use this to ulpad a file...



Now my problem is, that I can't get all errors.
As an example if I enter a wrong URL (the right URL would be http://192.168.0.100:5005/home/test.txt), the upload fails, but the exit code still is 0.



Here is the output with a wrong URL:



<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
Success 0


How can I get those errors as well?



I also tried the same thing with cURL and and ftp target, there it works with all errors.










share|improve this question















My shell script looks like this:



#!/bin/bash
curl -f -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt
res=$?
if test "$res" != 0; then
echo "the curl command failed with: $res"
else
echo "Success $res"
fi


I use this to ulpad a file...



Now my problem is, that I can't get all errors.
As an example if I enter a wrong URL (the right URL would be http://192.168.0.100:5005/home/test.txt), the upload fails, but the exit code still is 0.



Here is the output with a wrong URL:



<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
Success 0


How can I get those errors as well?



I also tried the same thing with cURL and and ftp target, there it works with all errors.







shell curl error-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 12:48

























asked Nov 20 at 12:38









dida110

344




344












  • I never used -f. Based on your description, it is worth to extract the HTTP status from the output with serverResponse=$(curl -f ...) ... ... if [ ! -z $(printf '%sn' "${serverResponse}" | sed -n 's/^.*title.([0-9][0-9][0-9]) .*$/1/gp' ] ; then printf "Failuren"; fi
    – Jay jargot
    Nov 20 at 13:04




















  • I never used -f. Based on your description, it is worth to extract the HTTP status from the output with serverResponse=$(curl -f ...) ... ... if [ ! -z $(printf '%sn' "${serverResponse}" | sed -n 's/^.*title.([0-9][0-9][0-9]) .*$/1/gp' ] ; then printf "Failuren"; fi
    – Jay jargot
    Nov 20 at 13:04


















I never used -f. Based on your description, it is worth to extract the HTTP status from the output with serverResponse=$(curl -f ...) ... ... if [ ! -z $(printf '%sn' "${serverResponse}" | sed -n 's/^.*title.([0-9][0-9][0-9]) .*$/1/gp' ] ; then printf "Failuren"; fi
– Jay jargot
Nov 20 at 13:04






I never used -f. Based on your description, it is worth to extract the HTTP status from the output with serverResponse=$(curl -f ...) ... ... if [ ! -z $(printf '%sn' "${serverResponse}" | sed -n 's/^.*title.([0-9][0-9][0-9]) .*$/1/gp' ] ; then printf "Failuren"; fi
– Jay jargot
Nov 20 at 13:04














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










-w 'http_code %{http_code}' will make curl add the HTTP status code at the end of the output.



Maybe you could go for this new version, which I only partially tested:



#!/bin/bash
serverResponse=$(curl -f -w 'http_code %{http_code}' -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt)
res=$?
if test "$res" != 0; then
printf "the curl command failed with: %sn" "${res}"
else
http_code="${serverResponse##*http_code }"
if [[ ! -z "${http_code}" && "${http_code}" -ne 200 ]] ; then
printf "Server sent back this http status: %sn" "${http_code}"
else
printf "Success %sn" "${res}"
fi
fi





share|improve this answer

















  • 1




    thank you so much it works perfect :-)!
    – dida110
    Nov 20 at 13:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393174%2fshell-script-get-curl-error-if-wrong-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










-w 'http_code %{http_code}' will make curl add the HTTP status code at the end of the output.



Maybe you could go for this new version, which I only partially tested:



#!/bin/bash
serverResponse=$(curl -f -w 'http_code %{http_code}' -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt)
res=$?
if test "$res" != 0; then
printf "the curl command failed with: %sn" "${res}"
else
http_code="${serverResponse##*http_code }"
if [[ ! -z "${http_code}" && "${http_code}" -ne 200 ]] ; then
printf "Server sent back this http status: %sn" "${http_code}"
else
printf "Success %sn" "${res}"
fi
fi





share|improve this answer

















  • 1




    thank you so much it works perfect :-)!
    – dida110
    Nov 20 at 13:47















up vote
1
down vote



accepted










-w 'http_code %{http_code}' will make curl add the HTTP status code at the end of the output.



Maybe you could go for this new version, which I only partially tested:



#!/bin/bash
serverResponse=$(curl -f -w 'http_code %{http_code}' -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt)
res=$?
if test "$res" != 0; then
printf "the curl command failed with: %sn" "${res}"
else
http_code="${serverResponse##*http_code }"
if [[ ! -z "${http_code}" && "${http_code}" -ne 200 ]] ; then
printf "Server sent back this http status: %sn" "${http_code}"
else
printf "Success %sn" "${res}"
fi
fi





share|improve this answer

















  • 1




    thank you so much it works perfect :-)!
    – dida110
    Nov 20 at 13:47













up vote
1
down vote



accepted







up vote
1
down vote



accepted






-w 'http_code %{http_code}' will make curl add the HTTP status code at the end of the output.



Maybe you could go for this new version, which I only partially tested:



#!/bin/bash
serverResponse=$(curl -f -w 'http_code %{http_code}' -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt)
res=$?
if test "$res" != 0; then
printf "the curl command failed with: %sn" "${res}"
else
http_code="${serverResponse##*http_code }"
if [[ ! -z "${http_code}" && "${http_code}" -ne 200 ]] ; then
printf "Server sent back this http status: %sn" "${http_code}"
else
printf "Success %sn" "${res}"
fi
fi





share|improve this answer












-w 'http_code %{http_code}' will make curl add the HTTP status code at the end of the output.



Maybe you could go for this new version, which I only partially tested:



#!/bin/bash
serverResponse=$(curl -f -w 'http_code %{http_code}' -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt)
res=$?
if test "$res" != 0; then
printf "the curl command failed with: %sn" "${res}"
else
http_code="${serverResponse##*http_code }"
if [[ ! -z "${http_code}" && "${http_code}" -ne 200 ]] ; then
printf "Server sent back this http status: %sn" "${http_code}"
else
printf "Success %sn" "${res}"
fi
fi






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 13:30









Jay jargot

1,8721410




1,8721410








  • 1




    thank you so much it works perfect :-)!
    – dida110
    Nov 20 at 13:47














  • 1




    thank you so much it works perfect :-)!
    – dida110
    Nov 20 at 13:47








1




1




thank you so much it works perfect :-)!
– dida110
Nov 20 at 13:47




thank you so much it works perfect :-)!
– dida110
Nov 20 at 13:47


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393174%2fshell-script-get-curl-error-if-wrong-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'