0

Busybox に付属のイーサネット カメラを使用しています。
RS232経由でシングルボードコンピュータが接続されています。SBC は、jpg スナップショットを取得し、それを CF メモリ カードに保存し、順番に名前を付ける (0001、0002 など) ために、カメラに 1 つのコマンドを送信する必要があります。
これは、連続した名前を付けずに、単一のスナップショットを作成するために使用するコードです。

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 は削除されました。これは、検索コマンドがファイル 0004 の前にファイル 0005 をリストしたため、両方の名前が相互に変更され、ファイル 0004 が削除されたためです。)

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

私が探しているのは、SBC によって 1 日に複数回要求できる単一のシェル スクリプトです。これにより、カメラは写真を撮り、保存し、最後に使用した番号に基づいて順番に名前を付けます (最新のファイルは 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 に答える