http要求を行い、ステータスコードが200の場合のみ内容を保存する

#背景
curlにて機能を作っていたが、「要求が失敗した場合も-o(output option)に保存されてしまう」問題が有りシェルスクリプト化しました。

#コード

#!/bin/bash  
# ./curl+.sh {url} {output}  
URL=$1  
OUTPUT=$2  
TMPOUTPUT=/tmp/$(date +%Y%m%d_%H%M%S_%3N)  
   
status=`curl -o $TMPOUTPUT -li $URL -w '%{http_code}' -s`  
if test $status -eq 200 ; then  
cp $TMPOUTPUT $OUTPUT  
echo 1  
else  
echo 0  
fi  

http要求を行い、ステータスコードが200の場合のみ内容を保存する
https://blog.hashito.biz/2021/12/01/c9adc08f-bc17-447a-a05a-53e1986b988b/
著者
hashito
作成日
2021年12月1日
著作権