I'm beginner at shell scripting and I'm having a little troubles with my code.
My goal is to have control on the size and the format of the numbers in my array "mystring"
#!/bin/bash
mystring="333,4444,333,333,4444,333,333"
expectedValue=4
addLeftZero () {
if [ $myCount -eq $expectedValue ]
then
#the number has the correct size
else
#this is where I have to prepend "0" until the number has the expected lenght,
#for example the number "333" will be "0333"
fi
#here i have to return the full array with the mods
}
IFS=',' read -ra ADDR <<< "$mystring"
for i in "${ADDR[@]}"; do
myCount=${#i}
addLeftZero $i
return $i
done
0333,4444,0333,0333,4444,0333,0333
I have used sed command, but it seems like I need to edit it in a file, not directly in my code.
What command can I use to format my strings? Do am I using the function correctly? Do I have visibility of my variables? Do you know a better way to reach my goal?
Thanks in advance!