If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > please help!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-02, 09:25
kinki_ngmy kinki_ngmy is offline
Registered User
 
Join Date: Mar 2002
Location: malaysia
Posts: 9
please help!!

any one knows that what does it mean by?i need someone explain to me......

export city state lat long port
grep -i "$1 $2"townfile | read city state lat long port
if [ -z "$city" ] then
echo "this city is not in the file"
elif [ "$port"="y"] then
echo "$city $state has its own airport"
else
awk '
BEGIN { close = 10000}
$5 =="y" { dist = ($3 - 'flat') *($3 - 'flat') + ($4-'$long')*($4-$long')
if (dist<close){
close=dist
ccity = $ 1
cstate=$2}}
END {print ("the nerest airport is in" ccity,cstate)
print ("approxiamate distance is "60 * sqrt(close)" miles")
} 'townfile
fi

a typical line in townfile:
Boston MA 42.3333 71.083 y
Reply With Quote
  #2 (permalink)  
Old 04-26-02, 05:18
geoffgomez geoffgomez is offline
Registered User
 
Join Date: Apr 2002
Posts: 17
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
Reply With Quote
  #3 (permalink)  
Old 04-26-02, 05:20
geoffgomez geoffgomez is offline
Registered User
 
Join Date: Apr 2002
Posts: 17
Sorry, forgot to mention:

This program takes two arguments town and state. These are used as $1 and $2 in the grep command to make the pattern you are searching for.

grep -i "$1 $2"townfile | read city state lat long port
Reply With Quote
  #4 (permalink)  
Old 04-29-02, 11:30
kinki_ngmy kinki_ngmy is offline
Registered User
 
Join Date: Mar 2002
Location: malaysia
Posts: 9
thanks

thanks a lot. you really help e a lot. actually, i still get blur taht where we should put the code (in which part of unix?) how was the procedures to write the shell script(create by own)??

have a nice day
Reply With Quote
  #5 (permalink)  
Old 04-29-02, 12:12
geoffgomez geoffgomez is offline
Registered User
 
Join Date: Apr 2002
Posts: 17
To create a shell script you just need to save the program text in a file and give it execute permission:

chmod u+x <file_name>

Also you should put something like:

#!/bin/sh

as the first line of the file to let unix know how to run it.

Hope this helps,

Geoff
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On