使用shell脚本从文件测试其余webservices时如何获取成功计数,失败计数和失败原因

时间:2017-11-28 08:21:11

标签: windows batch-file curl git-bash

您好我正在使用shell脚本通过多个if条件测试Web服务,使用shell脚本编码我获得成功计数,失败计数和失败原因

success=0
failure=0

if curl -s --head --request DELETE http://localhost/bimws/delete/deleteUser?email=pradeepkumarhe1989@gmail.com | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
echo "DeleteUser is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

if curl -s --head  --request GET http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
 curl -s --head  --request GET http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com > f1.txt
 echo "getUserDetails is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

if curl -s -i -X POST -H "Content-Type:application/json"  http://localhost/bimws/post/addProjectLocationAddress -d '{"companyid":"10","projectid":"200","addresstypeid":"5","address":"1234 main st","city":"san jose","state":"CA","zip":"989898","country":"United States"}' | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
echo "addProjectLocationAddress is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

echo $success Success
echo $failure failure

但我期待从像我有文件web_services.txt的文件测试Web服务,该文件包含我使用shell脚本的所有Web服务如何执行和成功计数,失败计数和失败原因

web_services.txt

所有不同的电话都是删除,获取和发布

http://localhost/bimws/delete/deleteUser?email=pradeepkumarhe1989@gmail.com


http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com



http://localhost/bimws/post/addProjectLocationAddress -d '{"companyid":"10","projectid":"200","addresstypeid":"5","address":"1234 main st"
,"city":"san jose","state":"CA","zip":"989898","country":"United States"}'

1 个答案:

答案 0 :(得分:1)

首先,您当前的代码无法正确处理空行。你需要跳过这些。

您的行已包含shell命令。在他们身上运行卷曲毫无意义。相反,您应该评估这些命令。

然后,您需要修改curl,以便通过添加-f来报告请求是否成功:

FILE=D:/WS.txt
success=0
failure=0
while read LINE; do
    if test -z "$LINE"; then
        continue
    fi
    if eval $(echo "$LINE" | sed 's/^curl/curl -f -s/') > /dev/null; then 
        success=$((success+1))
    else
        echo $LINE >> aNewFile.txt
        failure=$((failure+1))
    fi
done < $FILE
echo $success Success
echo $failure failure