在busybox sh中无法将0填充的数字增加到8以上

编程入门 行业动态 更新时间:2024-10-25 06:29:17
本文介绍了在busybox sh中无法将0填充的数字增加到8以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我用来保存相机文件并从0001开始命名的代码.相机正在运行Busybox,并且相机内部装有灰烬外壳. 该代码基于Charles Duffy先前的回答这里.

this is the code I am using to save files from a camera and name them from 0001 onward. The camera is running Busybox, and it has an ash shell inside. The code is based on a previous answer by 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 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 ./p>

This is what I get on the cmd line during the script operation. I manually ran the script nine times, but after the saving of file snapfull0008.jpg, as you can see in the last lines, files are named only 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后序列停止的原因是什么?

What could be the cause of the sequence stopping after file number 8?

推荐答案

问题是前导0导致数字读为八进制.

The problem is that leading 0s cause a number to be read as octal.

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

In bash, using $((10#$num)) will force decimal. Thus:

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

要使用busybox灰,您需要剥离0.做到这一点的一种方法甚至在忙碌的烟灰中也可以使用:

To work with busybox ash, you'll need to strip the 0s. One way to do this which will work even in busybox ash:

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

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

See the below transcript showing use (tested with ash from busybox v1.22.1):

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

如果您的外壳甚至不支持POSIX要求的基线参数扩展集,那么您最终可以使用:

If your shell doesn't support even the baseline set of parameter expansions required by POSIX, you could instead end up using:

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

...尽管这意味着您的busybox是用除灰以外的其他外壳构建的,但我强烈建议您重新考虑.

...though this would imply that your busybox was built with a shell other than ash, a decision I would strongly suggest reconsidering.

更多推荐

在busybox sh中无法将0填充的数字增加到8以上

本文发布于:2023-10-28 08:26:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   增加到   busybox   sh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!