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
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.
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.
rm -- "$0"
Ought to do the trick. $0 is a magic variable for the full path of the executed script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With