Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在bash脚本中解析命令的结果_Bash - Fatal编程技术网

如何在bash脚本中解析命令的结果

如何在bash脚本中解析命令的结果,bash,Bash,我想运行一个命令,然后检查字符串“200OK”的结果 到目前为止,我有以下bash脚本: for i in $(seq 1 50) do printf "i is: $i\n" RESULTS="$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 | grep -ne "200 OK")" if [ $RESULTS -eq

我想运行一个命令,然后检查字符串“200OK”的结果

到目前为止,我有以下bash脚本:

for i in $(seq 1 50)
   do
        printf "i is: $i\n"
        RESULTS="$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 | grep -ne "200 OK")"
        if [ $RESULTS -eq 0 ]; then
                echo "GET failed with $RESULTS"
                break
        else
                echo "we're good"
        fi

done
当我运行上述脚本时,我得到以下结果:

i is: 1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    85  100    85    0     0   3944      0 --:--:-- --:--:-- --:--:--  4047
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator
we're good
i is: 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    85  100    85    0     0   3293      0 --:--:-- --:--:-- --:--:--  3400
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator
we're good
运行curl命令返回的输出如下所示:

HTTP/1.1 200 OK
Date: Tue, 10 Feb 2015 13:21:18 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Content-Length: 85
Content-Type: application/json; charset=utf-8

{"status":pass,"widgetinfo":"{\"widgetname\":\"the best widget\",\"price\":\"100.00\"}"}
基于上述结果,我有几个问题:

  • 为什么bash脚本要打印关于总时间、速度等的统计数据,以便从curl中获得结果
  • 我应该如何修改脚本,以便除非脚本失败,否则我只希望看到计数器值(I)和指示通过的状态。如果失败,我希望脚本退出,并提供curl命令返回内容的详细信息
  • 为什么我会出错
  • gettest.sh:8:[:1:HTTP/1.1:意外的运算符


    有什么提示吗?谢谢!

    如果您不想打印统计数据,请使用
    -s
    ,顺便说一句,如果出现错误4xx和5xx,您可以使用
    -f
    以非零退出

    有效请求:

    curl -sf example.com -o /dev/null  ; echo $?
    0
    
    404未找到:

    curl -sf example.com/test ; echo $?
    22
    
    如果需要错误消息
    -S

    curl -sfS example.com/test ; echo $?
    curl: (22) The requested URL returned error: 404 Not Found
    22
    
    从手册中:

    man curl | grep '\-[Ssf],' -A 3
           -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 a HTTP server fails to deliver a document, it returns an HTML document stating so  (which  often  also
                  describes why and more). This flag will prevent curl from outputting that and return error 22.
    --
           -s, --silent
                  Silent or quiet mode. Don't show progress meter or error messages.  Makes Curl mute.
    
           -S, --show-error
                  When used with -s it makes curl show an error message if it fails.
    

    因此,您的脚本可以如下所示:

    RESULTS="$(curl -sSfi -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 -o /dev/null 2>&1)"
    if [ $RESULTS -eq 0 ]; then
            echo "GET failed with $RESULTS"
            break
    else
            echo "we're good"
    fi
    
    RESULTS=$(cmd)
    将命令的输出分配给结果,而不是命令返回的值。如果要检查输出中是否出现特定字符串,通常使用grep(它将消耗命令的输出,因此将不可用):

    如果要检查结果,只需执行以下操作:

    if cmd; then echo cmd succeeded ...
    else echo cmd failed ...
    fi
    

    请注意,在后一种情况下,cmd的输出仍将写入脚本的stdout,因此您可能需要重定向。

    您可以使用此命令获取http\u代码

    curl -s -o /dev/null -w "%{http_code}" http://www.wikipedia.org
    
    此命令在标准输出中打印http代码

    curl -s -o /dev/null -w "%{http_code}" http://www.wikipedia.org