Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If, then, else 2

Status
Not open for further replies.

Adell40

MIS
Nov 20, 2002
14
US
I am attempting to create a script that will
1)Ask for the name of a file
2)Verify that the file is readable
3)If the file d/n exist or is not readable, issue an error message of some sort
4)print the file that has been sorted.

I am not sure if I should declare a variable for the file or not.

name=address.data
>
> echo "What is the name of the file? "
>
> if [ -f $name ]
> read name
>
> then sort +8n +0 +1 $filename > address.sort
> awk '{print $1, $2
> print $3, $4, $5
> print $6, $7, $8, $9
> print " "
> print " "}' data.sort
>
> else
> echo "file doesnot exit"
>

This just sits there an looks at me....I input the correct filename or an incorrect one, does nothing...please help
 
for starters:

echo "What is the name of the file? "
read name

if [ -f $name ]
........ the rest of the code to be debugged

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Am I forgetting something here...not sure what I am to do next. Any suggestions? Thanks
 
If you want it read the standard input, no need to set name=something. And, read name should be in front of the script. Don't know what is the value of $filename. You haven't shown all code to here. Change as follows:

echo &quot;What is the name of the file? &quot;
read name
if [ -f $name ]

then sort +8n +0 +1 $name > address.sort
awk '{print $1, $2
print $3, $4, $5
print $6, $7, $8, $9
print &quot; &quot;
print &quot; &quot;}' data.sort

else
echo &quot;file doesnot exit&quot;


tikual
 
Something like this ?
Code:
echo &quot;What is the name of the file? \c&quot;
read name
[ -s $name -a -r $name ] &&
  sort +8n +0 +1 $name | awk '
    {print $1,$2
     print $3,$4,$5
     print $6,$7,$8,$9
     print &quot; &quot;
     print &quot; &quot;}' ||
echo &quot;Can't print file '$name'&quot;
In your post the variable filename is unknown and the file (i]data.sort[/i] is not the output of the sort command (address.sort
Anyway the syntax of the if instruction in sh or ksh is:
if list; then list
[elif list; then list]
...
[else list]
fi

Hope This Help
PH.
 
I think I am blind...the value for $filename is actually address.data which contains a list of names and address; although I do not think it is declared anywhere.

> then sort +8n +0 +1 $filename> address.sort
> awk '{print $1, $2
> print $3, $4, $5
> print $6, $7, $8, $9
> print &quot; &quot;
> print &quot; &quot;}' data.sort
>
> else
> echo &quot;file doesnot exit&quot;
 
Ok, I got this thing to work but I do not understand how my input file is passed to the script. Any help breaking down this script would be appreciated. The input file that gets passed is address.data

echo &quot;What is the name of the file? &quot;
read filename
if [ -r $filename ]
then
sort +8n +0 +1 $filename > sorted.data
awk '{print $1, $2
print $3, $4, $5
print $6, $7, $8, $9
print &quot; &quot;
print &quot; &quot;}' sorted.data
else
echo &quot;file does not exist&quot;
fi
 
Did you see what is the different of my post and your script? Because you didn't show us all info. of your env. How come we can guess that only one file input and one file output?

From your script, input file assigned to $filename. Many user misunderstand the meaning of 'read xxxx'. That means the input file will be assigned to a variable but not means as 'read a file called filename'. After these, the file sorted and be redirected to a file called 'sorted data'. Then you print few of fields from this file. That's all.

tikual
 
With ksh, you can replace the sequence echo/read by a single read :

read filename?&quot;What is the name of the file? &quot;

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top