2014-12-05 8 views
0

이것은 카메라에서 파일을 저장하고 0001 이후부터 이름을 지정하는 데 사용하는 코드입니다. 카메라가 Busybox를 실행 중이고 내부에 재 껍질이 있습니다.
코드는 Charles Duffy here의 이전 답변을 기반으로합니다.busybox sh에서 0이 채워진 숫자를 8을 초과 할 수 없습니다.

#!/bin/sh                            
# Snapshot script                       
cd /mnt/0/foto                           

sleep 1                             

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 there's more than one...                
    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.   #argv is set so that following cmds can start the sequence from 0 on.                    
set -- snapfull0000.jpg                         
else                              
echo "Piu' di un file jpg trovato."                      
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) 

         # echoes for debug                     
echo "variabile num="$num    # shows the number recognized in the latest filename                      
echo "\$#="$#       # displays num of argv variables                     
echo "\$1="$1       # displays the first arg variable                     


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. 

이것은 스크립트 작업 중 cmd 줄에 표시됩니다. 수동으로 스크립트를 9 번 실행했지만 파일 저장 후 snapfull0008.jpg 마지막 줄에서 볼 수 있듯이 파일의 이름은 snapfull0000.jpg입니다.

# ./snap4.sh                            
variable num=0001                          
$#=1                              
$1=snapfull0000.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:22 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0001.jpg  100% |*******************************| 246k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0002                          
$#=1                              
$1=snapfull0001.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:32 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0002.jpg  100% |*******************************| 249k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0003                          
$#=1                              
$1=snapfull0002.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:38 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0003.jpg  100% |*******************************| 248k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0004                          
$#=1                              
$1=snapfull0003.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:43 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0004.jpg  100% |*******************************| 330k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0005 
$#=1                              
$1=snapfull0004.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:51 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0005.jpg  100% |*******************************| 308k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0006                          
$#=1                              
$1=snapfull0005.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:55 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0006.jpg  100% |*******************************| 315k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0007                          
$#=1                              
$1=snapfull0006.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:22:59 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0007.jpg  100% |*******************************| 316k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0008                          
$#=1                              
$1=snapfull0007.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:23:04 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0008.jpg  100% |*******************************| 317k --:--:-- ETA           
# ./snap4.sh                            
More than a jpg file found.                        
variable num=0000                          
$#=1                              
$1=snapfull0008.jpg                          
Connecting to 127.0.0.1 (127.0.0.1:80)                     
127.0.0.1 127.0.0.1 - [05/Dec/2014:20:23:10 +0000] "GET /snapfull.php HTTP/1.1" 302 0 "-" "Wget"       
snapfull0000.jpg  100% |*******************************| 318k --:--:-- ETA 

파일 번호 8 이후에 시퀀스가 ​​중지 될 수있는 원인은 무엇입니까?

답변

6

문제는 앞에 0을 붙이면 숫자가 8 진수로 읽혀집니다.

bash에서는 $((10#$num))을 사용하면 10 진수가됩니다. 따라서 :

num=$(printf "%04d" "$((10#$num + 1))") 

는 비지 박스 재 작동하려면 0의를 제거해야합니다. 심지어 비지 박스 재에서 작동하는이 할 수있는 한 가지 방법 :

while [ "${num:0:1}" = 0 ]; do 
    num=${num:1} 
done 
num=$(printf '%04d' "$((num + 1))") 

사용을 보여주는 아래의 사본을보고을 (비지 박스의 v1.22.1에서 재 테스트) :

$ num=0008 
$ while [ "${num:0:1}" = 0 ]; do 
> num=${num:1} 
> done 
$ num=$(printf '%04d' "$((num + 1))") 
$ echo "$num" 
0009 

쉘이 POSIX에 필요한 기본 확장 매개 변수 세트를 지원하지 않으면 대신 다음을 사용하여 끝낼 수 있습니다.

num=$(echo "$num" | sed -e 's/^0*//') 
num=$(printf '%04d' "$(($num + 1))") 

... 이것은 귀하의 비지 박스가 애쉬 이외의 쉘로 제작되었음을 의미하지만, 재검토를 강력히 제안하는 결정입니다.

+0

_while 행에 대해 잘못된 대체 오류가 발생합니다. '# while [ "$ {num : 0 : 1}"= 0]; do /bin/sh : 구문 오류 : 잘못된 대체 ' – Rick

+0

어떤 셸입니까? (Busybox에는 몇 가지가 포함되어 있는데, 어떤 것을 테스트했는지 지정했습니다). –

+0

충분히 빈혈/하위 POSIX 셸을 사용하는 경우'num = $ (echo "$ num"| sed -e 's/^ 0 * //')'와 같은 무언가를 할 필요가 있다고 생각합니다. –