<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { padding-top: 0 ; padding-bottom: 0 }
--></style><title>shell script & nawk
exposure</title></head><body>
<div>This is a marginally involved note, so skip it if you like.</div>
<div><br></div>
<div><br></div>
<div><br></div>
<div>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.</div>
<div><br></div>
<div>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!</div>
<div><br></div>
<div><font
face="Courier">..................................................</font
></div>
<div><font face="Courier">#!/usr/bin/sh<br>
<br>
# dlf - display last (chronological) *_data file<br>
<br>
#ls -lrt *_data<br>
#echo ls -lrt *_data<br>
#echo "ls -lrt *_data"<br>
#echo `ls -lrt *_data`<br>
#echo "`ls -lrt *_data`"<br>
#echo "`ls -lrt *_data`" | nawk '{print}'<br>
<br>
#echo `ls -lrt *_data | nawk '{print $NF}'`<br>
<br>
#echo ls -lrt *_data | nawk '{print $NF}'<br>
#NAME=ls -lrt *_data | nawk '{print $NF}'<br>
#NAME=`ls -lrt *_data | nawk '{print $NF}'`</font><br>
</div>
<div><font face="Courier"> NAME=`echo ls -lrt *_data | nawk
'{print $NF}'`</font></div>
<div><font face="Courier"> echo $NAME</font></div>
<div><font
face="Courier">.................................................</font
></div>
<div><br></div>
<div><br></div>
<div>Commentary:</div>
<div><br></div>
<div>This ran on a solaris box but the results are probably the same
for bash on linux.</div>
<div><br></div>
<div>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.)</div>
<div><br></div>
<div>I went down a couple of other interesting dead ends, but I
deleted them, alas.</div>
<div><br></div>
<div>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.</div>
<div><br></div>
<div>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.</div>
<div><br></div>
<div>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.</div>
<div><br></div>
<div>I wonder how much other scripting languages are a derivation of
shell scripting.</div>
<div><br></div>
<div> --Don</div>
<x-sigsep><pre>--
</pre></x-sigsep>
<div><tt><font
color="#000000"
>====================================================================<span
></span>====</font></tt></div>
<div><tt><font color="#000000">Don
Gibbs<x-tab>
</x-tab><x-tab> </x-tab>|
"Somehow it seems to fill my head with ideas<br>
dgibbs@jpl.nasa.gov<x-tab>
</x-tab>| -- only I don't know exactly what they are!"<br>
818 354-2990 - office<x-tab>
</x-tab>|</font></tt></div>
<div><tt><font color="#000000">818 653-9531 -
cell<x-tab>
</x-tab>| Alice, after reading
JABBERWOCKY</font></tt></div>
<div><tt><font color="#000000"><br></font></tt></div>
<div><tt><font color="#000000">Sec 316, Flight S/W & Data Systems
- Group B, GN&C and FSW Testing</font></tt></div>
<div><tt><font color="#000000">Opinions expressed are not necessarily
those of JPL/Caltech or NASA</font></tt></div>
<div><font color="#000000"><br></font></div>
</body>
</html>