Bash script, filenames with spaces

Sometimes these things can be really annoying. Anyhow, good hint here:
Handling filenames with spaces in a bash for or while loop.
Helps loop over filenames which include spaces. Like most normal people do, nowadays.
I also got caught trying to extract substrings. We had the date embedded in the name of some backup files. The substring operator looks like this:
${file:19:8}
Get a substring, starting as position 19, 8 chars long.
I wanted from the end, so you make the position negative. But you have to use parens, otherwise it looks like the default-val operator
${file:(-19):8} and not ${file:-19:8}
Then I found that the year started with a zero, so it was getting treated as octal, and 08 is not a valid octal number. So I ended up with this to extract the 2 digit year into a integer var:
# 10# in front says 'use base 10', because the 08 was being interpreted as octal.
let file_year="10#${file:(-19):2}"
# get days by asking 'date' to convert the date string for us.
let file_days="10#$(date --date=${file:(-19):8} +%j)"
Just for your edification, here's how the end of that backup filename was generated:
suffix=$(date +%y-%m-%d_%H%M%S)

# add unique suffix (mbk = mirror backup)
suffix="_"$suffix".mbk"
All perfectly clear?

Comments

Popular posts from this blog

Inno Setup custom data directory location

OpenEmbedded Angstrom for Advantech PCM-9375

Inno Setup MSVC vcredist without bothering your users