BASHing Visual Basic Out of the Picture

RSS Feed - Comments
Author: Jacob Barkdull on Monday, June 28 2010

Awhile ago, two family members of mine were programing something for a simple task, they chose to do it in Visual Basic as the program was going to be running in a Windows environment anyway. The task was having a simple program that would run in the background -- or ran when needed -- to print a specific file (or multiple files) whenever the file(s) changed, specifically, whenever the file was modified. This was being done on a Windows system so file changes related to permissions, ownership, etc, didn't apply.

There were small problems getting it to work properly (as this can happen programing anything.) Seeing the code, everything looked like it would work, but it didn't, not ever having much experience with Visual Basic -- I only learned a few things, and they were during my last year using Windows, this was around three years ago) I could only offer a few guesses related to the errors it was throwing. The code reminded me of Python, and the errors reminded me of C++, C#, and Perl mixed together, they were not at all useful.

When they first started working on it, I already had the code layout to accomplish such a task in my head as PHP, but I wasn't quite sure how I would run the PHP code locally on a Windows system let alone print to a local printer using PHP.

That's when it hit me, BASH!

No, nothing bashed me in the head, I am referring to GNU BASH. I knew that it would be an easy task in BASH, a couple variables, a simple "while loop", an "if", and the print command "lpr". Here's what I got:

#!/bin/bash
moddate=$(stat -c %y workdoc.txt)
moddate=${moddate% *}
while [ 1 ]
do
 sleep 1
 newmoddate=$(stat -c %y workdoc.txt)
 newmoddate=${newmoddate% *}
 if [ "$newmoddate" != "$moddate" ]
  then
   lpr workdoc.txt
   moddate=$newmoddate
 fi
done

What I noticed was: this is near 50% smaller than Visual Basic's source. And BASH automatically runs in the background of a graphical environment -- unless it's executed from an open graphical terminal (such as gnome-terminal) -- at least in GNOME it works this way.

I'll note that I didn't find a way to use it conveniently on Windows, but it would be easy enough to run the code through SHC and compile the C source code for Windows. Despite the title, I eventually helped finish the program with a simple BATCH script, so both BASH and Visual Basic were out of the picture.

I write about this because I find it interesting, and because it's been a long time since I've written any code that someone might have a real-world or business related use for. So... It's simple! It's fast! Use it if you want :)
What Is This Place?
TildeHash is a website for Tech articles revolving around Free Software and Unix/Unix-like operating systems, written by Jacob Barkdull and various contributors, respectively. Meaning, Free Software (or Open Source); and GNU/Linux (or simply Linux), BSD, OpenSolaris, or Haiku; respectively. The main goal of TildeHash is to be different -- the name alone is a little different (explanation here) -- but to do it in a useful way.

TildeHash is about discussing Free Software topics that are beneficial to our community, topics that are largely not discussed nor shown. "Free Software" is also often called "Open Source Software". In practice the requirements are identical, although because the term "open" doesn't call to mind freedom, it misses the point.

List of Articles >>

Comments powered by Disqus