Why Non-Technical Users Might Find Shell Scripting Useful

Wednesday, October 31, 2007

You know, I've never found any real use for a shell script. Recently, I found an actual reason to write one for moving and converting audio files using a few less keystrokes.

I admit it, I'm really a desktop user who doesn't mind getting under the hood every now and then. I enjoy playing with the 'big' toys, like RDBMSes because I can afford to. But mostly, using GNU/Linux affords me an opportunity to do what I need and want to do for a price that can't be beat. Much as I've tried, the only shell scripts I've written in the past were pretty contrived, or simply following some tutorial. Now I have a shell script I can actually make use of.

I'm not going to try to teach Bash scripting here. There are plenty of places where you can learn that. LinuxCommand.org is one. The Advanced Bash-Scripting Guide is another. But I do want to make the argument that even non-technical people ought to consider taking a little time to learn something about the Bash shell and how to write a simple script.

For those that don't already know, I'm using gNewSense, a GNU/Linux distribution that aims at being completely libre, on my laptop. But at least one site I use offers some content only in RealMedia or MP3 format. Unfortunately, MP3 is hampered by patent issues, and I prefer to avoid that. So I started downloading the MP3 files and converting them to Ogg-Vorbis, and then deleting the MP3 files. But that requires downloading the files, moving them into the folder I want, and then converting and deleting them.

First, let me explain the need to move the files. Most of the time, I put my downloaded files in one folder, and choose a different folder for multimedia files by choosing "Save link as" from the context menu. In this case, the site uses a dialog that makes this approach impossible. So, after I download the file, it's a matter of dragging it to the right place in Konqueror, or typing the appropriate command at a Bash prompt. Then I have to run mp32ogg using Bash. And, of course, I have to delete the MP3 file when I'm finished. Using a shell script simplifies this task.

The whole command-line sequence to accomplish my task involves three commands: 'mv', 'mp32ogg', and 'rm'. Of course, in the text file I created, I added the standard initial comment lines:

# Move MP3 files from /downloads to /media/mp3 and convert to Ogg-Vorbis format

mv downloads/*.mp3 media/mp3

mp32ogg media/mp3/

rm media/mp3/*.mp3

So then I saved the file and made it executable. Now, instead of typing those commands, I just open a Bash prompt and type the one new command I created, like this, after downloading the file:

So, now I download the file, type the new command, and play it Amarok.

This may seem a bit contrived. After all, why not just download the MP3 codec, or use a distribution that supports MP3 out of the box? I've already answered that - there are legal issues that lead me to avoid MP3. The site I use has not responded yet to my request for Ogg-Vorbis files, and avoiding the site is detrimental to my goals, unfortunately.

The other reason this is a good example, rather than a bad one, is that, in the Free/Open Source Software community, there are numerous ways to do any particular task. So, while some newbies or non-technical folks might prefer the path of least resistance, and choose a distro that supports MP3 out of the box, this example demonstrates that you can make up new commands as you go (within reason, of course) to simplify what might seem like an otherwise daunting task.

A more typical task for shell scripting is automating your back-ups or auto-configuring your wifi card at boot-time, depending on your location. Blue GNU readers will surely be able to offer other examples. But I hope that non-technical people will be able to see better why anyone would even bother trying to write a shell script.

Note:As has been pointed out in the comments, this script needs some error checking and could probably be greatly enhanced.