shell - How can I increment a number in a while-loop while preserving leading zeroes (BASH < V4) -
i trying write bash script downloads transcripts of podcast curl. transcript files have name differs 3 digits: filename[three-digits].txt
- from
filename001.txt
- to....
filename440.txt
.
i store 3 digits number in variable , increment variable in while loop. how can increment number without losing leading zeroes?
#!/bin/bash clear # [...] code handling storage episode=001 last=440 secnow_transcript_url="https://www.grc.com/sn/sn-" last_token=".txt" while [ $episode -le $last ]; curl -x $secnow_transcript_url$episode$last_token > # storage location episode=$[$episode+001]; sleep 60 # don't stress server much! done
i searched lot , discovered nice approaches of others, solve problem, out of curiosity love know if there solution problem keeps while-loop, despite for-loop more appropriate in first place, know range, day come, when need while loop! :-)
#!/bin/bash episode in $(seq -w 01 05); curl -x $secnow_transcript_url$episode$last_token > # ... done
or few digits (becomes unpractical more digits)
#!/bin/bash episode in 00{1..9} 0{10..99} {100..440}; curl -x $secnow_transcript_url$episode$last_token > # ... done
you can use $((10#$n))
remove 0 padding (and calculations), , printf
add 0 padding back. here both put increment 0 padded number in while loop:
n="0000123" digits=${#n} # number of digits, here calculated original number while sleep 1 n=$(printf "%0${digits}d\n" "$((10#$n + 1))") echo "$n" done
Comments
Post a Comment