V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
anai1943
V2EX  ›  程序员

Linux shell 获取 curl 返回值

  •  
  •   anai1943 · 2015-04-02 17:47:01 +08:00 · 52235 次点击
    这是一个创建于 3322 天前的主题,其中的信息可能已经有所发展或是发生改变。
    对 shell 语法不熟,有个这样的需求,用 crontab 每天定时执行一次 xxx.shxxx.sh 里面的逻辑是这样的:不断 curl 请求 http://xxx.com/xxx.php ,xxx.php 会输出 1111 和 0000 ,当 xxx.php 输出 1111 时,继续请求 xxx.php ,直到输出 0000 ,xxx.sh 执行结束,请问下这个 xxx.sh 该怎么写?谢谢
    10 条回复    2017-07-21 16:34:21 +08:00
    xhat
        1
    xhat  
       2015-04-02 17:59:17 +08:00
    或许你是想要:
    crontab里面执行 /usr/bin/php *.php
    anai1943
        2
    anai1943  
    OP
       2015-04-02 18:13:13 +08:00
    @xhat 我想用shell来发送http请求,不是用php的curl
    ooxxcc
        3
    ooxxcc  
       2015-04-02 18:17:42 +08:00   ❤️ 1
    这个不是返回值……

    RESULT=1111
    while [[ $RESULT != "0000" ]]; do
    RESULT=$(curl xxxxxxxxx)
    sleep 1
    done
    jyz19880823
        4
    jyz19880823  
       2015-04-02 18:20:05 +08:00
    result=`curl http://xxx.com/xx.php -s`
    chinawrj
        5
    chinawrj  
       2015-04-02 18:22:48 +08:00
    可以输出到stdout, wget是"-",你看一下help。curl是啥。。
    clanned
        6
    clanned  
       2015-04-02 18:25:35 +08:00   ❤️ 2
    这个是你要的
    https://gist.github.com/xdtianyu/aa339f6003604ef6a72f

    xxx.sh文件
    ----

    #/bin/bash

    URL="http://192.168.5.100/xxx.php"

    check() {
    RESULT=$(curl -s http://192.168.5.100/xxx.php)
    echo $RESULT

    if [ "$RESULT" -eq "1111" ] ; then
    echo "again"
    sleep 1
    check
    elif [ "$RESULT" -eq "0000" ] ; then
    echo "exit"
    exit 0
    else
    echo "error"
    fi
    }

    check

    ----

    xxx.php文件模拟

    ----

    <?php
    if (rand()%5==0) {
    echo "1111";
    } else {
    echo "0000";
    }
    ?>
    anai1943
        7
    anai1943  
    OP
       2015-04-02 18:28:49 +08:00
    @ooxxcc 么么哒。。可以了,感谢!
    cevincheung
        8
    cevincheung  
       2015-04-02 18:30:29 +08:00   ❤️ 1
    如果一定要用脚本的话不如用python了。- -#
    wate
        9
    wate  
       2015-04-03 11:45:09 +08:00
    @cevincheung 所言甚是
    ajan
        10
    ajan  
       2017-07-21 16:34:21 +08:00
    @clanned 学习了,正好在折腾一个类似的脚本
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   6260 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:25 · PVG 10:25 · LAX 19:25 · JFK 22:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.