mruff
asked on
check unix curl command return value
Hi Experts,
I am calling a webservice via curl on AIX:
curl -k http://www.mocky.io/v2/57dd391e0f00007918a2d520 --user MyUser:MyUserPW-H "Content-Type: application/csv" -o myfile.csv
Now I'd like a way to check the return value of the curl command
I've tried:
retval=$(curl -k http://www.mocky.io/v2/57dd391e0f00007918a2d520 --user MyUser:MyUserPW-H "Content-Type: application/csv" -o myfile.csv)
but retval is always empty
Thank you for your support
I am calling a webservice via curl on AIX:
curl -k http://www.mocky.io/v2/57dd391e0f00007918a2d520 --user MyUser:MyUserPW-H "Content-Type: application/csv" -o myfile.csv
Now I'd like a way to check the return value of the curl command
I've tried:
retval=$(curl -k http://www.mocky.io/v2/57dd391e0f00007918a2d520 --user MyUser:MyUserPW-H "Content-Type: application/csv" -o myfile.csv)
but retval is always empty
Thank you for your support
ASKER
Perfect, thank you, I've tested with linux (opesuse) and it worked, have to test it tomorrow at the office with AIX, if it works with AIX as well, which I expect, then I assign you the points
AIX has ksh by default, and this shell will (in our case) show exactly the same behavior as bash under Linux.
Actually, I use to work with AIX/ksh way more often than with Linux/bash and I always apply a scheme like the above for testing return values.
You'll be fine, I'm sure!
wmp
Actually, I use to work with AIX/ksh way more often than with Linux/bash and I always apply a scheme like the above for testing return values.
You'll be fine, I'm sure!
wmp
ASKER
I've got another small question:
How can I evaluate the return value AND log the output of the curl command to a file
curl -f -H "Content-Type: application/csv" $curURL -o ${fileDestintionPath}${ext ractDate}_ ${resultFi leName} 2>&1 | tee -a ${extractDate}_logile.txt
RC=$?
echo 'curlretval:'$RC
curl -f -H "Content-Type: application/csv" bla -o ${fileDestintionPath}${ext ractDate}_ ${resultFi leName} | tee -a ${extractDate}_logile.txt
RC=$?
echo 'curlretval:'$RC
both solution do not work
Many thanks for this solution
How can I evaluate the return value AND log the output of the curl command to a file
curl -f -H "Content-Type: application/csv" $curURL -o ${fileDestintionPath}${ext
RC=$?
echo 'curlretval:'$RC
curl -f -H "Content-Type: application/csv" bla -o ${fileDestintionPath}${ext
RC=$?
echo 'curlretval:'$RC
both solution do not work
Many thanks for this solution
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks, found a solution with evaluating return value and write to log file
this works very well
checkRetVal () {
#echo 'checkRetVal:curlretval:'$ 1
if [[ $1 -ne 0 ]]; then
echo 'failed with returncode:' $1 'please check the log file: ' ${currentpath}'/'${extract Date}_logf ile.txt
echo 'curlcmd failed with returncode:' $1 >> ${extractDate}_logile.txt
exit 255
else
echo 'curlcmd succeeded' >> ${extractDate}_logile.txt
fi
return 0
}
RC=$?
#echo 'curlretval:'$RC
checkRetVal $RC
curl -f -H "Content-Type: application/csv" bla -o ${fileDestintionPath}${ext ractDate}_ ${resultFi leName} >> ${extractDate}_logfile.txt 2>&1
RC=$?
#echo 'curlretval:'$RC
checkRetVal $RC
curl -f -H "Content-Type: application/csv" $curURL -o ${fileDestintionPath}${ext ractDate}_ ${resultFi leName} >> ${extractDate}_logfile.txt 2>&1
RC=$?
checkRetVal $RC
the result in the console:
failed with returncode: 6 please check the log file: /home/wbk/pgm/2016-09-27-2 1-47-18_lo gile.txt
and the log file:
ore 2016-09-27-21-51-50_logfil e.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 34 0 34 0 0 197 0 --:--:-- --:--:-- --:--:-- 198
curlcmd succeeded
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: bla
curlcmd failed with returncode: 6
this works very well
checkRetVal () {
#echo 'checkRetVal:curlretval:'$
if [[ $1 -ne 0 ]]; then
echo 'failed with returncode:' $1 'please check the log file: ' ${currentpath}'/'${extract
echo 'curlcmd failed with returncode:' $1 >> ${extractDate}_logile.txt
exit 255
else
echo 'curlcmd succeeded' >> ${extractDate}_logile.txt
fi
return 0
}
RC=$?
#echo 'curlretval:'$RC
checkRetVal $RC
curl -f -H "Content-Type: application/csv" bla -o ${fileDestintionPath}${ext
RC=$?
#echo 'curlretval:'$RC
checkRetVal $RC
curl -f -H "Content-Type: application/csv" $curURL -o ${fileDestintionPath}${ext
RC=$?
checkRetVal $RC
the result in the console:
failed with returncode: 6 please check the log file: /home/wbk/pgm/2016-09-27-2
and the log file:
ore 2016-09-27-21-51-50_logfil
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 34 0 34 0 0 197 0 --:--:-- --:--:-- --:--:-- 198
curlcmd succeeded
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: bla
curlcmd failed with returncode: 6
ASKER
Perfect solution, thx
curl -k http://www.mocky.io/v2/57dd391e0f00007918a2d520 --user MyUser:MyUserPW-H "Content-Type: application/csv" -o myfile.csv
RC=$?
if [[ $RC -ne 0 ]]; then
echo "curl failed with returncode $RC"
else
echo "curl succeeded"
fi
If you want curl to exit with code 22 in case of a 404 response instead of outputting the document containing this 404 message provide the -f flag to curl:
curl -f -k http://www.mocky.io/v2/57dd391e0f00007918a2d520 --user MyUser:MyUserPW-H "Content-Type: application/csv" -o myfile.csv
...
...
The curl man page contains an exhaustive list of possible return values:
https://linux.die.net/man/1/curl :