Software Design UK


Home Page
Contact Us
About Us

Web 2.0
Sound Cards for Linux
Spam Filters
Linux FlashReader
Scripting in Linux
A SQL Tutorial

Clients
Client Login
Autoresponder
 
Scripting in Linux
Home Page > Training > Tutorials > Scripting in Linux


This is a Linux article coverin Flash Readers for Digital Cameras. It explains what is a Flash Reader, the circumstances in which its use is advantageous, the IT requirements for use and how to install the hardware and software under Linux.

Contents
Scripting
Creating a script file
Example : Image handling and HTML generation
The Script file
Explaining the file


Scripting

Scripting is simple process that many of us often do. This article shows how powerful simple scripting can be, with a script that takes a collection of pictures, automates their conversion to a size appropriate to the Internet, creates for each picture a thumb-nail picture, and creates an html file which, when clicked on by a browser , lists and labels each of the pictures, and includes the facility to click on a picture or label and see the enlarged picture. In the process, we look at some wonderful facilities for Linux users, including simple creation of a text file and appending text to it, html, automated image manipulation and more.

For those who have not yet dipped their toes in the water, a script is little more than a file that lists a series of Linux commands. Each command is processed in order. A script allows us to string together in one document a series of monotonous commands, so that we only have to issue the command once. A script can be used, for example, to mount and unmount a series directories on different machines around an office, adding in special requirements.

(Back to contents)


Creating a script file

To create a script, simple create any simple text file, list a series of linux commands and save the file (say as /home/me/MyScriptFile). To make the file executable, type at the command prompt:
admin@softwaredesign.co.uk> chmod 755 /home/me/MyScriptFile

You can then run the script by typing at the command prompt:
admin@softwaredesign.co.uk> /home/me/MyScriptFile

and each of the instructions is carried out in order, one by one.

An example of a script file might be:

# Move the current back up file to another
# place so that it does not get
# overwritten by a new back up - in case
# of disaster during back up
mv home/me/MyBackUpFile.tar /home/me/MyBackUpFile.old
# Back up the directories "software" and "data" to MyBackUpFile.tar
tar cf /home/me/MyBackUpFile.tar /home/me/software /home/me/data

The simplistic example ensures that you will not forget to save the old back up file before you create the new one -which will serve you well if the system becomes corrupt during the process of backup up, thereby corrupting both the system and its back up at the same time. (Note that putting a "#" sign at the front of the line allows you to add comments to your script files because the line will be ignored)

(Back to contents)


Example : Image handling and HTML generation

The full script file is on the magazine CD. For explanation purposes, the script is simplified into two files, one of which rotates images that should be portrait, from the landscape version that cameras tend to default to:

# Set up variables
SourceDirectory="/home/me/Images/PortraitPics"

# Loop through each file
for photoName in $SourceDirectory/*.JPG; do
  # Convert files
  convert -rotate 90 $photoName $photoName
done


Setting up variables in Linux can be useful. To do so, simply make up the name of a variable (eg. SourceDirectory) and assign a value to it. Note that some commands require there to be no spaces before and after the "=" sign, and leaving them out generally is a good habit to get in to. To get the script file to use the content of the variable, type it with the "$" sign in front. This means "replace the text "$variable" with its contents before carrying out the command.

Whereas the basic idea of listing a string of Linux commands in order in a script file can be fun, the real power of the script file comes with the ability to add in some simplistic logic - saying to the script something like "rerun this command and continue to do so until you come across something that indicates you have finished" or "only do this command if you come across certain conditions", or a combination of the two. In the example above, the login that is added in uses the syntax

for xxx;  do
  #carry out this list of commands
  #until you have reached the end
  #of the "for" conditions
done

Within the "for xxx; do" line, we have to specify when the logic can stop looping. In this example, the syntax:

photoName in $SourceDirectory/*.JPG;

says "look at every file of the format "*.JPG" in the directory specified in the variable "SourceDirectory" (in this case /home/me/Images/PortraitPics), and for each one, one at a time, put the name of the file in the variable photoName for later use.

Since this instruction is within the "for xxx; do" loop, the system goes through every file in the directory, and each time it comes across a new file, it carries out the following instruction(s), and comes back for the next file when the "done" instruction is reached, and so on until there are no more files in the directory.

The instruction that is carried out in the example is:

convert -rotate 90 $photoName $photoName

The "convert" linux command uses the "-rotate 90" option which says rotate the image 90% clockwise (-90 to rotate it anticlockwise), carries out the command on the first of the two files specified (in this case, the file name value assigned in the "for xxx; do" instruction above to the variable "photoName"), and write the output to the second of the files specified (using the same name means overwrite the original!).

The Re-imaging and html creation script file

(Back to contents)


The script file

The simplified script file is:

# Set up variables
TargetDirectory="/home/me/Images/BlastOff"
SourceDirectory="/home/me/Images/PortraitPics"
HTMLOutput=$TargetDirectory/portraits.html

# Write out the start of the html file echo "" > $HTMLOutput echo "Linux Magazine - a great magazine" >> $HTMLOutput echo "" >> $HTMLOutput echo "" >> $HTMLOutput # Use a Loop to create the thumbnail and medium sized image #and write out the body of the html file for photoName in $SourceDirectory/*.JPG; do # Create output names for pictures TargetMedium=`echo ${photoName#$SourceDirectory/} | sed s/.JPG/.med.jpg/g` TargetThumb=`echo ${photoName#$SourceDirectory/} | sed s/.JPG/.th.jpg/g` # Convert files convert $photoName -geometry 150X200! $TargetDirectory/$TargetThumb convert $photoName -geometry 375X500! $TargetDirectory/$TargetMedium # Write out the html code, using the file names of the converted images echo "" >> $HTMLOutput done # Write out the end of the html file echo "
" >> $HTMLOutput echo "$TargetMedium" >> $HTMLOutput echo "
" >> $HTMLOutput echo "$TargetMedium" >> $HTMLOutput echo "
" >> $HTMLOutput

(Back to contents)


Explaining the script file

The simplified script file is:

Note that the html that we want to output starts with some basic html, followed by looped html for each image, and ending with some basic html. The key aspects of the script are explained below.

The variables are set up, as in the last section.

The echo" command is used to create a string which normally be displayed on the screen. By adding the ">" symbols, the text is redirected to a text file. If the file does not exist, a new file is created. If it does exist, the existing contents are wiped out. So by using the ">>" symbols, which mean append the text to a file, rather than wiping out its contents, for all subsequent outputs, we build up our html file as desired.

The "for xxx; do" loop, uses the "sed" command and the "convert" command.

The "sed" command is used (in this example) to manipulate the name of the file to use as the basis for the name of both the thumbnail and the medium sized image. The names of each file are squirrelled away in the variable so that we can reuse it in the convert command and also the html writing instruction.

The complex line
  TargetThumb=`echo ${photoName#$SourceDirectory/} | sed s/.JPG/.th.jpg/g`

means echo the filename contained in the photoName variable, eliminate from the start of its result the directory element of the name contained in the variable SourceDirectory, take the result of this command and feed it into the sed command. The sed command says substitute (s) the text ".JPG" (in the first of the "/xx/" pair) with ".th.jpg" (in the second of the "/xx/" pair) and display the resultant line (g). Enclosing the entire process within the "`" symbols means process the commands contained within and make TargetThumb (in this example) equal to the result.

The "convert" command is used to convert the original image to a new size (150 x 200 pixels for the thumbnail and 375x500 pixels for the medium sized image). The sizes chosen give a thumbnail image that is somewhat larger than conventional, and useful if the detail in the image is important, and the medium sized gives a compromise between adequate resolution when the viewer wants to see the detail, but without excessive detail resulting in excessively large files. You can amend these values easily in your own real life applications. (Note that we needed to include the "!" symbol to force the sizes we instructed to be used - in principal this was unnecessary, but not in practice).

We then output for each converted image the relevant html, giving each image its own row within an html table. In the full example, we have some code which works out how to display two images on each line - and looks much better in certain circumstances.

(Back to contents)