Expansion of variables in sh scripts

Badri Lokanathan badri at valhalla.ee.rochester.edu
Sun Feb 14 14:19:40 AEST 1988


Well, after several years of shell script writing, I thought I knew
everything about it, but I was wrong!

Given a 2 column file of data in two columns, the columns separated by
blanks. A script is desired which, among other things, searches for a
word in the I column and outputs the corresponding entry in the II column.

There are several ways of doing this; I want to know why the following
inconsistency took place (I tried it on BSD4.3):

#!/bin/sh
word=$1
result=`awk "/^${word}/{print \$2}" datafile`
echo $result
# This outputs the entire line, rather than the entry in the II column.
awk "/^${word}/{print \$2}" datafile
# This outputs only the entry in the II column, as expected.
######################### END SCRIPT ###########################
-- 
"I care about my fellow man            {) badri at valhalla.ee.rochester.edu
 Being taken for a ride,              //\\ {ames,caip,cmcl2,columbia,cornell,
 I care that things start changing   ///\\\ harvard,ll-xn,rutgers,topaz}!
 But there's no one on my side."-UB40 _||_   rochester!ur-valhalla!badri



More information about the Comp.unix.questions mailing list