Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self-deleting shell script

Tags:

bash

shell

I've looked around for an answer to this one but couldn't find one.

I have written a simple script that does initial server settings and I'd like it to remove/unlink itself from the root directory on completion. I've tried a number of solutions i googled ( for example /bin/rm $test.sh) but the script always seems to remain in place. Is this possible? Below is my script so far.

#! /bin/bash cd /root/ wget -r -nH -np --cut-dirs=1 http://myhost.com/install/scripts/ rm -f index.html* *.gif */index.html* */*.gif robots.txt ls -al /root/  if [ -d /usr/local/psa ]     then         echo plesk > /root/bin/INST_SERVER_TYPE.txt     chmod 775 /root/bin/*     /root/bin/setting_server_ve.sh     rm -rf /root/etc | rm -rf /root/bin | rm -rf /root/log | rm -rf /root/old     sed -i "75s/false/true/" /etc/permissions/jail.conf         exit 1; elif [ -d /var/webmin ]     then     echo webmin > /root/bin/INST_SERVER_TYPE.txt     chmod 775 /root/bin/*     /root/bin/setting_server_ve.sh     rm -rf /root/etc | rm -rf /root/bin | rm -rf /root/log | rm -rf /root/old     sed -i "67s/false/true/" /etc/permissions/jail.conf         break     exit 1; else     echo no-gui > /root/bin/INST_SERVER_TYPE.txt     chmod 775 /root/bin/*     /root/bin/setting_server_ve.sh     rm -rf /root/etc | rm -rf /root/bin | rm -rf /root/log | rm -rf /root/old     sed -i "67s/false/true/" /etc/permissions/jail.conf         break     exit 1; fi   
like image 304
RFH Avatar asked Jan 24 '12 01:01

RFH


People also ask

Can a shell script delete itself?

Only when the last program closes its handle does the file really get deleted. So, in a sense, yes, a shell script can delete itself, but it won't really be deleted until after the execution of that script finishes.

How do I delete a shell script?

To delete a specific file, you can use the command rm followed by the name of the file you want to delete (e.g. rm filename ). For example, you can delete the addresses.


1 Answers

rm -- "$0" 

Ought to do the trick. $0 is a magic variable for the full path of the executed script.

like image 154
richo Avatar answered Sep 22 '22 14:09

richo