[techtalk] Bourne Shell Programming and Awk

Cat cschmitt at polaris.umuc.edu
Sat Dec 4 21:07:31 EST 1999

Hi there --

I am having quite a time with a final project in my Unix shell scripting
course, and I'd love some help.  Basically, I'm writing a script that
takes two parameters from the user, an alphanumeric character and a
directory.  Then, my script has to find and list any subdirectories in the
user-defined directory which begin with the alphanumberic character.  I
decided to go what I thought was a better route and used awk, instead of
listing out the files and examining them one by one.  The section I am
having trouble with is this:

   echo "ls -l $DIR | awk '\$1 ~ /^d/ && \$9 ~ /^$ALPHANUM/ {print \$9}'"
   LIST=`ls -l $DIR | awk '\$1 ~ /^d/ && \$9 ~ /^$ALPHANUM/ {print \$9}'`

After this segment, I go through a for loop to list out the filenames
listed in $LIST.  I put the echo print statement in there to figure out
what the computer thought it was getting, and put the backslashes in
everywhere because the shell was basically erasing the $1 and $9 awk field
numbers (I expect this was because it thought they were unfilled

Anyway, what prints out is this, when I run the script with . as the
directory and L as the alphanum :

ls -l . | awk '$1 ~ /^d/ && $9 ~ /^L/ {print $9}'

This is the printout from the echo command.  The problem is, nothing is
coming out from my LIST assignment and for loop.

Here we go -- details details!  I pasted that exact echo onto the command
line, and it worked perfectly.  I normally work in tcsh, so I ran sh
manually and tried the command again -- same good result.  However, when
it runs in the script it does not work.  From my debug, it looks like the
LIST variable never gets anything assigned to it.

Also, I pared down the command in the script to:

LIST=`ls -l $DIR | awk '{print \$9}'`

When I ran that, the script worked 'perfectly', listing out all the files
in my working directory.  

So, if any of you out there know something about this, I would really
appreciate the help.  I can't figure out where it is going wrong between
the command line and the script.


 The plural of anecdote is not data.

techtalk at linuxchix.org   http://www.linuxchix.org

More information about the Techtalk mailing list