6

アプリケーションで使用されるアドオンツールをクリーンアップするアンインストールスクリプトがあります。スクリプトのバージョンは、WindowsとLinuxの両方で実行されます。

アンインストールスクリプトファイルと、スクリプトが実行されているディレクトリも削除できるようにしたいと思います(Windowsバッチファイルの場合とLinux bashファイルの場合の両方)。現在、スクリプトとそれが実行されるディレクトリ以外のすべては、実行後も残ります。

スクリプトとスクリプトのディレクトリを削除するにはどうすればよいですか?

ありがとう

4

2 に答える 2

13

Bashでは、次のことができます

#!/bin/bash
# do your uninstallation here
# ...
# and now remove the script
rm $0
# and the entire directory
rmdir `dirname $0`
于 2011-08-25T23:19:14.723 に答える
4
#!/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"
于 2013-08-20T14:49:42.090 に答える