Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to have a script delete itself

Perl - script delete itself

         

StopSpam

3:03 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



perl question - How can i have a script deleting it self ..?

Imean i have a script that saves all settings to a data file.

then other script runs fine on the data input from the data file.

now i want the first script to be delted after it saved the fom input .

this to avoid peopel forget to delete the script and then others can change settings.

what kind of coding should ithink of
so the script afterform has been submited that the script removes it self from server?

claus

3:25 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I personally don't think this can be done. Write another script (#2) that deletes script #1. Then, let #1 call #2 and die.

It's my best guess.

StopSpam

4:11 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



yes thats good thinking .

yes i can have script one auto forward to script two that deletes script one ..

but can you show me a smaple of code that extualy delerts a sample 1 script?

is it simular to open read rewrite or?

jatar_k

4:56 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



unlink?

[perldoc.com...]

claus

5:01 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, i really hope that you know exactly what you are doing. This could result in you deleting something that you really didn't want to delete.

Ill' say just enough to make it possible for you to construct it yourself, possibly using perl documentation but i'll sure as **** will not wipeout your web site for you. This can happen if you abuse any of this, so be very careful.

This tiny perl script will delete the file "examplescript.cgi":


#!/usr/bin/perl

unlink("examplescript.cgi");


Use with caution, please. The file will get deleted. Here's another version, that deletes the file calling it:


#!/usr/bin/perl

$deleteThisString = %ENV{HTTP_REFERRER};

unlink($deleteThisString);


Again Use with extreme caution, please

Then, how to call another perl script from a perl script? Try this line:


system ("perl path/to/otherscript.pl");

Now, i simply do not dare to tell you more, apart from this: These examples are not guaranteed to work on your server, they're only examples to get you started. Remember to set proper file permissions and paths.

/claus

StopSpam

6:42 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



dear jatar k and Claus

if deleting files is dangeruos i could try open the file
and rewrite the file so all code inside gets cleared and only a htmltag gets printed

this way script has become useless

i could try this

if i use the other way like:

#!/usr/bin/perl

unlink("examplescript.cgi");

can i put any other code bellow this coding?
i mean i can add it to a ecsisiting script?

claus

7:37 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, deleting files is not really dangerous. Not by itself, that is. It can become dangerous, though, if for some reason the script used to delete the file can be abused (or used by accident) to delete files that you do not want to delete.

I must stress, though, that using my examples as they are above could potentially be dangerous - they do not have any kind of security checks built in, they just delete files, no matter who asks, as long as it's allowed by the file system. In other words, they will do the job, even if you make a mistake, or someone deliberately abuses them.

The overwrite-method is actually the same thing. It's not really dangerous unless it can be used or abused to do something else than what you want.

On this question:

can i put any other code bellow this coding?
i mean i can add it to a ecsisiting script?

- the answer is yes and no:

1) Yes, you can put code anywhere below this line:

#/usr/bin/perl

Mind you, the first line under this line should be blank.

2) No, you can not include the whole thing in another script, as this script will probably already have one such line (#/usr...). You can include this line, though:

unlink("examplescript.cgi");

But as you already know how to write to a file, i suggest you do something else than deleting the script.

The problem is that you only want to run the original script once. This can be solved without deleting it.

Just add a line to the start of your "run-once" script, that checks if a file called "onetime" exists, and if it does, stop the script and do nothing ("die").

Then (here's the clever part) add a line to the end of the script, telling it that after it has done what it is supposed to, it scould create a file named "onetime"

In that way, the script will only run once, and you don't have to delete or overwrite files - plus, it's safer :)

/claus

StopSpam

9:05 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



cool excellent i go work on this thanks a lot ;-)

yes the form just saves a hidden input ...
if some one loads the script again it will read this hidden input from the data file and letthe script end directly

thats it ...

amoore

9:10 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



a line like this will cause a script to delete itself:

unlink $0 or die "Unable to unlink $0. $!";

before you try it, I suggest that you make a copy of your script so that when it deletes itself, you're not sorry.

As someone has already told you, "unlink" deletes files in perl. (well, it really unlinks them. Your OS deals with deleting.) In perl, the $0 refers to the filename of the currently running script. See the "perlvar" man page for more information on that.

As has already been suggested, you can probably do better by not deleting the script. You can make it check to see if it has been run already, or change its own permissions, or something like that and probably have better results.

Hope it helps.

-Andy

StopSpam

9:26 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



i will try this to;
unlink $0 or die "Unable to unlink $0. $!";

i willadd it to near the end of script one.
as it would save me time .
im nopro in programming and making it to check if
has run takes me more time igo tryboth..

unlink $0 or die "Unable to unlink $0. $!";

and the checkif has run before

thx