bash 自删除shell脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8981164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
self-deleting shell script
提问by RFH
I've looked around for an answer to this one but couldn't find one.
我环顾四周寻找这个问题的答案,但找不到。
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.
我编写了一个简单的脚本来进行初始服务器设置,我希望它在完成时从根目录中删除/取消链接。我已经尝试了许多我在 google 上搜索到的解决方案(例如 /bin/rm $test.sh),但该脚本似乎总是保持原样。这可能吗?以下是我到目前为止的脚本。
#! /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
回答by richo
rm -- "#!/bin/sh
rm test.sh
"
Ought to do the trick. $0 is a magic variable for the full path of the executed script.
应该做的伎俩。$0 是执行脚本完整路径的魔法变量。
回答by ziesemer
This works for me:
这对我有用:
#!/bin/bash
currentscript="##代码##"
# Function that is called when the script exits:
function finish {
echo "Securely shredding ${currentscript}"; shred -u ${currentscript};
}
# Do your bashing here...
# When your script is finished, exit with a call to the function, "finish":
trap finish EXIT
Maybe you didn't really mean to have the '$' in '$test.sh'?
也许你并不是真的想在'$test.sh'中使用'$'?
回答by ziesemer
The script can delete itself via the shred command(as a secure deletion) when it exits.
脚本退出时可以通过shred 命令(作为安全删除)删除自身。
##代码##回答by Bram
Why remove the script at all? As other have mentioned it means you have to keep a copy elsewhere.
为什么要完全删除脚本?正如其他人所提到的,这意味着您必须在其他地方保留一份副本。
A suggestion is to use a "firstboot" like approach. Simply create an empty file in e.g. /etc/sysconfig that triggers the execution of this script if it is present. Then remove that file at the end of the script.
一个建议是使用类似“firstboot”的方法。只需在例如 /etc/sysconfig 中创建一个空文件,它会触发此脚本的执行(如果它存在)。然后在脚本末尾删除该文件。
Modify the script so it has the necessary chkconfig headers and place it in /etc/init.d/ so it is run at every boot.
修改脚本,使其具有必要的 chkconfig 标头并将其放置在 /etc/init.d/ 中,以便在每次启动时运行。
That way you can rerun the script at a later time simply by recreating the trigger script.
这样您就可以在以后重新运行脚本,只需重新创建触发器脚本即可。
Hope this helps.
希望这可以帮助。