0

我正在使用 Busybox 附带的以太网摄像机。
单板计算机通过 RS232 连接到它。SBC 需要向相机发送单个命令以拍摄 jpg 快照,将其保存到 CF 存储卡并按顺序命名(0001、0002 等)。
这是我用来拍摄单个快照的代码,没有顺序命名:

wget http://127.0.0.1/snap.php -O /mnt/0/snapfull`date +%d%m%y%H%M%S`.jpg

我需要按顺序命名文件。这是我在这里找到的代码,它对已经存在的文件进行顺序重命名,但我注意到当在重命名多个文件后再次执行代码时,交叉重命名会导致文件删除(我在文件时运行代码从 0001.jpg 到 0005.jpg 存在于目录中,并且文件 0004.jpg 被删除,因为 find cmd 在文件 0004 之前列出了文件 0005,因此将两者交叉重命名,文件 0004 被删除。)

find . -name '*.jpg' | awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | dash

我正在寻找的是一个单一的 shell 脚本,它可以由 SBC 每天多次请求,以便相机拍照、保存并根据最后使用的数字按顺序命名(如果最新文件为 0005.jpg,下一张图片将命名为 0006.jpg)。
在我附加的第一行代码中添加这个命名功能会很棒,这样我就可以将它包含在 SBC 可以调用的 sh 脚本中。

4

2 回答 2

1

这是我实际测试的代码,似乎正在工作,基于@Charles 的回答:

#!/bin/sh
set -- *.jpg             # put the sorted list of picture namefiles on argv ( the number of files on the list can be requested by echo $# ) 
while [ $# -gt 1 ]; do   # as long as the number of files in the list is more than 1 ...
  shift                  # ...some rows are shifted until only one remains
done
if [ "$1" = "*.jpg" ]; then   # If cycle to determine if argv is empty because there is no jpg      file present in the dir.
  set -- snapfull0000.jpg     # argv is set so that following cmds can start the sequence from 1 on.
else
  echo "More than a jpg file found in the dir."
fi

num=${1#*snapfull}                     # 1# is the first row of $#. The alphabetical part of the filename is removed.
num=${num%.*}                          # Removes the suffix after the name.
num=$(printf "%04d" "$(($num + 1))")   # the variable is updated to the next digit and the number is padded (zeroes are added) 

wget http://127.0.0.1/snapfull.php -O "snapfull${num}.jpg" #the snapshot is requested to the camera, with the sequential naming of the jpeg file.
于 2014-12-04T18:23:11.887 回答
0

当且仅当您的文件名除了数字部分之外都相同并且数字部分被填充到足以使它们的位数相同时,这将起作用。

set -- *.jpg           # put the sorted list of names on argv
while [ $# -gt 1 ]; do # as long as there's more than one...
  shift                # ...pop something off the beginning...
done
num=${1#*snapfull}                  # trim the leading alpha part of the name
num=${num%.*}                       # trim the trailing numeric part of the name
printf -v num '%04d' "$((num + 1))" # increment the number and pad it out

wget http://127.0.0.1/snap.php -O "snapfull${num}.jpg"
于 2014-12-03T15:38:25.017 回答