问题
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.
回答1:
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"
回答2:
This is the code I'm actually testing and seems to be working, based on @Charles answer:
#!/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.
来源:https://stackoverflow.com/questions/27275331/linux-shell-script-with-file-saving-and-sequential-file-naming