self-deleting shell script

后端 未结 4 2043
予麋鹿
予麋鹿 2021-01-30 10:43

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

相关标签:
4条回答
  • 2021-01-30 10:50

    This works for me:

    #!/bin/sh
    
    rm test.sh
    

    Maybe you didn't really mean to have the '$' in '$test.sh'?

    0 讨论(0)
  • 2021-01-30 10:54

    The script can delete itself via the shred command (as a secure deletion) when it exits.

    #!/bin/bash
    
    currentscript="$0"
    
    # 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
    
    0 讨论(0)
  • 2021-01-30 11:03
    rm -- "$0"
    

    Ought to do the trick. $0 is a magic variable for the full path of the executed script.

    0 讨论(0)
  • 2021-01-30 11:05

    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.

    Modify the script so it has the necessary chkconfig headers and place it in /etc/init.d/ so it is run at every boot.

    That way you can rerun the script at a later time simply by recreating the trigger script.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题