Hi,
I've added some comments to the code to try and make it a bit more readable.
Hope this helps you,
Geoff
#
#Export your variables to make them available in new shells that get spawned.
#(This is needed for this program as the variables are used in an awk subprogram)
#The variables should all be null (empty) at the moment.
#
export city state lat long port
#
#grep pulls out all lines in a file which match a pattern. -i makes it case insensitive.
#The output is sent through a pipe to a read command which puts the values from the file into
#variables.
#
grep -i "$1 $2"townfile | read city state lat long port
#
#If the value in the variable city is null (empty) then print out an error "this city is not in the file"
#
#
if [ -z "$city" ] then
echo "this city is not in the file"
#
#ELSE IF value in the variable port is "y" then tell the user that the city has an airport.
#
#
elif [ "$port"="y"] then
echo "$city $state has its own airport"
#
#Otherwise start an awk program which reads input from the file and
#for each town with an airport works out the distance to the current town.
#The indented code is awk code not shell so the syntax is different. Also an awk program
#will execute once for every line of the input so the program flow may not be exactly what you
#would expect if you are used to other languages.
#(I think that some code got messed up in the HTML post and flat should be $lat so I've changed it)
#
else
awk '
#
#Set the value of the close variable to be 10000 (Arbitrary large number).
#
BEGIN { close = 10000}
#
#If the fifth field is "y" then the variable dist gets set to the distance from the current
#town (worked out from the coordinates.
#
$5 =="y" { dist = ($3 - '$lat') *($3 - '$lat') + ($4-'$long')*($4-'$long')
#
#If the value in the variable dist is smaller than the current value of close
#set the value of close to equal dist
#set the variables ccity and cstate to be the current city and state
#
if (dist<close){
close=dist
ccity = $ 1
cstate=$2}}
#
#Print
#
END {print ("the nerest airport is in" ccity,cstate)
print ("approxiamate distance is "60 * sqrt(close)" miles")
} 'townfile
#
#The line above is the end of the awk program, note that the townfile is defined as input here.
#Syntax that has been used is awk '<some program code>' <input_file>
#
#
#This just ends the if statement.
#
fi