【问题标题】:Can't increment a 0-padded number past 8 in busybox sh不能在busybox sh中增加一个超过8的0填充数字
【发布时间】:2014-12-05 20:54:54
【问题描述】:

这是我用来保存相机文件并从 0001 开始命名的代码。相机运行 Busybox,里面有一个灰壳。
该代码基于 Charles Duffy here 之前的回答。

#!/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 之后序列停止的原因可能是什么?

【问题讨论】:

    标签: linux shell embedded-linux busybox ash


    【解决方案1】:

    问题是前导 0s 会导致数字被读取为八进制。

    在 bash 中,使用 $((10#$num)) 将强制使用十进制。因此:

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

    要使用busybox ash,您需要剥离0s。即使在busybox ash中也可以使用的一种方法:

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

    请参阅下面显示使用情况的记录(使用busybox v1.22.1 的灰测试):

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

    如果您的 shell 甚至不支持 POSIX 所需的基本参数扩展集,您可以改为使用:

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

    ...虽然这意味着您的 busybox 是用 ash 以外的外壳构建的,但我强烈建议重新考虑这个决定。

    【讨论】:

    • 我收到了 while 行的错误替换错误:# while [ "${num:0:1}" = 0 ]; do /bin/sh: Syntax error: Bad substitution
    • 这是哪个外壳? (Busybox 包括几个;我已经指定了我要测试的那个)。
    • 我想你可能需要做一些糟糕的事情,比如num=$(echo "$num" | sed -e 's/^0*//'),如果使用足够贫血/sub-POSIX shell。
    • 相机正在运行:BusyBox v1.13.3 (2014-06-25 17:39:48 MDT) multi-call binary。许多 AXIS SoC 都应该附带它。
    • 版本是一部分——编译时选项是另一部分。 Busybox 可以被告知在 ash、hush、msh 或 lash 中编译。 ash 完全符合 POSIX(嗯,上游 ash 是;稍微阅读邮件列表,看起来busybox 中性化了一些数学支持以节省空间);其他人(在不同程度上)不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多