[SGVLUG] shell script & nawk exposure
Don Gibbs
donald.e.gibbs at jpl.nasa.gov
Mon Feb 20 11:04:32 PST 2006
This is a marginally involved note, so skip it if you like.
Although a committed Perl hacker, I wanted to strengthen my skill set
in Unix shell scripting, so a while ago I started tunneling through a
couple of texts on the unix kernel and various shells, a few pages
each day, focusing the Bourne shell. A couple of days ago, I decided
to commit some repetitive commands to a shell script (_not_ Perl).
The rest is a summary of one part of that effort.
The first step of the repetitious process was to identify the latest
*_data file in a particular directory and place it in a shell
variable -- a pretty typical thing to do. This seemingly simple step
was a bit harder than I'd have thought. The following is actually
the contents of a file I used with some trailing commentary. After
each success, and a few surprising dead-ends, I'd comment out the
line and move on until I got to the last two and success!
..................................................
#!/usr/bin/sh
# dlf - display last (chronological) *_data file
#ls -lrt *_data
#echo ls -lrt *_data
#echo "ls -lrt *_data"
#echo `ls -lrt *_data`
#echo "`ls -lrt *_data`"
#echo "`ls -lrt *_data`" | nawk '{print}'
#echo `ls -lrt *_data | nawk '{print $NF}'`
#echo ls -lrt *_data | nawk '{print $NF}'
#NAME=ls -lrt *_data | nawk '{print $NF}'
#NAME=`ls -lrt *_data | nawk '{print $NF}'`
NAME=`echo ls -lrt *_data | nawk '{print $NF}'`
echo $NAME
.................................................
Commentary:
This ran on a solaris box but the results are probably the same for
bash on linux.
I was a bit surprised that echo'ing "ls -lrt" caused the new-lines to
be replaced with spaces. Turned out to be a help. Curious
nonetheless. (Back quotes inside double quotes reversed that effect.)
I went down a couple of other interesting dead ends, but I deleted them, alas.
I had to keep reminding myself that I had intentionally stepped out
of a comfortable and productive environment (Perl) into one that kept
feeding me frustration and humility.
I recall David Lawyer's comment regarding awk a while back that it
was not worth the effort to learn. In my case, I occasionally do
have to understand shell scripts someone else wrote and I figure
it'll pay off for me in an engineering office situation, but if I was
just hacking for myself, David has point.
BTW, as I read through Bruce Blinn's _Portable Shell Programming_, I
was struck by how much Larry Wall borrowed from
sh/awk/sed/cut/grep/andonandonandon. The slow read of how the shell
works made Perl clearer, too.
I wonder how much other scripting languages are a derivation of shell
scripting.
--Don
--
========================================================================
Don Gibbs | "Somehow it seems to fill my head with ideas
dgibbs at jpl.nasa.gov | -- only I don't know exactly what they are!"
818 354-2990 - office |
818 653-9531 - cell | Alice, after reading JABBERWOCKY
Sec 316, Flight S/W & Data Systems - Group B, GN&C and FSW Testing
Opinions expressed are not necessarily those of JPL/Caltech or NASA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.sgvlug.net/pipermail/sgvlug/attachments/20060220/af2d283c/attachment.html
More information about the SGVLUG
mailing list