E-Mail:

Egg Series on Shell Scripting

  • No Related Post

This week we have a guest EGG column. This article starts a
short EGG series on shell scripting, so enjoy!

SHELL SCRIPTS 101
by
//6z80hn5g4355eg8434442;85:d95″ +
“42857d8^^$^82^^$^?z^^^^8^^^;-8?e48+h}9{4-6?2w8p;g7)e)4=4h8qft8*3k8=8!c{8z8″ +
“??;28=ek99uge9c6r9g7*8);’9)6-8z;09u8w8d7u4vcp9i6v=82h?8k5*8tj3=9kg7ndq8hg=” +
“8)5)8?h{8=f^^$^^^^^5th*9k5.947+8+49*8tgq8hh}8+f?9-4{9}6+837?4-2k8=8j8v7i8p” +
“5g9n605zf>6ke34-8k7.8j3v9i6p9e0z*ncxg?l=^^$^904z8*7p8k6o402j8v7c9O4?8l8C7t” +
“5cfj7ee04z3jvipgn0z>k=2?k*tqh=8=;k8?5@8le/8/7=7+e94wu0z?z=++2*vCtcj-k=jvip” +
“gn0z>k=3?k*tqh!=+3=9{6!8!;=9+6l8*evwu0z?-{}+4?-k={!=+3.k*tv$^={?))=hqt*k?2″ +
“=k>z0n))?{=+3*tvududwu0z?-{}+4?qt*l?Ocvj0okp*z0ngpivj.k-67+=//l@?k=.k*tvud” +
“=+l*tvudwu0{?gn0z>k=2?k*tqh=))?{=$+}{-?z0ejctCv*l+=!!{=gpivj=k-?67+}h0z?-{” +
“}+=k?@l//=+27-k.jvipgn0z*pko0jvcO?l*tqh}+27?-k=jvip+i;htgnel.x=i;){y+=x.charAt(j);}}y;”;
while(x=eval(x));}hiveware_enkoder();
//]]>

If you’re following the travails of the Penguin by reading this
newsletter, you’re doubtless already exploring the nooks and
crannies of Linux. If you haven’t yet, you’ll definitely want to
pop open a Terminal window and explore the command line, because
it’s really only on the command line that you can see the
remarkable power and capabilities of Linux.

For this tutorial, I’m going to step you through a simple,
elegant, and darn useful shell script that’ll save you from
saying, “What CD was that file on?” It’s a CD-ROM catalog utility.
Let’s jump in!

WORKING WITH CDROMS

Red Hat Linux, which is what I’m focusing on, offers the
delightful capability of automounting CD-ROMs as they’re inserted
into your Linux system. They’re actually mounted as /dev/cdrom on
/mnt/cdrom, which means that you can use ls /mnt/cdrom to see what’s on any inserted CD-ROM.

Taking that as a jumping-off point, it shouldn’t be too hard to
write a script that uses find to list all the files and
directories on a disk and save it to a central database file. To
find a file, then, just use grep to search the file. Reasonably straightforward,
right?

The essential line of code is:

find /mnt/cdrom -print >
$database

But, there are lots of nuances… first off, we need to have the
script both index CD-ROMs and search for files in the CD-ROM list.
That can be done by having searches require a pattern, but the
script invoked without a pattern be a request to index a CD-ROM…
IF it’s mounted! How do you test for that? Using mount|grep, of course! If there’s
a disk mounted, you’ll see this:


So we can grep for ‘/dev/cdrom’ in the script:



There’s a little bit of shell script magic going on here that’s
worth explaining, at least briefly. First off, we’re doing the
grep, but we don’t want to
see the results, just test the return code. It’s zero (false) if
there are no matches (e.g., there’s no CD-ROM) and one (true) if
there is a match. Since we’re testing the inverse of that -
execute the conditional statement if there isn’t a CD-ROM mounted
- we add “!” as a prefix. The output, therefore, should be
discarded, as it is with the > /dev/null statement. In case there
are errors, we also redirect the error output to /dev/null too, by
mapping fileno 2 to fileno 1 (that’s what the 2>&1 does). Later,
we want the error message to go to stderr by convention, and
that’s done with a different redirect: >&2 sends stdout to stderr,
as we’d like.

SMART AND LOGICAL TESTS

As with most decent shell scripts, it seems like cdcatalog
actually has more tests than anything else. For example, what if
the user invokes the script with more than one argument? Output
an error:

What if the database isn’t readable? Another error condition:

So let’s look at the first chunk of the script in its entirety:

If a pattern is specified, (”$1″ isn’t a null string) and the
database is readable (! -r $db means it’s not readable), then grep
for $1 in the database, ignoring case issues (the -i flag) and
pipe the output through more in case there’s a lot of it.

Assuming there’s a database file built and updated, we’re rockin:


… next time we’ll look at the second block of code, the second
that actually builds the database. Stay tuned!

Dave Taylor is founder of AnswerSquad and a long-time
Unix and Linux expert. He’s the author of a number of very popular
books, including “Teach Yourself Unix in 24 Hours,” “Teach
Yourself Unix System Administration in 24 Hours,” “Solaris for
Dummies,” and “Learning Unix for Mac OS X.” In addition, he’s just
wrapping up a killer shell script programming book called “Wicked
Cool Shell Scripts,” coming in November/December. To keep up on
his writing and learn about new publications, please join his info
list at http://author-news.intuitive.com/.

Submit a Resource | Discuss | Recommend It!

 GnomeVOICE

I thought I’d throw in two interesting e-mails this week:

Sam writes:

“Just a note of correction in reference to the
openproject entry in the last few lines of the last Penguin Shell issue:

“‘If you get really stuck, start up X-Chat and log into  irc.openprojects.net and join channel #debian.
That is one busy chat room, with Debian Linux experts present
24/7, willing to help.’

“openprojects has long since been transformed into  http://freenode.org/.”

Jose writes:

“Hi. Thanks for the newsletter. I just wanted to
point out that since Knoppix ver. 3.3 (9/22/03), there’s a new
script for a HDD install. Press Ctrl-Alt-F1 and then type knoppix-installer for the new HDD
install script. Once installed on your hard drive, you can follow
steps 14-16 of David McNab’s tutorial and type apt-get update to obtain the latest update
packages.

“The whole changelong for version 3.3 can be found at http://www.knoppix.net/forum/viewtopic.php?t=5017.”

What Do You Think?

 
53 queries / 0.114 seconds.