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

system command in Perl script doesn't work sometimes

Status
Not open for further replies.

RottPaws

Programmer
Mar 1, 2002
478
US
I've got a script that opens a telnet session, does some stuff and then closes the session.

At certain points in the script things can happen or not happen that will trip an error flag.

At the end if the error flag variable is false, meaning the script did not encounter any problems, the log file is deleted using a system command. This part works.

But if the error flag is true, I'd like to rename the file and this part doesn't work.

If I run it from the command line, it works, but when the web server runs it, the renaming part doesn't work even if I hard-code the error flag. The web server is part of the group for the directories involved and both directories are set to 770 permissions. The log files that are created have the web server username as both owner and group.

Here's the pertinent part of the code:
if ($errFlag==1){
#I tried this a couple of different ways, but it does not rename the file
# system("mv $dlog $derr");
# system("mv $ilog $ierr");

`cp $dlog $derr`;
`cp $ilog $ierr`;
} else {
#This part works. The files are deleted if $errFlag is 0
`rm $dlog`;
`rm $ilog`;
}


Any ideas?

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Have you tried:
[tt]rename $dlog, $derr;
rename $ilog, $ierr;[/tt]

 
I had not tried that, but I have now. It errors out if I try to use backticks for it, but I get the same results as before if I use the system() function.

It seems like it's a permissions problem or something, because it works if I run it from the command line with my login, but it doesn't work when the web server runs it. But that doesn't make sense to me because the files are created by the script and if no error occurs, they are deleted by the script and this part works when the web server runs it.

I'm stumped.


_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
You shouldn't use backticks or [tt]system[/tt] to use [tt]rename[/tt]. It's a perl builtin function.

It's possible that there are PATH problems when it's the web server account running. If [tt]mv[/tt] is not in the path for that user, it wouldn't be able to run it. (Don't know why it wouldn't be in the same place as rm, but it's possible.)
 
What's that??? Oh!!! It's a clue!

Removed the system function wrapper and rename worked perfectly.

Thanks a bunch!

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Rott Paws could you explain what you did to me? I think I have a similar problem whereby my mpirun command cannot be executed by my web server even though I have tried to specify the full path ( the command can be executed by me in my account ).

I would really appreaciate your explaination. =\
 
My problem ended up being my own oversight.

The filename in my script is generated from parameters that are passed to it. Some of those variables can have spaces. In my "log" filename, I stripped out the spaces after creating the filename string, but I forgot to do it in the "error" filename.

When I tried to rename the file with system commands, it was failing because of the spaces in the filename. When I used the Perl rename function, it renamed the file anyway.

Something you might try.......something I should have done before posting here........is print the target filename to the screen and make sure it's what you want.


_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Hmm ok.
But that doesnt explain how removing the system wrapper function made it all work?
 
What I meant by that was I accidentally tried to use the Perl rename function as a system command.

system("rename $ilog $ierr");
system("rename $dlog $derr");


But the system() should not have been there. Removing it and just running the rename command itself worked.

rename $ilog $ierr;
rename $dlog $derr;


Does that help?

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
The [tt]system[/tt] function just passes the command along to the shell. If you had something like:

[tt]$ilog = "log file";
$ierr = "error log file";

system("mv $ilog $ierr");[/tt]

The shell would try to execute the command:
[tt]mv log file error log file[/tt]

That's not what you want. What that tries to do is move the files log, file, error and log to a directory called "file". This would just give an error.

It works with the perl [tt]rename[/tt] function because it knows that the parametes that you passed it are the names of the files, even if there were spaces in them.

It would also work if you quotes the strings you passed to system, like:
[tt]system("mv \"$ilog\" \"$ierr\"");[/tt]

Does this help explain it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top