Intro to Shell Programming: Writing a Simple Web Gallery
A Simple Script
So you're not a programmer, you say? If you can string a few shell commands together, it's not much of a step from there to programming.
To demonstrate that, I'll take you through the steps of writing a very simple web gallery script: one that will take your images and build a little web page to show them off.
Your script will need to do some image operations like resizing from the command-line. For that, I recommend ImageMagick, available as a package in just about every Linux distro. Just
apt-get install imagemagick or
yum install imagemagick or whatever your distro prefers, and you're ready to go. If you prefer GraphicsMagick, a fork of ImageMagick, that's okay too. All the basic commands are the same.
For this project, you'll also want a directory with some images in it. You can create a directory with
mkdir dirname or use your favorite file manager. Then copy some images into it. Be sure to copy, not move: you'll be making changes to images in this directory, and you wouldn't want to overwrite the originals.
In a shell, change directory (cd) into your new image directory; you'll be running your script from there.
Create your shell script
To create your shell script, use a text editor, not a word processor like OpenOffice. If you don't have a favorite text editor yet, check out the recent LinuxPlanet articles on Kate.
A shell script is, at its most basic, a list of commands you want the shell to run. It starts with a magic line:
That's called a shebang line, because the # character is usually pronounced "hash" and the !, "bang". It lets you run your script like a program, by just typing the program name, and Linux will figure out that it's a shell script, run by the shell, /bin/sh.
There's one other step you need before you can run your shell script: you need to make it executable. Choose a file name (say, gal for gallery) and save your shell script. Then, in a shell, you can change its permissions this way:
chmod ugo+x gal
(that means "Make the file executable for user, group and other.") If you prefer using gui tools, right-click on your newly created script, choose Properties and look for the Permissions tab: you can make the script executable there.
Now you have a shell script, and it's runnable. But it doesn't do anything. That's the next step.
Sponsored by BlackBerry
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!