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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this possible

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
Hi
I have a awk script which parses a file a.txt and produces some output.
Is it possible for me to upload the awk script on to my unix webserver and create a HTML form for the end user so that he selects the file a.txt from his local hard drive and say "submit" the output should be produced on the screen. As soon as he presses submit button, in the background the awk script should process the user selected a.txt file and produce the output.

Is this possible..? Any suggestions please.

regards
MP
 
Unless the browser software is broken (Internet explorer anyone??) you can't access a file on the client machine.
That would be very bad news indeed.
There are plenty of nasty exploits for certain browsers without intentionally incorporating something like this.

OTOH, you can use the shell and awk to do neat cgi stuff.
Here is what you ask for as a cgi script.
#!/bin/sh

function addpage() {
filen=${1:-""}
if [ ! -z "$filen" ] ; then
block=`cat $filen`
if [ ! -z "$block" ] ; then
echo &quot;<font face=\&quot;lucida sans,arial,gothic\&quot; size=0 color=black>&quot;
echo &quot;<blockquote>&quot;
echo $block | awk ' {
if (length($0) > 40) {
for (x = int(length($0) / 40) ; x > 0 ; x--) {
end = int((x * 40)) ; begin = int((end - 40) + 1)
printf &quot;%s\n&quot;,substr($0,begin,40)
}
}
}'
echo &quot;
<br></blockquote></font>
&quot;
else
echo &quot;<table align=center border=3 bordercolor=silver><tr>
<td nowrap><font size=-1>Sorry, nothing here yet.</font></td>
</tr></table>&quot;
fi
fi
return
}

echo &quot;Content-type: text/html&quot;
echo &quot; &quot;
echo &quot;
<html>
<head>
</head>
<body align=center fgcolor=black bgcolor=white>
<table align=center border=5 bordercolor=silver width=\&quot;70%\&quot;><tr><td>
<br><br>&quot;
addpage &quot;yourfilenamehere&quot;
echo &quot;
<br><br></td></tr></table>
</body></html>&quot;

Redirect the OP:
sh -vx awktest.cgi > boo.html
And you will see a silver bordered table containing the
nicely formatted file contents.

Have Fun
 
Hello marsd
Thanks for the response.
But I am not a cgi techie guy..It is difficult for me to understand.
I uploaded &quot;myscript&quot; and &quot;input.txt&quot; to my web server and in the shell prompt I tried
awk -f myscript input.txt and works fine.
How do I achieve the same using a browser. I understand there is some cgi/perl programming involved to achieve this. Shall I save
awk -f myscript input.txt
command as a cgi file and do and will this solve my objective.


Thanks again
MP
 
Hi, Murugs.
You can save it as a cgi-script.
1. cp it to cgi-bin.
2. chmod 755 scriptname
3. cp the file you want displayed to cgi-bin
4. chmod 644 the displayfile


The tricky part here is if you accept input from the user
on what file they want to view. This is a huge security problem.
IMHO you should hard code the filename into the script
as I have it. At least have a select box with acceptable
options and verify that the requested name is one of the acceptable ones.
If you are new to cgi-scripting you should definitely
have someone help you the first couple of times.
Getting a cgi to work is troublesome sometimes as well.
This is all OT now though so..
Good Luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top