Self deleting script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Self deleting script
# 1  
Old 10-31-2005
Question Self deleting script

I have a script like this called "remove.sh":

#!/usr/bin/ksh

rm remove.sh
echo "Deleted myself!"

Since UNIX is a scripting language I thought running it like " sh remove.sh" would generate an error/ or ANYTHING except the echo statements output. But the echo statement came out alright!
Since in this case I had spawned a sub-shell (using 'sh'), I tried running it as ". remove.sh" and it still worked.

Can anybody explain why?
# 2  
Old 10-31-2005
Put this in a script and run it. It will delete itself.

Code:
#! /bin/rm -f

echo "This will never get printed...!"

vino
# 3  
Old 10-31-2005
Question Want to know why the previous ver works

Thanks a lot Vino!

Could you also tell me why the previous version (the one I posted ) works (I mean, gives the echo output). I had thought the shell scripts were executed line-for-line without being stored in a temporary buffer.
# 4  
Old 10-31-2005
Because the interpreter (the shell) loads the whole script and then executes it line by line. It would be really rather ineficient for it to repeatedly read a line and execute that line.
# 5  
Old 10-31-2005
Question File not getting deleted

I tried the script by Vino. The line is getting printed and the file is not getting deleted. Im using OSF1 V5.1 732 alpha. I am using Korn Shell.
# 6  
Old 10-31-2005
It works fine for me ... how are you executing the file?

If you are calling it like:

Code:
ksh remove.sh

then it will not work since you are foorcing the interpreter to be ksh where vino's example does not.

What do you have in your script and how are you involking it?
# 7  
Old 10-31-2005
Quote:
Originally Posted by Unbeliever
Because the interpreter (the shell) loads the whole script and then executes it line by line. It would be really rather ineficient for it to repeatedly read a line and execute that line.
Ordinary shells behave like that, but ksh is much more efficient. To execute a loop, it ensures that it has the entire loop in core, then it compiles the loop and then it executes the compiled code. Other shells need to re-interpret the code on each iteration. This the secret of ksh's speed.

More than that, despite the name, rm does not remove files, it unlinks them from directories. If the file has zero links and is not open by any process, it is removed. So the file will not disappear while ksh has it open. Instead, it becomes a file with zero file names. This behavior is a good way to handle temporary files and a lot of programs do that... create a file, unlink it, then use it. It is guaranteed to disappear upon program exit.

Newbie admins post fairly often saying that a big file ate their filesystem, they deleted it, and they did not get the space back. This is why that happens. The file is still consuming space and will until the process that has it opened is killed. And now the file has no name so it is much harder to deal with.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for deleting Files

Hello, I am new to shell scripting..i need your help. i have the delete the log files which are present in the specific path .i have the rules as below. 1)In Path1 i need to delete all the files which are present 2)in Path2 i need to keep 1 month of data and delete the rest of the data... (5 Replies)
Discussion started by: rajesh25
5 Replies

2. Shell Programming and Scripting

Help with Deleting Folders Script

Hi all new to the forums. Very beginner at shell scripting. What I'm trying to do is this: Is the directory empty prior to removal of it? If not, inform the user that the directory is not empty and if the user wants to remove it remove or move the files in it first. But I'm stuck.. It checks... (3 Replies)
Discussion started by: rduckett1456
3 Replies

3. Shell Programming and Scripting

Deleting Specific Characters in Script

What's one single UNIX pipeline that would delete all of the vowels from one file (global)? I know I would need to use the sed command. But I'm stuck after that. i know how to replace characters with other characters I just don't know how to fully get rid of it. (4 Replies)
Discussion started by: sarahahah
4 Replies

4. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

5. Shell Programming and Scripting

Script problem with deleting data

The 10.Delete data doesnt work at all, plesase anyone could help on that. When I choose options 10 to delete a record it only copy the selected data on the other file dbs1 but doesnt delete it from the database where other records are. #! /bin/bash i="y" echo "Enter name of database " read db... (6 Replies)
Discussion started by: Lina_14
6 Replies

6. Shell Programming and Scripting

Script for Deleting files

Hi All, I need a script which compares the sysdate and the file date(Last updated time stamp) and if the time difference is greater than 10 mins i need to delete that file. Can anyone help me pls?????? Ashok. (5 Replies)
Discussion started by: Ashok_oct22
5 Replies

7. Shell Programming and Scripting

Deleting table cells in a script

I'd like to use sed or awk to do this but I'm weak on both along with RE. Looking for a way with sed or awk to count for the 7th table data within a table row and if the condition is met to delete "<td>and everything in between </td>". Since the table header start on a specific line each time, that... (15 Replies)
Discussion started by: phpfreak
15 Replies

8. Shell Programming and Scripting

Script for deleting 30 days older

Hi, I need a script to delete files that are 30 days older and also the file name should contain aa or ab or ac as substring... Regards, Dolly.... (3 Replies)
Discussion started by: moon_friend
3 Replies
Login or Register to Ask a Question