PDA

View Full Version : CGI Wrestling match


gorilla1
11-17-2002, 06:35 PM
The cgi below runs fine with the line that does the rename. The script with the rename and without the html stuff runs fine as a perl script from the command line... But with that line as a form submitted cgi, I get the following error message:

Configuration Error
The server encountered an error while processing your request.
ERROR_NOTES = Premature end of script headers: ....etc...

Any clue why that would happen?
G


#!/bin/sh

$new_file = "junk2.txt";

$old_file = "junk1.txt";

rename($old_file,$new_file) or "Error renaming file ($old_file)\n";




echo Content-type: text/html
echo
echo "<HTML>"
echo "<HEAD>"
echo "<TITLE>demo.cgi</TITLE>"
echo "</HEAD>"
echo "<BODY>"
echo "<h1>demo.cgi</h1>"
echo "<h2>Date: `date` </h2>"
echo "<h2>This script is running as user `id -nu`</h2>"
echo '$new_file'
echo "</BODY>"
echo "</HTML>"

mercurus
11-18-2002, 03:30 AM
G'day

It looks like the first line:

echo Content-type: text/html

should have double quotes around it
eg. echo "Content-type: text/html"
This should give you the required content header.

echo '$new_file'

I don't know about bash \ sh scripting, but in perl this won't interpolate the variable $new_file... I may be way off track though :)

Hope that helps

Cheers
mercurus

gorilla1
11-18-2002, 05:47 AM
mercurus,

Thanks - though the html part works fine. The line with the echo of the variable does not work, but that is not the problem. The only thing I am trying to accomplish is the rename, and that is the line that produces the error (if I remove that line, then no error rresults and the script runs ok).

g

fivesidecube
11-18-2002, 01:23 PM
gorilla1,

Check which directory the script is running in. When you run the script from the command line and when it's run via the web server, then the current working directory may well have changed and the name of the files being renamed are using relative addressing.

If this is the case, then the rename will fail possibly writing the error message, which isn't the HTML content header and hence the Server error message! Try moving the HTML content header to a location before the rename and see if you still get the error message.

gorilla1
11-18-2002, 01:28 PM
The html code is only in there because I wanted to confirm that the script was running at all. The only code that I need is the 3 lines that do the rename. I tried absolute addresses - the full url to the cgi-bin where the 2 files are located, but same result.

G

Mouldy_Goat
11-18-2002, 10:18 PM
Hi,
I don't actually use *NIX, but I'm fairly certain that there is no rename command. This could be causing your problem.

Try doing mv file1 file2 instead to rename your files.

See here (http://www.sgmltools.org/HOWTO/DOS-Win-to-Linux-HOWTO/t1111.html).

gorilla1
11-20-2002, 01:12 PM
Thanks, Mouldy, but rename definitely exists and works, since I am able to execute the script (the rename part) successfully from the command line.

G

Grizz2
11-23-2002, 02:37 AM
Well I'll give it a shot. Is your txt file in your cgi-bin with the script? If not you will need to use the server path to these files. Not the relative or url paths.
Of course I would think you would get a message from this line instead of a server error. Try fivesidecube's idea, moving the content-type above the rename so it will print the failed message?

rename($old_file,$new_file) or "Error renaming file ($old_file)\n";

Maybe try:

rename($old_file,$new_file) or die "Error renaming file ($old_file)\n";

Grizz

Grizz2
11-23-2002, 02:45 AM
Ok so I can't read. Your txt files ARE in your cgi-bin! Try adding this test to see if there are problems locating the file.

unless(-e $old_file){print"Cant find that darn file\n"}

Grizz

gorilla1
11-23-2002, 02:44 PM
Thanks, Grizz. OK, I tried moving the code under the header, but I am probably missing something very obvious. I don't know perl well enough to know what it is, and cant seem to pick it up looking around at references, etc. The html prints out successfuly, but then I get this message, which suggests associated with the variable declaration, I guess..
$new_file /path1/path2/cgi-bin/test.cgi: =: command not found

Here is the code:

#!/bin/sh

echo Content-type: text/html
echo
echo "<HTML>"
echo "<HEAD>"
echo "<TITLE>demo.cgi</TITLE>"
echo "</HEAD>"
echo "<BODY>"
echo "<h1>demo.cgi</h1>"
echo "<h2>Date: `date` </h2>"
echo "<h2>This script is running as user `id -nu`</h2>"
echo '$new_file'
echo "</BODY>"
echo "</HTML>"
$new_file = "junk2.txt";

$old_file = "junk1.txt";

Grizz2
11-24-2002, 06:07 AM
Just for kicks, try this one.

#!/bin/sh

$new_file = "junk2.txt";
$old_file = "junk1.txt";

print "Content-type: text/html\n\n";

print<<"PrintTag";
<html>
<head>
<TITLE>demo.cgi</TITLE>
</head>
<body>
PrintTag
unless(-e $old_file){
print "<h2>Cant find that darn file</h2>\n";
print "</body>\n";
print "</html>";
exit;
}
unless(rename($old_file,$new_file)){
print "<h2>Couldn't rename the file</h2>\n";
print "</body>\n";
print "</html>";
exit;
}

print<<"PrintTag";
<h1>demo.cgi</h1>
<h2>Date: `date` </h2>
<h2>This script is running as user `id -nu`</h2>
$new_file
</body>
</html>
PrintTag
exit;

Grizz

gorilla1
11-24-2002, 02:29 PM
Grizz, Thanks much for the help. Around the time you were writing your post, I got this to work using the approach you and fivesidecube had suggested (specifically, as shown below)

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print qq~<html>
<body>
</body>
</html>~;

$new_file = "/home/userx/public_html/dir1/dir3";

$old_file = "/home/userx/public_html/dir1/dir2";
rename($old_file,$new_file) or "Error renaming file ($old_file)\n";