具有文件保存和顺序文件命名的Linux Shell脚本 [英] Linux shell script with file saving and sequential file naming

查看:233
本文介绍了具有文件保存和顺序文件命名的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 

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

c $ c> find。名称 * .jpg | awk‘BEGIN {a = 0} {printf mv%s%04d.jpg\n,$ 0,a ++}’|破折号



我正在寻找的是一个单脚本,可以由SBC每天多次请求,因此摄像机拍摄图片,保存并命名,并根据使用的最后一个编号(如果最新文件为0005.jpg,下一张图片将命名为0006.jpg)。

在我附加的代码的第一行中添加此命名功能非常好,这样我就可以将其包含在SBC可以调用的sh脚本中。

解决方案方案

仅当您的文件名(除了数字部分)完全相同且数字部分被充分填充以使它们都具有相同的数字位数时,此方法才有效。

  set-* .jpg#将名称的排序列表放在argv 
上,而[$#-gt 1];做#只要有不止一个...
移位#...开始时弹出一些东西...
完成
num = $ {1#* snapfull}#修剪
num = $ {num%。*}的前导字母部分#修剪名称
的尾随数字部分printf -v num'%04d' $(((num + 1))) #增加数字并将其填充

wget http://127.0.0.1/snap.php -O snapfull $ {num} .jpg


I am working with a Ethernet camera that comes with Busybox.
A single board computer is connected to it through RS232. The SBC needs to send a single command to the camera in order to take a jpg snapshot, save it to a CF memory card and name it in a sequential order (0001, 0002 etc..).
This is the code I use to take a single snapshot, without sequential naming:

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

I need the files to be named sequentially. This is the code I found here that does a sequential renaming for files already existing, but I noted that when the code is executed once more after the renaming of multiple files, cross-renaming can lead to file deletion (I ran the code when files from 0001.jpg to 0005.jpg were present in the dir, and file 0004.jpg was deleted because the find cmd listed file 0005 before file 0004, so it cross-renamed both and file 0004 was deleted.)

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

What I am looking for, is a single shell script that can be requested multiple times per day by the SBC, so that the camera takes the picture, save it and name it in a sequential order, based on the last number used (if the latest file is 0005.jpg, next picture will be named 0006.jpg).
It would be great to add this naming capability in the fisrt line of code I attached, so that I can include it in a sh script that can be called by the SBC.

解决方案

This will work if and only if your filenames are all identical except for the numeric part, and the numeric part is padded enough that they're all the same number of digits.

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"

这篇关于具有文件保存和顺序文件命名的Linux Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆