具有文件保存和顺序文件命名的Linux shell脚本

我正在使用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 

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

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

我正在寻找的是一个单一的shell脚本,可以每天由SBC多次请求,以便摄像机拍摄照片,保存并按照最后使用的编号顺序命名(如果最新的文件是0005.jpg,下一张图片将被命名为0006.jpg)。
将这个命名function添加到我附加的代码行中是非常好的,这样我就可以将它包含在一个可以被SBC调用的sh脚本中。

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

 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" 

这是我实际测试的代码,似乎正在工作,基于@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.