BASHing Through Scripts

Written by Ken Gypen

November 26, 2007 | 08:12

Tags: #script #shell #unix

Companies: #bash

Foreword

by Brett Thomas
Of all of the "how-to" articles that we've been bringing to the front page as of late, it seems that the most popular have been the Linux series from our resident guru, Ken Gypen. Many of you have asked for these artilces to be a regular feature here on bit-tech, and we've been listening.

Today, Ken's back to bring you the next portion of his 'Nix series - creating scripts for command-line commands that help automate commands. Those of you who have used Apple's "Automator" software or have used batch-editors built into Adobe Photoshop CS will be familiar with just how extensible this can be - and it just gets more powerful when it becomes part of the OS.

Scripting in the BASH shell allows you to run literally every task that can be done, over and over, requiring only as much interaction as you add to it. The possibilities are endless, but we are going to use this article to do something that is generally useful - image resizing and watermarking. Hands down, it's one of the most annoying and mundane tasks around, particularly when you have hundreds of images to go through. Using a script turns an hours-long task into a couple minutes of automated processor cranking, saving you tonnes of time when you're doing your next mod update!

With that said, I'll hand it off to Ken...

Introduction

Use of programming tools like 'for' loops make many repetitive tasks a breeze.

Use of programming tools like 'for' loops
make many repetitive tasks a breeze.

After reading how you really should build a home server, or at least pointing out that there is a way besides Microsoft products, you gently became more accustomed to how that dark terminal works. Now that we've brought you to the dark side, it's time to show the true power of Linux - scripting.

While there are 1001 ways and languages to write up a script in Linux, each better than the other for different tasks, a nice one to start with is BASH. BASH you ask? Isn't that my shell? The command line I put my individual command into? Yes, it sure is! But as with many pieces of Linux software, what lies under the surface is often thousands of times more powerful than it seems.

BASH what?

BASH, short for Bourne Again Shell, is derived from the Bourne Shell. But the developers didn't stop there, they also took the best from the Korn Shell, then added in the best from the C shell. This mixture of the best of the best led to an awesome piece of software. As a finishing touch BASH completion was added in, to make even the laziest (or most forgetful, in my case -- Ed.) among us proficient at CLI.

The true power of BASH is hard to imagine without having witnessed it, so the idea for another article was born. In this issue, I'll try to give you a glimpse of what BASH can do for you. Certainly for the ones who like to take huge pictures and then need to resize or watermark them, this article is worth a read. You can save yourself hours of Photoshop "fun," just by entering one line of commands.

But before we can start with all this fun stuff, we need an editor!

To edit, or not to edit, that is the question...

As with most concepts in Linux, there are thousands of pieces of software that do the same thing in a different way. Editors are a fine example of that. They come in graphical or terminal flavour, with easy to use interfaces or lightning fast geeky interfaces. A nice graphical editor which is very much like notepad is gedit. Gedit is part of Gnome, a famous Desktop environment on Linux. Of course this requires a graphical environment, something we don't always have the luxury (or burden) of in some cases.

BASHing Through Scripts Introduction BASHing Through Scripts Introduction
Terminal text editors in Linux can go from the sparse Nano to the feature-rich Vim, and everything in between.

On the terminal side of things, there are three major (though a myriad of minor) players. Nano, an improved version of Pico, is an easy to use terminal based editor. If you want ease of use, go nano. Then there's emacs, a swiss army knife to display stuff - which is exactly what reduces the quality and usability of the editor in my humble opinion.

And finally we become real men, or geeks, and use VIm, or VI improved. As the name suggests, it's a (welcome) improvement to the VI editor. If you are proficient at using VIm, all your friends will be astonished at how fast you can do file editing. However, this speed comes with a downside - VIm isn't the easiest editor to pick up. But never fear, you're allowed to cheat in the VIm exam!

I'll be using VIm in this guide, but you can feel free to use any editor you want. Linux is all about choice, remember.
Discuss this in the forums
YouTube logo
MSI MPG Velox 100R Chassis Review

October 14 2021 | 15:04