Power Tool
A simple script is usually enough to automate a knotty task in Linux, but with some simple shell commands and a little effort, you can convert a simple batch file into a robust shell program.
|
A simple script is usually enough to automate a knotty task in Linux, but with some simple shell commands and a little effort, you can convert a simple batch file into a robust shell program.
Sometimes you need things fast; when the search for a needed tool leads to a dead end, a knowledgeable Linux user can usually cobble together a program with a few lines of shell code. However, putting a bit more time into the quickly developed script could result in a robust program.
One example is the famous Windows "Snipping Tool" that captures sections of a screen and opens, saves, or copies them to the clipboard for use in another program. The same function is easily programmed in Linux. With a bit of effort, you can create a full-fledged tool that you can integrate into your desktop as a keyboard shortcut or as an item in the start menu or taskbar.
The basis for the present project is the import tool [1] from the powerful ImageMagick [2] command-line graphics suite. The introduction on its man page is enough to give you an idea of its capabilities: It captures a single window, the whole screen, or any rectangular portion thereof and saves it as a graphics file or sends it to standard output, from which you can pipe it to another command.
If you run import abc.jpg on the command line, the cursor changes to a crosshair, with which you make your selection. The tool then takes a screenshot and saves it in the abc.jpg file.
Other screenshot tools that are already integrated into many desktops usually accept only a few graphics formats, such as JPG or PNG. A further limitation of these tools is that they're likely to be linked to the specific desktop framework and, in some cases, won't allow you to select part of a window. Also, some tools require two or three steps to complete the screenshot.
This entire process can be handled in one step – with a keyboard shortcut, an item on the taskbar, or via the desktop start menu – by grabbing a graphic area, then automatically saving it and even handing it on to another program. One application of the script might be when you're reading a particularly interesting web page and want to capture and save just a portion of it, preferably to a program or test folder created for the purpose, before moving on.
Because the import command is a powerful tool, it's just asking for a shell script to provide just such a solution (Listing 1) [3]. Like any good script, it begins with a line specifying the interpreter to be used. Its exact path appears after the #! . Bash is always a good choice for such tasks. In lines 2 and 3, you set the variables for the paths to use, with the folder in which you want to save the graphics. To enable automatic assignment of a file name, you can start it off with a sequentially generated number.
Listing 1
Simple Script
01 #!/bin/bash 02 path=<home-directory>/Programs/pic-cutter 03 pix=<home-directory>/Pictures/Imports 04 declare -i number 05 number=$(cat ${path}/picnumber) 06 import ${pix}/${number}.jpg 07 let number++ 08 echo $number > ${path}/picnumber
The statement in line 4 creates an integer variable that you can increment. During program execution, the script reads in this integer from a file or writes the value incremented by 1 .
To begin, you need a file that contains a number. Instead of opening an editor, enter
echo 1 > Programs/pic-cutter/picnumber
in a terminal to create the required file.
In line 5, you read this integer with a simple cat command. Because the statement is in parentheses, Bash runs the command in a subshell and saves the output in the number variable. The import follows this operation.
Instead of a name for the picture file, the script combines the path stored in the pix variable along with the number and an appended .jpg . The curly brackets around the names ensure that the shell uses the values stored therein, gives the picture a number, and saves it in the desired directory. The command in line 7 increments the integer by 1 . The simple echo command in line 8 writes the new value to the picnumber file.
If you want to perform a specific task with an image, you could output another graphics format, or you might want to "snip" a particularly interesting passage of text and pipe it to Ocrad [4], an optical character recognition program, so that you can convert the graphics file into a text file. In this case, it is necessary to work with some less common formats, such as PBM, PGM, and PPM, and not saving the graphic but writing it to standard output.
At this point, of course, you wouldn't want to leave things as they are, because you would always need to call the script from the command line. Instead, it would help to have a keyboard shortcut that points to the script. With Gnome, this is possible with System Settings | Keyboard | Shortcuts . With KDE, use System Settings | System Management | Keyboard Combinations . As a rule, all graphical interfaces have a way of configuring or recreating these keyboard shortcuts.
With regular use of the small batch program, you will realize that it is indeed behaving very much like a full-fledged desktop tool, but with a few ragged edges. Now is the time to extend it as desired.
Pages: 4
With a small script, you can convert large amounts of scanned text into PDF files that you can then browse with typical Linux tools – all thanks to OCR.
Keeping an eye on possible changes to the filesystem can prove very beneficial. It can also help you to automate many processes.
The Shell Script Compiler converts scripts into binaries, which protects against accidental changes but also carries some pitfalls.
Many Linux beginners stumble over the fact that you need to precede any calls to your own scripts or programs with a ./ combination. What's up with these dot-slashes?
Does the ingenious script your wrote last month look like confusing spaghetti code today? We show some proven style guidelines that can provide order and readability.
© 2024 Linux New Media USA, LLC – Legal Notice