tratar as saídas do curl [RESOLVIDO]

1. tratar as saídas do curl [RESOLVIDO]

Morpheus
Minions

(usa Nenhuma)

Enviado em 24/07/2021 - 18:23h

Para simular um problema alterei o nome do arquivo PACKAGE.txt na linha do curl abaixo.

O if não esta funcionando corretamente. Mesmo com alteração do nome de PACKAGE.txt para PACKAGE.txt1 ele cai em
echo " Arquivo PACKAGE.txt baixado para /tmp "


rm -Rf /tmp/PACKAGE.txt

curl "$URL"/PACKAGE.txt1 -o "/tmp/PACKAGE.txt1" 2>/dev/null

if [ "$?" == "0" ]; then

echo " Arquivo PACKAGE.txt baixado para /tmp "

ls -lh /tmp/PACKAGE.txt

else

echo " Erro ao baixar o arquivo PACKAGE.txt do site $URL "
ls -lh /tmp/PACKAGE.txt

exit


fi



  


2. MELHOR RESPOSTA

Mauricio Ferrari
maurixnovatrento

(usa Slackware)

Enviado em 25/07/2021 - 23:30h


Recriei seu script para testes. A opção -f gera a saída 7, já é o bastante para o script funcionar.

#!/bin/bash

URL="https://slackbuilds.org/slackbuilds/14.2/multimedia/acetoneiso"
FILE="README1"

rm -Rf /tmp/$FILE

curl -f $URL/$FILE -o /tmp/$FILE 2>&-

if [ "$?" == "0" ]; then
echo "Arquivo $FILE baixado para /tmp"
ls -lh /tmp/$FILE
else
echo "Erro ao baixar o arquivo PACKAGE.txt do site $URL"
exit 1
fi


___________________________________________________________
Conhecimento não se Leva para o Túmulo.
https://github.com/MauricioFerrari-NovaTrento


3. Re: tratar as saídas do curl

Paulo
paulo1205

(usa Ubuntu)

Enviado em 25/07/2021 - 20:30h

Da manpage do 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).


E também.
       --fail-early
Fail and exit on the first detected transfer error.

When curl is used to do multiple transfers on the command line,
it will attempt to operate on each given URL, one by one. By
default, it will ignore errors if there are more URLs given and
the last URL's success will determine the error code curl
returns. So early failures will be "hidden" by subsequent suc‐
cessful transfers.

Using this option, curl will instead return an error on the
first transfer that fails, independent of the amount of URLs
that are given on the command line. This way, no transfer fail‐
ures go undetected by scripts and similar.

This option is global and does not need to be specified for each
use of -:, --next.

This option does not imply -f, --fail, which causes transfers to
fail due to the server's HTTP status code. You can combine the
two options, however note -f, --fail is not global and is there‐
fore contained by -:, --next.

Added in 7.52.0.



... Então Jesus afirmou de novo: “(...) eu vim para que tenham vida, e a tenham plenamente.” (João 10:7-10)






Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts