2013-03-14 22 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

相关问题