Steve Richter wrote:
I might be able to understand why the Unix shell has advantages if
examples were provided. As an applications programmer I do a lot of
adhoc searches of the system, looking for info as to how an
application works, or why a job bombed. I guess shell piping output to
input would help in that regard. But the many CL programs I write and
maintain have nothing to do with this adhoc searching. I do use the
SQL command line all the time. Maybe adhoc SQL is the equivalent of a
Unix programmers use of the shell. But frequently I regret not having
stored the SQL statements that reported "the breakage and charge off
transactions which affected the A/R for the first 3 days of the week".

Adhoc searching is dirt easy in the Unix shell. The command find and
grep are incredibly useful for searching. find looks for files in the
file system based on particular characteristics. grep searches the
contents of files. Often, I pipe the results of grep into grep in order
to limit the search results in some way.

But you're looking for a practical example? Okay, at home, I recently
wanted to reload files onto my MP3 player. But it had to be done in a
particular order to keep the songs in same order as on the albums. So I
wrote a very simple script:

------------
#!/bin/sh

# Target of copying
tgt="/media/disk"

# Copy directory structure
find . -type d | sort | while read d; do
mkdir "$tgt/$d"
done

# Copy files
find . -name "*.mp3" |sort | while read f; do
cp -v "$f" "$tgt/$f"
done
------------

This is called from the source directory, and copies files to
/media/disk, which is where my MP3 player gets automatically mounted.

In the second step, the directories are located, sorted, and created on
the MP3 player. That is, the find command lists all directories, which
are piped to command sort. The sorted directory names are fed into a
while loop, which, for each directory, does mkdir on the target directory.

A similar thing is done for the final step, but for the MP3 files, which
are copied.

The easier way would be to do "cp -r", which would copy everything. But
that would not preserve the order. Maybe there's an easier way still?
Perhaps. Sure, this script isn't perfect. But it does what I want.

(Actually, come to think of it, rsync would probably do exactly what I
need!)

My point is that the Unix shell allows you to do some powerful things in
a concise manner. Heck, at the last Toronto Linux Fest, one presenter
gave a talk on shell programming where he stated that he did practically
all of his general programming in the shell. I don't think I'd go that
far, but it does show how powerful the concepts are.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.