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

Linux shell 获取 curl 返回值

  •  
  •   anai1943 · Apr 2, 2015 · 53446 views
    This topic created in 4245 days ago, the information mentioned may be changed or developed.
    对 shell 语法不熟,有个这样的需求,用 crontab 每天定时执行一次 xxx.sh 。xxx.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
    xhat
    Apr 2, 2015
    1
    或许你是想要:
    crontab里面执行 /usr/bin/php *.php
    anai1943
    anai1943
    OP
    Apr 2, 2015
    2
    @xhat 我想用shell来发送http请求,不是用php的curl
    47jm9ozp
    47jm9ozp
    Apr 2, 2015❤️1
    3
    这个不是返回值……

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