How can I increment a number in a while-loop while preserving leading zeroes (BASH < V4)

℡╲_俬逩灬. 提交于 2019-11-29 16:24:33

You can use $((10#$n)) to remove zero padding (and do calculations), and printf to add zero padding back. Here are both put together to increment a zero padded number in a while loop:

n="0000123"
digits=${#n} # number of digits, here calculated from the original number
while sleep 1
do
    n=$(printf "%0${digits}d\n" "$((10#$n + 1))")
    echo "$n"
done

for ep in {001..440} should work.

But, as you want a while loop: let printf handle leading zeroes

while (( episode <= last )); do
    printf -v url "%s%03d%s" $secnow_transcript_url $episode $last_token
    curl -X GET $url > # storage location
    (( episode++ ))
    sleep 60 # Don't stress the server too much!
done

Will this do?

#!/bin/bash

i=1
zi=000000000$i
s=${zi:(-3)}
echo $s
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!