I have been writing scripts to help manage the systems I administer for a very long time now. In fact one of the first open source applications I published back in 1998 was MySqlBackUp. MSBU was a simple bash shell script that basically simplified backing up of my web servers‘ MySql databases. However since I only wanted to write the script once and crontab is I wrote the script to be somewhat adaptive. Meaning that I did not want to edit the script every time someone added another database.
I know many out there are turned off by the simplicity of bash and will immediately jump into perl, python, ruby or even php but I honestly feel that you are overlooking elegance of bash’s simple design. Advanced shell scripting especially in bash is almost always a learning experience, but one that I think is absolutely essential to better understanding the system architecture. Be that as it may I am not here to tout the merits of shell programming with bash. In stead I would like discuss some advanced scripting topics.
Obviously if you can write a script to perform a specific function or tasks automatically then the client does not have to really get involved. However sometimes a shell script isn’t exactly the right venue for your client’s project because there need to be some sort of interaction. Let’s be honest not all users are created equal some, not matter how much training you give, can not handle even a second on the command line. I mean every sysadmin has seen the look of horror descend upon a users face as you open a terminal. It is rare that I have heard users gasp in awe at the terminal. Although a few times I did hear a user utter I had no idea that was even there.
Recently one of my clients needed a solution to simplify the data packaging and transmittal from a satellite office to the central office. We investigated all of the usual suspects quickly ruling out things like file shares, ftp services and even email because of attachment size limits. My client wanted something so simple an intern monkey with almost no training could do it. So using blib as the foundation I wrote a script to bundle the files in question and transmit the bundle to the destination via ssh. Obviously this was not user proof and I would have to work on something a bit more simple but the proof of concept was enough to get the client to sign off on doing some more heavy programming.
I decided that the absolute easiest option would be to create a drag and drop input driven script. The change make the script take argument input was relatively simple I added the following code snippet to the script and set the necessary variables:
if [[ ${1+isset} = isset ]]; then FILE=${1} FILENAME=$(basename "${FILE}") DIR=$( dirname "${FILE}") pushd "${DIR}" warpFileOut "${FILENAME}" else warpFileList fi
All that this snippet does is verify the argument passed and attempt to explode the file name out from the directory path. If there is no argument passed then it simply lists the files available on the destination server. At this point the user still needs to enter a command like warpfile MyFile.report on the command line but we are now one step closer to our goal.
At this point I needed to riddle out how to turn a bash shell script into a application that supports drag and drop. On many other UNIX based systems like PC-BSD it is a simple task, Linux and even Windows make this relatively simple as well. Unfortunately Mac OS X is not as easy which is perplexing for a UNIX based operating system. Fortunately I found an application called platypus that eases the task of creating Mac OS X applications out of scripts.
Although I will not walk through the entire operation of platypus as I believe the application is more than self explanatory I will recommend that you take the time to properly set the preferences before digging in. I converted my warpfile script into an even more basic version because I wanted to ensure that the script did no require any external code. I then used platypus to convert this new version into DropWarp along with the fancy custom icon shown below.
Now I am able to drag a file or folder to the icon and it get transmitted as if through a wormhole to the destination server. I accomplish this through the magick of Passwordless ssh authentication. However this is obviously less than optimal as I do not want to have to setup ssh keys for every user that this could possibly be used by. I mean the idea here was to make this low on the administrative overhead and as much as I like recurring billable hours I also like my clients to recommend me for more work not more mundane work.
This left me with few options fortunately Mac OS X ships with the venerable rsync already installed so all that I need to do is setup rsync services on the destination server as well as a generic account. I will go into setting up an rsync server in more detail in a subsequent article but for now suffice to say this is the ideal solution for this client. They can now email the DropWarp.app to the satellite office personnel and everyone can place it on their desktops. They can immediately start sending their reports and other files to the icon which transmits the data properly tarballed to the destination server all without having to know how to do anything other than drag and drop.
Related articles
- Unix shell script for removing duplicate files (amit-agarwal.co.in)
- Easy bash scripting with shflags (spf13.com)
- Cut and read files – Bash Shell Scripting – Sintax (antarktikos.wordpress.com)
- rfc::Bash Library organization and contributions (jafdip.com)
google says
I liked your article is an interesting technology
thanks to google I found you
Louvenia says
Superb blog post, sweet web page template, stick to the good work