自删除shell脚本 [英] self-deleting shell script

查看:72
本文介绍了自删除shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找关于这个问题的答案,但是找不到答案.

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

我写了一个简单的脚本来进行初始服务器设置,我希望它在完成时从根目录中删除/取消链接本身.我尝试了许多用谷歌搜索的解决方案(例如/bin/rm $ test.sh),但脚本似乎始终保持不变.这可能吗?下面是到目前为止的脚本.

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  

推荐答案

rm -- "$0"

应该做到这一点.$ 0是执行脚本完整路径的不可思议变量.

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

这篇关于自删除shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆