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

Delete a file in Perl script 2

Status
Not open for further replies.

RottPaws

Programmer
Mar 1, 2002
478
US
Is there a Perl command that will let me delete files from a script?

I've got a script that uses telnet and generates log files.

If an error occurs in the script, a flag is set and an email is sent to notify me.

If no error occurs in the script, I'd like it to delete the log files, but I haven't been able to find a command to do it.

rm doesn't seem to be available in Perl. delete seems to deal with hash entries.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
`rm file`;

Blue [dragon]

If I wasn't Blue, I would just be a Dragon...
 
You can have perl make system calls:

system("rm $file");
exec("rm $file");
`rm $file`; #Blues suggestion

Or you can use perl directly:

unlink($file);
 
I wasn't able to get Blue's suggestion to work, but the system("rm $file"); method did the trick.

Thanks for the quick response!

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
About Blue's suggestion. It uses backticks. The symbol is found on the same key as the tilde (~). Note that backticks are not the same as single quotes.

Backticks ` `
Single Quotes ' '

The advantage of using backticks is that you can assign the command to a variable. Any information that would normally be display on STDOUT from calling the system command is now captured to the variable.

$result = `rm file`;

print $result; #prints "file removed" or whatever STDOUT would display
 
You nailed it. I was using single quotes (apostrophes, anyone???)

I tried it again with the backticks and it worked perfectly.

Thanks again.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
or --- you could be lazy and use the Perl function:

unlink 'a_file';

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Yeah, racklet mentioned that above. I think it's the way to go. It uses a system library call which should be faster than a system shell call, not to mention it's significanly more portable.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
tsk -- so he did, sorry guys

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Don't forget also, make sure '$file' is checked for taintedness! Wouldnt it suck if somehow the user input

"/etc/passwd; rm -rf /; echo 'I just screwed you!'"

Etc etc.

Moral? CAREFULLY CONTROL your system() executions. Never ever allow user input to make their way into them.

 
Thanks for the warning. It's not an issue in this case because $file is a filename (actually there are 2 files) that's generated by the script so each run creates a unique log file.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top