• 请不要在回答技术问题时复制粘贴 AI 生成的内容
anai1943
V2EX  ›  程序员

Linux shell 获取 curl 返回值

  •  
  •   anai1943 · Apr 2, 2015 · 53162 views
    This topic created in 4135 days ago, the information mentioned may be changed or developed.
    对 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 replies    2017-07-21 16:34:21 +08:00
    xhat
        1
    xhat  
       Apr 2, 2015
    或许你是想要:
    crontab里面执行 /usr/bin/php *.php
    anai1943
        2
    anai1943  
    OP
       Apr 2, 2015
    @xhat 我想用shell来发送http请求,不是用php的curl
    47jm9ozp
        3
    47jm9ozp  
       Apr 2, 2015   ❤️ 1
    这个不是返回值……

    RESULT=1111
    while [[ $RESULT != "0000" ]]; do
    RESULT=$(curl xxxxxxxxx)
    sleep 1
    done
    jyz19880823
        4
    jyz19880823  
       Apr 2, 2015
    result=`curl http://xxx.com/xx.php -s`
    chinawrj
        5
    chinawrj  
       Apr 2, 2015
    可以输出到stdout, wget是"-",你看一下help。curl是啥。。
    clanned
        6
    clanned  
       Apr 2, 2015   ❤️ 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
       Apr 2, 2015
    @ooxxcc 么么哒。。可以了,感谢!
    cevincheung
        8
    cevincheung  
       Apr 2, 2015   ❤️ 1
    如果一定要用脚本的话不如用python了。- -#
    wate
        9
    wate  
       Apr 3, 2015
    @cevincheung 所言甚是
    ajan
        10
    ajan  
       Jul 21, 2017
    @clanned 学习了,正好在折腾一个类似的脚本
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1105 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 18:17 · PVG 02:17 · LAX 11:17 · JFK 14:17
    ♥ Do have faith in what you're doing.