Issue
The following bash script automatically produces N number of files using i number in the naming according to the given sequence:
prod_parts=30
for i in $(seq 2 $prod_parts); do
prev=$((i - 1))
next=$((i + 1))
printf "something" > ext0"$i".txt
done
which produces 29 filles with the names
ext029.txt ext026.txt ext022.txt ext015.txt ext011.txt ext05.txt
ext030.txt ext023.txt ext019.txt ext016.txt ext09.txt ext06.txt
ext027.txt ext024.txt ext017.txt ext012.txt ext07.txt ext02.txt
ext028.txt ext020.txt ext018.txt ext013.txt ext08.txt ext03.txt
ext025.txt ext021.txt ext014.txt ext010.txt ext04.txt
how could I modify my script to automatically remove ZERO starting from the 10th, which should give me something like:
ext09.txt ext26.txt ext22.txt ext15.txt ext11.txt ext05.txt
ext30.txt ext23.txt ext19.txt ext16.txt ext09.txt ext06.txt
ext27.txt ext24.txt ext17.txt ext12.txt ext07.txt ext02.txt
ext28.txt ext20.txt ext18.txt ext13.txt ext08.txt ext03.txt
ext25.txt ext21.txt ext14.txt ext10.txt ext04.txt
Solution
What probably could solve your problem would be using printf
to format the numbers with the exact number of digits that you need:
prod_parts=30
for i in $(seq 2 "$prod_parts"); do
prev=$((i - 1))
next=$((i + 1))
## print the number $i using at least 2 digits
## padding with 0s if necessary into $suffix
printf -v suffix "%02d" "$i"
printf "something" > ext"$suffix".txt
done
Answered By - Dima Chubarov Answer Checked By - Timothy Miller (WPSolving Admin)