linux - 如何使用 bash 确保 user & pass 在 curl 中是正确的

标签 linux bash curl stdout

我写了下面的脚本:

#!/bin/bash
if [ $# -ne 1 ];then
   echo "Usage: ./script <input-file>"
   exit 1
fi

while read user pass; do
curl -iL --data-urlencode  user="$user" --data-urlencode password="$pass" http://foo.com/signin 1>/dev/null 2>&1
   if [ $? -eq 0 ];then
      echo "ok"
   elif [ $? -ne 0 ]; then
      echo "failed"
   fi
done < $1

问题:

每当我运行错误的用户并传递结果对我来说都是ok...

如何确定我的参数是否正确?

谢谢

最佳答案

这是因为您正在从 curl 命令获取输出。使用随机用户/密码输入该命令得到这个:

$ curl -iL --data-urlencode  user=BLAHBLAH --data-urlencode password=BLAH http://foo.com/signin 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.5
Date: Wed, 19 Feb 2014 05:53:36 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.foo.com/signin
. . .
. . .
<body>
  <!-- This file lives in public/500.html -->
  <div class="dialog">
    <h1>We're sorry, but something went wrong.</h1>
  </div>
</body>
</html>

因此,

$ echo $?
0

但是把url修改成垃圾:

$ curl -iL --data-urlencode  user=BLAHBLAH --data-urlencode password=BLAH http://foof.com/signin 
curl: (6) Could not resolve host: foof.com
$ echo $?
6

关于linux - 如何使用 bash 确保 user & pass 在 curl 中是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871985/

相关文章:

linux - 在 bash 中为文件的每一行添加一个数字

bash - 使用管道读取文件,运行脚本并写入同一文件

php - "curl -v --noproxy localhost"在 php 中的等效项是什么?

libcurl 可以用于发出多个并发请求吗?

python - 定影器替代品?我想知道访问文件的所有进程

linux - 脚本中的命令仅运行一次,即使脚本被执行多次

bash - 将变量从 bash 脚本传递到 makefile

php - CURLOPT_USERPWD 数据在通过 ssl 传输时是否安全?

linux - 找不到 NGINX 404 但文件存在

python - 使用无缓冲 shell 重定向时,stdout 和 stderr 不会进入文件?