Sunday, January 29, 2017

More Bash Scripts in Windows 10

I basically see this post as something that might extend in the future with more scripts as circumstances warrant. I have written literally dozens, if not hundreds of production Unix/Linux scripts since the mid-90's of all major shells (Bourne, C, and Korn, with a preference for the latter). Examples include, but are not restricted to, customized Oracle database daily status reports and alerts, ERP production concurrent programs (I wrote a process that allowed the feed of price loads for inventory items (including data cleansing, loading spreadsheet data into the database, and updating Oracle Apps base tables)), and dynamic Oracle "hot backup" scripts.

I'm not necessarily a guru in the sense of exploring all of a shell's nuances, and there are different approaches/preferences/solutions to scripting but generally I'm able to devise a simple, functional script to accommodate requirements. I have used emulators like Cygwin and MKS Toolkit in Windows environments. I was pleased to see Windows 10 anniversary edition include Ubuntu shell support, as I mentioned here, and I also described a Bash script I use to generate random quote signature html files I attach to Thunderbird emails.

In this post, I intend to publish over time other Bash scripts designed for practical reasons.

[1/31/17]. In this first example, I was archiving old emails into an encrypted volume. There's a great Export-Import add-on to Thunderbird which allows exports of emails in Thunderbird mail folders in *.eml format. (There are alternative available file types, including text and html files, but I can easily read and/or import archived emails in eml format with the Thunderbird client.)

I ran into a practical issue dealing with the length of the tool-generated eml file names in moving or copying files to encrypted archive volumes. Now of course there are a number of freeware utilities that provide file renaming functionality, but for some reason, my security software didn't like the ones I've installed, and I found them gone, so I decided to write a "meat-and-potatoes" shell script. (In this case it involved Twitter email notifications.)  I didn't really need a context filename, i.e., I simply wanted a "quick and dirty" generated unique filename, e.g., 1.eml, 2.eml for maybe up to a thousand or so emails in the folder. So here's a simple solution where I use a counter i and capture the current working directory in DIR: (Note that I am running this in the local working directory where the files are located.)

 DIR=`pwd`; i=0; ls * | while read fn; do (( i = $i + 1 )); mv "$DIR/$fn" "$DIR/$i.eml"; done