2015-07-19 66 views
-1

我想製作一個升級filebot jar文件的「智能」腳本,但我遇到了一些問題,第一個問題是如果我使用curl比較在線和本地文件,有時候我收到的http_code是304,有時是200,這不是非常有用! 首先,我產生一個時間戳文件,stat -c %Y ...但問題不在這裏,除了這一切的作品,有時返回200,有時304,我覺得這不是一個代碼問題,但也許有人知道一個更好的方式來做到這一點curl http_code返回不同​​的值

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} 

只是因爲我認爲這是一個「正常」的錯誤,我不能修復它,我鑫卡特趕HTTP_CODE,如果是200做一些塞入,如果是其他的東西,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是空的,所以任務失敗!

任何想法或建議?

Ĵ


好TNX rednoah,你怎麼看待這個?我試圖讓「直覺」,所以如果下載的文件是不正確或其他不做更新

#!/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 
+0

可以減少這到一個MWE? –

+0

對不起,我不知道什麼是MWE –

+0

https://en.wikipedia.org/wiki/Minimal_Working_Example –

回答

0
+0

好吧,也許我找到了第二個問題的解決方案,我可以在if語句中分配一個變量if [「$ {HTTPCODE = $(curl -s -k $ FILEBOTURL -z」$(date --rfc-2822 -d @ $(<$ FILEBOTTIMESTAMP))「-o $ TMPFILE -L -w%{http_code}) }「-eq 200];那麼這是正確的嗎?或者,如果作品是一種奇蹟?! –