linux - Can't increment a 0-padded number past 8 in busybox sh -


this code using save files camera , name them 0001 onward. camera running busybox, , has ash shell inside.
code based on previous answer charles duffy here.

#!/bin/sh                                                                                                                # snapshot script                                                                                            cd /mnt/0/foto                                                                                                            sleep 1                                                                                                                   set -- *.jpg            # put sorted list of picture namefiles on argv ( number of files on list can requested echo $# )                                                            while [ $# -gt 1 ];  # long there's more one...                                                               shift                 # ...some rows shifted until 1 remains                                                           done                                                                                                                      if [ "$1" = "*.jpg" ];  # if cycle determine if argv empty because there no jpg file present in dir.         #argv set following cmds can start sequence 0 on.                                                                               set -- snapfull0000.jpg                                                                                                  else                                                                                                                     echo "piu' di un file jpg trovato."                                                                                      fi                                                                                                                        num=${1#*snapfull}                     # $1 first row of $#. alphabetical part of filename removed.                                         num=${num%.*}                          # removes suffix after name.                                    num=$(printf "%04d" "$(($num + 1))")   # variable updated next digit , number padded (zeroes added)                        # echoes debug                                                                                  echo "variabile num="$num              # shows number recognized in latest filename                                                                                      echo "\$#="$#                          # displays num of argv variables                                                                                 echo "\$1="$1                          # displays first arg variable                                                                                      wget http://127.0.0.1/snapfull.php -o "snapfull${num}.jpg"  # snapshot requested camera, sequential naming of jpeg file. 

this on cmd line during script operation. manually ran script 9 times, after saving of file snapfull0008.jpg, can see in last lines, files named snapfull0000.jpg.

# ./snap4.sh                                                                                                             variable num=0001                                                                                                       $#=1                                                                                                                     $1=snapfull0000.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0002                                                                                                       $#=1                                                                                                                     $1=snapfull0001.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0003                                                                                                       $#=1                                                                                                                     $1=snapfull0002.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0004                                                                                                       $#=1                                                                                                                     $1=snapfull0003.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0005 $#=1                                                                                                                     $1=snapfull0004.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0006                                                                                                       $#=1                                                                                                                     $1=snapfull0005.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0007                                                                                                       $#=1                                                                                                                     $1=snapfull0006.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0008                                                                                                       $#=1                                                                                                                     $1=snapfull0007.jpg                                                                                                      connecting 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 jpg file found.                                                                                             variable num=0000                                                                                                       $#=1                                                                                                                     $1=snapfull0008.jpg                                                                                                      connecting 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 

what cause of sequence stopping after file number 8?

the problem leading 0s cause number read octal.

in bash, using $((10#$num)) force decimal. thus:

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

to work busybox ash, you'll need strip 0s. 1 way work in busybox ash:

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

see below transcript showing use (tested ash busybox v1.22.1):

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

if shell doesn't support baseline set of parameter expansions required posix, instead end using:

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

...though imply busybox built shell other ash, decision suggest reconsidering.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -