Computing.Net > Forums > Unix > Creating a script to sort a file

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Creating a script to sort a file

Reply to Message Icon

Original Message
Name: Brian
Date: December 13, 2002 at 13:38:45 Pacific
Subject: Creating a script to sort a file
OS: UNIX
CPU/Ram: p3 256mb
Comment:

I need to sort an input file that the user can specify.
1: Ask the user for the name of the file
2: Verify that the file exists and is readable
3: issue a message if it's not readable
4: sort the input file by zip code, then last name, then first name.
5: then use awk to print the sorted file

I can't figure this out for teh life of me. I'm new to unix scrips. Here is what I tried so far

echo "Enter a filename :"
read filename
echo $filename
if test ! -r $filename
then
if test "$WARN" = "yes"
then
echo "$filename does not exist"
fi
fi
sort +8n address.data > sorted address.data

the above doesn't seem to send a warning no matter what filename is input. I also haven't been able to figure out how to read the input file. Any help would be appreciated


Report Offensive Message For Removal


Response Number 1
Name: unixadm2000
Date: December 14, 2002 at 10:45:27 Pacific
Reply: (edit)

I take it you have WARN set to "yes" somewhere else in your script or environment?



Report Offensive Follow Up For Removal

Response Number 2
Name: dave_ancon
Date: December 30, 2002 at 09:18:02 Pacific
Reply: (edit)

Better way of reading in a filename:

The command to call the shell should be:
shellname inputfilename

in other words, have the user list the filename right after the shell script call. If your shell is called command.ksh, then have the user do this:

command.ksh filename_to_read_in

Inside the script, use the variable $1 to capture the filename, i.e.
inputfile=$1

Now, you have the input file in your shell var.
You can use
if ( -e $1 ); then

do something

else
create error msg

fi

Do a "man test" to see the -e definition.


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software