I need to write a script that parses a list of folders and creates some "missing" folder names.
The background to this is that I am using imapsync to sync a google imap mailbox to a local ubuntu machine's maildir running dovecot. The script works pretty good with one issue:
Sometimes I end up with a sequence of folders in the maildir that look like this:
The issue with the example folder list above is that some of the "parent" folders in the tree do not exist. In this case, the following folders *should* exist, but haven't been created
The reason why they haven't been created is because technically no messages were actually inside those labels in Google. This doesn't *actually* cause any real issues with dovecot, but it makes the folders not display correctly in SquirrelMail.
So I'm trying to write a bash script which will create the above folders. Basically the algorithm for what it needs to do is this:
1) Loop through all the "dot" folders (folders beginning with .) in /home/jsmith/maildir/
2) Skip "." and ".."
3) For every folder it finds:
a) Parse the string and determine a list of "parent" folder names, e.g. if the folder name is
The parent names that need to be examined are
b) For every "parent name" calculated in a), check if it exists. If it doesn't, create it.
No need for optimizations - it can do this to *every* folder it finds and it can repeat a lot of work checking folders that already exist over and over. The script will run only once or twice a day, so no big deal.
I'm pretty good with Bash, but the string parsing and looping through folders (3 and 3a) has got me stumped. There's a few important issues:
- Folders CAN have spaces in them, so a basic FOR loop won't work. At the very least, the IFS variable needs to be set to delimit on \n instead of whitespace.
- I need to loop through all folders beginning with a dot, but SKIP "." and ".."
- I have no idea how to parse the string using bash
Can anyone help me write this script?
Doesn't necessarily need to be in Bash - it can be in Perl if that helps... but I think Bash is probably the best candidate.