From shell script to compact tool
|
Even More Robust
In the next step, you'll see how to extend the script so that it can be easily transferred to another system. So far, the script is fairly simple and therefore prone to error. For example, if you were to pass it on, the script would return an error because home directories on other PCs are different.
Listing 2 shows an enhanced version of the snip tool. In line 2, you'll notice the first extension to the code in the acceptance of a parameter, which it references as $1 or ${1} .
Listing 2
Enhanced Script
01 #!/bin/bash 02 GRPROGRAM=${1:-nothing} 03 PICTURES=~/Pictures 04 TARGET=${PICTURES}/Imports 05 BNR=$(date +"%Y-%m-%d_%H%M%S") 06 FMT=jpg 07 PICNAME=ImportedPic-${BNR}.${FMT} 08 CONFIG=~/.config/sniprc 09 [ -f ${CONFIG} && source ${CONFIG} ] 10 [ -d ${TARGET} ] || mkdir -p ${TARGET} 11 import ${TARGET}/${PICNAME} && which ${GRPROGRAM} &>/dev/null && ${GRPROGRAM} ${TARGET}/${PICNAME}
If you don't want to attach a parameter, the :-nothing extension provides a fallback value. If you don't pass in a parameter, it takes the string nothing as the value. That's necessary to avoid an error later on.
The variables in all caps in lines 3 through 6 are used to save pathnames and file names. The use of caps makes the code clear: As a rule, these variables won't change during the course of the program.
The date command in line 5 helps to create a clear and continuous naming convention for the images. The formatting string after the plus sign ensures that the four-digit year, month, day, and time (in seconds) are included in the file name. Line 6 specifies the desired suffix for the images, which determines the file type. Line 7 builds the image file name.
The composite expression in line 4 defines where the script saves the images. But, because the program won't know if this folder even exists, it checks later on to see whether it's been created. If not, it creates it on its own.
Line 8 shows the path to a configuration file for the user. The script checks in line 9 whether the file exists and then reads it. This leaves you the task of designing the script so that it contains sensible defaults, but the individual user has the option to overwrite it without touching or copying the script.
In line 10, the script checks whether the target directory for the images exists. If this is not the case, it creates it. The -p option for the mkdir command ensures that it recursively grabs all higher directories.
Line 11 initiates the import action. The command includes no options. Without a parameter, the script simply imports your "snip" and saves it in the specified folder with the name generated in lines 5-7. If you run the script with a parameter (e.g., gimp ), it checks with the which ${GRPROGRAMM} command in line 11 to see whether the input parameter is a regular path to an executable file. If it is, your image will be opened with this program and further processing of the image file is possible immediately.
Conclusion
With this small script, you have a full-fledged desktop application in your pocket. You can continue to extend it when needed, and you can even port it over to other systems.
One way to extend the script would be simply to add a few options to the import command. For example, you can add -snaps 3 after the command to take a sequence of three screenshots, and from which the import command automatically generates names for the image files. The man page for import provides further advice about the options you can add to the command.
Infos
- Import: http://linux.die.net/man/1/import
- ImageMagick: http://www.imagemagick.org/script/index.php
- Code for this article: ftp://www.linux-magazin.de/pub/listings/ubuntu-user.com/17
- Ocrad: http://www.gnu.org/software/ocrad/
« Previous 1 2 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)