0

我有一个 IP 摄像机,可以将文件通过 ftps 传输到我的 SuSE 服务器上的一个目录。

我正在尝试编写一个 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 ,并且执行 cat of value.txt 没有显示任何内容,因此所有文件都显示为相同。

我知道问题出在哪里,但我不知道我实际上应该做什么来提取 imgcmp 的输出(从脚本中运行),然后将它放入一个我可以比较的变量中。

4

1 回答 1

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
于 2013-03-14T09:15:14.070 回答