Curl http_code返回不同​​的值

时间:2015-07-19 17:18:30

标签: bash variables curl

我试图做一个聪明的"升级filebot jar文件的脚本但我遇到了一些问题,第一个问题是,如果我使用curl比较在线文件和本地文件,有时我收到的http_code是304,有时是200,这不是很有帮助! ! 首先我使用stat -c %Y ...生成一个时间戳文件但问题不在这里,所有工作除此之外,有时返回200有时304,我认为这不是代码问题,但也许有人知道更好的方法来做到这一点

curl -s -k http://sourceforge.net/projects/filebot/files/filebot/HEAD/FileBot.jar -z "$(date --rfc-2822 -d @$(</opt/usr/share/fbot/FileBot.jar.timestamp))" -o /opt/usr/share/filebot/FileBot.jar -L -w %{http_code}

仅仅因为我认为这是一个正常的&#34;错误,我无法解决它,我想抓住http_code,如果是200做一些stuf,如果是304其他东西和其他......

但我无法在不重新下载文件的情况下向http_code声明变量

我有这段代码

        if [[ "$(curl -s -k $FILEBOTURL -z "$(date --rfc-2822 -d @$(<$FILEBOTTIMESTAMP))" -o $TMPFILE -L -w %{http_code})" == "200" ]]; then
        echo "yep!"
        get_timestamp
    else
        echo "I can't download the FileBot jar, try again later"
        exit
    fi

正如您在理论上所看到的,如果http_code为200,则下载并升级文件,仅当http_code为304时,文件已经更新。所以我的想法是卷曲一次我需要并将http_code分配给一个varialbe,然后如果变量是200等等......

我试图这样做

if [[ "HTTPCODE=$(curl -s -k $FILEBOTURL -z "$(date --rfc-2822 -d @$(<$FILEBOTTIMESTAMP))" -o $TMPFILE -L -w %{http_code})" == "200" ]]; then

但是HTTPCODE是空的,所以任务失败了!

有任何想法或建议吗?

Ĵ

Ok tnx rednoah,您如何看待这个?我试图让它变得直观&#34;因此,如果下载文件不正确或其他不进行更新

#!/bin/sh

set -e

CAT=$(which cat)
GREP=$(which grep)
FIND=$(which find)
RM=$(which rm)
MV=$(which mv)
ECHO=$(which echo)
FILEBOTPROGRAM=$(which filebot)
FILEBOTJAR=$($CAT $FILEBOTPROGRAM | $GREP -oP '(?<=-jar ).*(?= )')
FILEBOTDIR=$(dirname "${FILEBOTJAR}")
TMPFILE="$FILEBOTDIR/fb.jar"
FILEBOTTIMESTAMP="$FILEBOTJAR.timestamp"
FILEBOTURL="http://sourceforge.net/projects/filebot/files/filebot/HEAD/FileBot.jar"

#CLEAN
if [[ -f $TMPFILE ]]; then
    $RM $TMPFILE
fi

#FUNCTIONS
get_timestamp () {
    $ECHO -n "Info: Generating new timestamp for the jar ... "
    stat -c %Y $FILEBOTJAR > $FILEBOTTIMESTAMP
    $ECHO "done, happy file renaming with your upgraded FileBot"
    exit
}

update_check () {
    $ECHO -n "Info: Let's check if the Head of FileBot is updated ... "
    if [[ -f $FILEBOTTIMESTAMP ]]; then
        HTTPCODE=$(curl -s -k $FILEBOTURL -z "$(date --rfc-2822 -d @$(<$FILEBOTTIMESTAMP))" -o $TMPFILE -L -w %{http_code})
        if [[ $HTTPCODE == "200" ]]; then
            if [[ $($FIND $TMPFILE -type f -size +25M 2>/dev/null) ]]; then
                $MV $TMPFILE $FILEBOTJAR
                $ECHO "yep, downloaded and upgraded!"
                get_timestamp
            fi
            $ECHO "No new version of FileBot are available"
            exit
        fi
        if [[ $HTTPCODE == "304" ]]; then
            $ECHO "No new version of FileBot are available"
            exit
        fi
        if [[ $HTTPCODE == "404" ]]; then
            $ECHO "I can't download the FileBot jar, try again later"
            exit
        fi
    else
        $ECHO "I can't know if there's an update, so I'll perform a full update and I take note of this"
        HTTPCODE=$(curl -s -k $FILEBOTURL -o $TMPFILE -L -w %{http_code})
        if [[ $HTTPCODE == "200" ]]; then
            if [[ $($FIND $TMPFILE -type f -size +25M 2>/dev/null) ]]; then
                $MV $TMPFILE $FILEBOTJAR
                $ECHO "jar, downloaded and upgraded!"
                get_timestamp
            fi
            $RM $TMPFILE
            $ECHO "I can't download the FileBot jar, try again later"
        fi
    fi
}

recovery() {
    if [[ -f $TMPFILE ]]; then
        $RM $TMPFILE
    fi
    $ECHO "yep but don't works very well, I'll try to fix it! Please be patient"
    $ECHO ""
    $RM $FILEBOTJAR
    HTTPCODE=$(curl -s -k $FILEBOTURL -o $TMPFILE -L -w %{http_code})
    if [[ $HTTPCODE == "200" ]]; then
        if [[ $($FIND $TMPFILE -type f -size +25M 2>/dev/null) ]]; then
            $MV $TMPFILE $FILEBOTJAR
            $ECHO "jar, downloaded and upgraded!"
            get_timestamp
        fi
        $RM $TMPFILE
        $ECHO "I can't download the FileBot jar, try again later"
    fi
}

#THE SCRIPT
clear

$ECHO ""
$ECHO "============================================================="
$ECHO "               Filebot Auto-Updater script"
$ECHO "============================================================="
$ECHO ""

$ECHO -n "Info: Checking if FileBot jar exist ... "
if [[ -f $FILEBOTJAR ]]; then
    # so the file exist but I don't know if works correctly, so
    $FILEBOTPROGRAM $FILEBOTJAR -version || recovery
    $ECHO "yep and works like a charm, let's check if there are some update"
    $ECHO ""
    update_check
else
    $ECHO "I can't find FileBot, is installed?"
    exit
fi

1 个答案:

答案 0 :(得分:0)