2013-03-14 19 views
0

我有一臺IP攝像機,可以將文件ftps文件傳輸到我的SuSE服務器上的某個目錄。bash shell腳本在兩個JPG上運行imgcmp並存儲「不同的」

我試圖寫一個shell腳本來執行以下操作:

for every file in a directory; 
    use image compare to check this file against the next one 
    store the output in a file or variable. 
    if the next file is different then 
     copy the original to another folder 
    else 
     delete the original 
end for 

運行在提示符下產生的:

myserver:/uploads # imgcmp -f img_01.jpg -F img_02.jpg -m rmse > value.txt 
myserver:/uploads # cat value.txt 
5.559730 
5.276747 
6.256132 
myserver:/uploads # 

我知道有加載錯誤的代碼,我得到的主要問題是從腳本執行imgcmp並從中提取一個值,所以請指出顯然的原因,因爲它可能不適合我。

FILES=/uploads/img* 
declare -i value 
declare -i result 
value = 10 
shopt -s nullglob 
# no idea what the above even does # 
# IFS=. 
# attempt to read the floating point number from imgcmp & make it an integer 
for f in $FILES 
do 
    echo "doing stuff w/ $f" 
    imgcmp -f 4f -F 4f+1 -m rmse > value.txt 
    # doesn't seem to find the files from the variables # 
    result= ($(<value.txt)) 
    if [ $result > $value ] ; then 
    echo 'different'; 
    # and copy it off to another directory # 
    else 
    echo 'same' 
    # and delete it # 
    fi 
    if $f+1 = null; then 
    break; 
    fi 
done 
運行上面的時候

,我得到一個錯誤cannot open /uploads/img_023.jpg+1 和做value.txt的貓說明不了什麼,所以所有的文件顯示爲相同。

我知道問題出在哪裏,但我不知道我應該怎麼做才能提取imgcmp的輸出(從腳本中運行),然後將它變成一個變量,我可以將其與。

+0

你有一些錯別字(4f而不是$ f)。你是否嘗試過使用basename來提取文件名,使用sed來提取名稱的數字部分,並重新組合? – 2013-03-14 08:08:00

+0

'如果[$ result> $ value]'不起作用。使用'-gt'代替'>'來比較「大於」。 '>'用於標準輸出重定向;它會創建一個名爲'$ value'的文件,並且'if'測試總是會成功。 – 2013-03-14 08:15:12

+0

感謝Costi,我會放棄並回來。我之前試過-gt,因爲我讀的是它想要的,但它給了我一個錯誤,說它期望別的東西?抱歉不記得是什麼,但我會給它一個去,併發布一些有用的東西回來即'嘿它現在有效',或'我不知道我在做什麼,因爲它說X'。真的很感謝幫助。 Richard – 2013-03-15 09:46:47

回答

1
FILES=/uploads/* 

current= 
for f in $FILES; do 
    if [ -z "$current" ]; then 
    current="$f" 
    continue 
    fi 
    next="$f" 
    echo "<> Comparing $current against $next" 
    ## imgcmp will return non-0 if images cannot be compared 
    ## and print an explanation message to stderr; 
    if result=$(imgcmp -f $current -F $next -m rmse); then 
    echo "comparison result: " $result 
    ## Checking whether the first value returned 
    ## is greater than 10 
    if [ "$(echo "$result" | awk '$1 > 10 {print "different"}')" = "different" ]; then 
     echo 'different'; 
     # cp -v $current /some/other/folder/ 
    else 
     echo 'same' 
     # rm -v $current 
    fi 
    else 
    ## images cannot be compared... different dimensions/components/... 
    echo 'wholly different' 
    # cp -v $current /some/other/folder/ 
    fi 
    current="$next" 
done 
+0

謝謝Costi,你是明星。我一直在玩w/sed awk,並且整個上午都停下來,要麼輸錯語法,要麼輸入完全相同的字符(即輸出三行浮點數)。午餐後我會給你一個補丁,然後回到你身邊。我認爲我現在必須成爲數字,我知道我想要做什麼的邏輯,但我的舊大腦無法解釋它;) – 2013-03-15 23:49:04

+0

您好Costi,這是與邏輯的點,它作品是一種享受,但是......我沒有具體說明「不同」是什麼意思。當imgcmp比較同一事物在兩個不同時間拍攝的兩張照片時,它傾向於給出三個值大約爲3.something到6.something,所以我想比較$ result的第一個值和10/10.0以確定如果它是'有趣的'。我早些時候試圖做到這一點,但似乎我太模糊了。感謝您的幫助和大腦的貸款,非常感謝。 – 2013-03-16 02:55:16

+0

所以你的測試應該是:'如果firstValue> 10'?我會編輯我的答案... – 2013-03-16 09:49:52

相關問題