2011-08-25 4 views
2

응용 프로그램과 함께 사용되는 추가 기능 도구를 정리하는 제거 스크립트가 있습니다. 스크립트 버전은 Windows와 Linux에서 실행됩니다.Linux Bash 및 Windows 일괄 처리를위한 자체 삭제 스크립트

제거 스크립트 파일과 스크립트가 실행되는 디렉토리 (Windows 배치 파일의 경우와 Linux bash 파일의 경우)를 삭제할 수 있기를 바랍니다. 현재 스크립트가 실행되는 디렉토리와 디렉토리를 제외한 모든 것이 실행 된 후에도 남아 있습니다.

스크립트 및 스크립트의 디렉토리를 삭제하려면 어떻게해야합니까?

감사

답변

9

배쉬에서, 당신은 내가 '돈 있지만, 내가 삭제할 수있는 스크립트를 얻을 수 있었다지만, 디렉토리가 삭제하지 않는 것이 사용

#!/bin/bash 
# do your uninstallation here 
# ... 
# and now remove the script 
rm $0 
# and the entire directory 
rmdir `dirname $0` 
+0

할 수 오류를 보지 마라. –

+0

디렉토리에 다른 숨겨진 파일이 있습니까? – leon

+1

오른쪽; 디렉토리를 안전하게 지울 수 있다고 확신한다면''rm -rf'dirname $ 0'''을 사용하면됩니다. –

3
#!/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" 
관련 문제