Самораспаковывающийся скрипт для Linux Bash и Windows Batch
У меня есть скрипт удаления, который очищает дополнительный инструмент, используемый с приложением. Версии скрипта работают как в Windows, так и в Linux.
Я хотел бы иметь возможность удалить файл сценария удаления, а также каталог, в котором выполняется сценарий (как в случае пакетного файла Windows, так и в случае bash-файла Linux). Прямо сейчас все, кроме скрипта и каталога, в котором он выполняется, остается после его запуска.
Как я могу удалить скрипт и каталог скрипта?
Спасибо
2 ответа
Решение
В Bash вы можете сделать
#!/bin/bash
# do your uninstallation here
# ...
# and now remove the script
rm $0
# and the entire directory
rmdir `dirname $0`
#!/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"