The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script for Deleting Core files on root filesystem rgfirefly24 Shell Programming and Scripting 7 02-11-2008 02:41 PM
make script deleting mp3 with warnig to users stefan Shell Programming and Scripting 2 11-24-2006 01:43 AM
Urgent help required in deleting a line without opening a file usinga shell script naan Shell Programming and Scripting 6 07-20-2006 12:42 AM
shell script: deleting files from a directory onlyc Shell Programming and Scripting 1 07-09-2006 03:41 AM
Deleting by ID boyler Shell Programming and Scripting 1 09-23-2003 07:37 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-31-2005
Registered User
 

Join Date: Sep 2005
Location: Chennai
Posts: 80
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?
Reply With Quote
Forum Sponsor
  #2  
Old 10-31-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Put this in a script and run it. It will delete itself.

Code:
#! /bin/rm -f

echo "This will never get printed...!"
vino
Reply With Quote
  #3  
Old 10-31-2005
Registered User
 

Join Date: Sep 2005
Location: Chennai
Posts: 80
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.
Reply With Quote
  #4  
Old 10-31-2005
Registered User
 

Join Date: Jul 2005
Location: England
Posts: 183
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.
Reply With Quote
  #5  
Old 10-31-2005
Registered User
 

Join Date: Sep 2005
Location: Chennai
Posts: 80
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.
Reply With Quote
  #6  
Old 10-31-2005
Registered User
 

Join Date: Jul 2005
Location: England
Posts: 183
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?
Reply With Quote
  #7  
Old 10-31-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0