我的 Bash 中的输出curl
是:
object(\Response\)#1399 (3) {
["httpResponse"]=>
object(GuzzleHttp\Psr7\Response)#1084 (6) {
["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
string(2) "OK"
["statusCode":"GuzzleHttp\Psr7\Response":private]=>
int(200)
["headers":"GuzzleHttp\Psr7\Response":private]=>
array(11) {
....
....
....
....
["status"]=>
string(2) "ok"
}
}
我的代码是:
while read ID; do
curl -X -d "http://localhost/new.php?media_id="$ID"&submit=Submit"
done < ~/ids.txt
我还添加了这个:
curl -s -X -d "http://localhost/new.php?media_id="$ID"&submit=Submit" | grep -c 'string(2) "ok"'
输出是1
.我希望它计算所有输出并显示示例Success : 12
答案1
您可以通过以下方式完成此操作grep -c
:
f=0
s=0
while read ID; do
var=$(curl -X -d "http://localhost/new.php?media_id="$ID"&submit=Submit")
grep -q 'string(2) "ok"' <<<"$var" && ((s++))
grep -q 'string(2) "notok"' <<<"$var" && ((f++))
echo "$var"
done < ~/ids.txt
echo "Success : $s"
echo "Failed : $f"