Alan
asked on
Linux - Bash Script - Create index with leading zeros * Efficiently *
Hi All,
I am trying to write a bash script, and I am stuck trying to work out how to give an index leading zeros without slowing everything down too much.
In my script I have an index ($I) which is incrementing, and I am naming backup files with it thus:
001
002
...
099
100
101
I figured I would add another variable to take the current value of $I, convert it to "000" format, and then use that variable to name the zip file something like this:
Is there a better / more efficient option?
Thanks,
Alan.
I am trying to write a bash script, and I am stuck trying to work out how to give an index leading zeros without slowing everything down too much.
In my script I have an index ($I) which is incrementing, and I am naming backup files with it thus:
zip "Backup-$I.zip" Path/To/Files
At the risk of being pedantic (never!), my problem is that I would like to always have the filenames use three digits, it just looks kind of, well, untidy otherwise:001
002
...
099
100
101
I figured I would add another variable to take the current value of $I, convert it to "000" format, and then use that variable to name the zip file something like this:
Padded=$(printf "%03d" $I)
zip "Backup-$Padded.zip" Path/To/Files/
However, this seems to slow things to a crawl. I'm not sure why, but I am guessing that it is creating a new shell instance each time it goes through or something?Is there a better / more efficient option?
Thanks,
Alan.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
2) Note #1 is unlikely because, running zip will be so slow compared with running printf... the printf load time... will be lost in the noise of the zip operation.
3) Try @simon3270's fix + if this fix makes a difference, likely something else is amiss...