Skip to Main Content
Linux Desktop Hacks
book

Linux Desktop Hacks

by Nicholas Petreley, Jono Bacon
March 2005
Beginner to intermediate content levelBeginner to intermediate
342 pages
15h 55m
English
O'Reilly Media, Inc.
Content preview from Linux Desktop Hacks
156
|
Chapter 6, GNOME Desktop Hacks
#47 Create Your Own GNOME Right-Click Actions
HACK
Checking File Types
The one time you can easily get into trouble with Nautilus scripts is when
you run a script against the wrong kind of file. It’s easy to find the type of a
file, but it’s not that easy to narrow it down to something usable in a script.
The following script has some additional code that checks to make sure the
file is an image:
#!/bin/bash
convertprg=`which convert`
while [ $# -gt 0 ]; do
picture=$1
filetype=`file $picture | cut -d ' ' -f 3`
if [ $filetype = "image" ]
then
newfile=`echo "$picture" | cut -d . -f 1`
$convertprg "$picture" "$newfile".gif
fi
shift
done
This additional code runs the file command against the target image. The
file command returns a string of text describing the type of the file. Then it
uses the
cut command to find the word “image” in the text output of file.
The word “image” should be the third field if you separate the words with
spaces. If this command returns the word “image,” the script attempts to
convert the image; otherwise, the file is skipped. The second field would
identify the type of the image (JPEG, for example), but if we used that field
we would have to compare it against a long list of image types. It is much
easier to simply identify the file as an “image.”
One final tip: the preceding script doesn’t care what kind of image it is con-
verting. That makes ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Linux Server Hacks, Volume Two

Linux Server Hacks, Volume Two

William von Hagen, Brian K. Jones
Introducing Linux Distros

Introducing Linux Distros

Jose Dieguez Castro

Publisher Resources

ISBN: 0596009119Supplemental ContentCatalog PageErrata