首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Linux Bash和Windows批处理的自删除脚本

Linux Bash和Windows批处理的自删除脚本
EN

Stack Overflow用户
提问于 2011-08-26 07:14:00
回答 2查看 7.4K关注 0票数 6

我有一个卸载脚本,可以清理应用程序中使用的附加工具。该脚本的不同版本可以在Windows和Linux上运行。

我希望能够删除卸载脚本文件以及运行脚本的目录(在Windows批处理文件和Linux bash文件的情况下)。现在,除了脚本和运行它的目录之外,所有东西在它运行后都会保留。

如何删除脚本和脚本目录?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-26 07:19:15

在Bash中,您可以

代码语言:javascript
运行
复制
#!/bin/bash
# do your uninstallation here
# ...
# and now remove the script
rm $0
# and the entire directory
rmdir `dirname $0`
票数 13
EN

Stack Overflow用户

发布于 2013-08-20 22:49:42

代码语言:javascript
运行
复制
#!/bin/bash
#
# Author: Steve Stonebraker
# Date: August 20, 2013
# Name: shred_self_and_dir.sh
# Purpose: securely self-deleting shell script, delete current directory if empty
# http://brakertech.com/self-deleting-bash-script

#set some variables
currentscript=$0
currentdir=$PWD

#export variable for use in subshell
export currentdir

# function that is called when the script exits
function finish {
    #securely shred running script
    echo "shredding ${currentscript}"
    shred -u ${currentscript};

    #if current directory is empty, remove it    
    if [ "$(ls -A ${currentdir})" ]; then
       echo "${currentdir} is not empty!"
    else
        echo "${currentdir} is empty, removing!"
        rmdir ${currentdir};
    fi

}

#whenver the script exits call the function "finish"
trap finish EXIT

#last line of script
echo "exiting script"
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7198281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档