Self Destructing Bash Script

Hi all,

I know this is a pretty basic question, I am very new to shell scripting and I am looking for information on how to write a self destructing script. basically, I want the script to run, exit terminal and then delete it's self.

any help would be most appreciated! thanks!

Posted on Oct 16, 2009 12:04 AM

Reply
13 replies

Oct 16, 2009 1:30 AM in response to Matt James1

I want the script to run, exit terminal and then delete it's self.

Exit the terminal? I'm not sure what you're talking about here but maybe you need this (haven't tested it so not sure it works):
exit &

If you want the script to delete itself, this does the trick (assuming you have write permissions to the directory in which the script resides):
rm $0

N.B. This assumes that you are working in the same directory where the script is located.

Message was edited by: hungryjoe

Oct 16, 2009 10:26 PM in response to Matt James1

Matt James1 wrote:

I know this is a pretty basic question, I am very new to shell scripting and I am looking for information on how to write a self destructing script. basically, I want the script to run, exit terminal and then delete it's self.


I think it would help if you gave more detail about what you want. For example, there's a difference between a script and a process. A script is a text document that contains some high-level commands in a given language (usually bash or tch for unix commands). A process is what you get when you run (execute) a script - the computer translates the text commands in the script file into low-level computer commands, and then creates and executes those commands. it's like the difference between a musical score (music written on paper) and the the same music played on instruments. most processes are self-terminating (unless they are built with endless loops of some sort), so nothing special normally needs to be done to 'delete' a process. scripts are text files, and you can delete them if you want, but that's not normal (since you usually want to run a script again at some point in the future).

Oct 18, 2009 5:13 PM in response to twtwtw

What I am doing is putting a script in the LaunchDaemons that points to a process I need run after the system is first imaged (it points to a binding script).. I want to keep the binding script on the system for ease of rebinding the system when it unbinds itself, however I don't want the system trying rebind every time it boots. So if the LaunchDaemon can be deleted after first boot, that would work perfectly.

Oct 18, 2009 10:20 PM in response to Matt James1

first off, you don't put scripts in the LaunchDaemons folder. That folder is for plist files that get loaded as launchd jobs (where the jobs call scripts when triggered). I assume you know that, but you're not being very clear, so I need to make sure we're on the same page.

now, if you have a script that you only want to run after a disk is first imaged, you probably don't want to be using launchd at all. just store the script somewhere convenient, and have the disk imaging program run the script when it first mounts the disk after the imaging. if for some reason you feel the need to run the script via launchd, then have your script call launchctl to remove the launchd job from the list and disable the plist file (set its disabled key to true) so that it won't run again at reboot. deleting the plist (or the script) is unnecessary.

Message was edited by: twtwtw

Oct 18, 2009 11:13 PM in response to Matt James1

Thanks for the advise everyone. the $0 command turns out to be what I was after.. but after i did a little rejigging. In the end i created a login hook for the script to run the required process, and had the script delete itself once run.. I found this to be the easiest way to delete to prevent it form running again.. and as for having it handy to run again if required, i have just stored a copy on my flash drive.

Thanks again!

Edit; I nearly forgot, i included a "delete login hook" command in the script

Oct 19, 2009 8:40 AM in response to hungryjoe

The reason you're having trouble here is that you're using the wrong method to accomplish your goal. launch agents and launch daemons are for tasks that occur every time particular conditions arise. Basically you've been asking "how do I launch this script every time the computer starts up, but stop doing it after the first time?", which is just silliness. Now I don't as a rule object to people trying to tighten a bolt with a hammer - not knowing any better and needing to improvise are good reasons to do that - but I don't see a reason to help you make the hammer work when there's a perfectly good wrench sitting on the table. Make the script executable and put it in /Library/StartupItems - then you can delete the script file from the process without any issues.

Oct 19, 2009 9:46 AM in response to twtwtw

Well, actually my purpose was different from Matt's and cannot be solved with a startup script. I wanted my script to change the launch agent (like changing the StartInterval) if certain conditions changed — but of course then the launch agent has to be reloaded and there is no way of doing it with any script which is started by the launch agent itself.

Oct 19, 2009 10:22 AM in response to hungryjoe

hungryjoe wrote:
Well, actually my purpose was different from Matt's and cannot be solved with a startup script. I wanted my script to change the launch agent (like changing the StartInterval) if certain conditions changed — but of course then the launch agent has to be reloaded and there is no way of doing it with any script which is started by the launch agent itself.


if you want a launchd job to change under certain circumstances, that's easily accomplished by writing a second launch daemon that makes the changes. you just need to define the conditions correctly (and probably check in the script to make sure the process of the first job is not currently executing).

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Self Destructing Bash Script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.