251
\$\begingroup\$

Goal

The goal of this challenge is to write code that will execute once and only once. This means basically that it damages the program, script, or environment in some way. If rebooting the system allows the code to run again that is permitted.

Scoring

Number of votes. All assumptions must be clearly listed. Any answers just initiating a reboot or halt will be disqualified.

Additional Rules because Greg Hewgill is a demi-god

No root access is permitted.

End Date

The contest will close on May 31, 2014.

Edit

This contest has been changed to popularity contest.

\$\endgroup\$
17
  • 8
    \$\begingroup\$ possible duplicate of A program that deletes itself \$\endgroup\$ Commented May 28, 2014 at 7:04
  • 7
    \$\begingroup\$ @PeterTaylor a possible Solution would be the a self delete but as the result are showing it isn't the only one. \$\endgroup\$
    – Lukei
    Commented May 28, 2014 at 7:21
  • 118
    \$\begingroup\$ To a lot of people calling vi in a single terminal environment has this effect, no escape from it unless you reboot :) Just a joke here. \$\endgroup\$
    – orion
    Commented May 28, 2014 at 20:41
  • 24
    \$\begingroup\$ echo "If you try to execute me again, it means you are an idiot."; <-- Nobody will execute more than once :P \$\endgroup\$ Commented Jun 5, 2014 at 12:45
  • 35
    \$\begingroup\$ Would missile-related software have qualified? ;) \$\endgroup\$
    – rsegal
    Commented Jun 8, 2014 at 4:28

82 Answers 82

1 2
3
1
\$\begingroup\$

PowerShell (37)

Save following code to a script file (test.ps1)

rm $MyInvocation.MyCommand.Definition

Run from command line:

PS > .\test.ps1

Script will automatically delete itself.

\$\endgroup\$
1
\$\begingroup\$

AppleScript ; 17 chars

delete document 1

I was playing with more elegant solutions like this one :

set the contents of the front document to ""

But the action of delete document 1 is even more fun !

\$\endgroup\$
1
  • \$\begingroup\$ you know the is optional? so you can just write set contents of front document to "" \$\endgroup\$
    – anna328p
    Commented Oct 10, 2016 at 19:51
1
\$\begingroup\$

beeswax, 12 bytes

_8F+++P]f1Fw

Save this program under the name ! and execute it.

              lstack                       gstack
_             [0,0,0]•                                          create bee
 8            [0,0,8]•                                          lstack 1st=8
  F           [8,8,8]•                                          all lstack=lstack 1st
   +++        [8,8,16]•                                         1st=1st+2nd
              [8,8,24]•                                         3 times
              [8,8,32]•
      P       [8,8,33]•                                         increment 1st
       ]      [8,8,2377900603251621888]•                        rotate bits of 1st by 2nd steps
        f                               [2377900603251621888]•  push lstack 1st ont gstack
         1    [8,8,1]•                                          lstack 1st=1
          F   [1,1,1]•                                          all lstack=lstack 1st
           w            write gstack to file. lstack 1st=bytes used for file name, lstack 2nd= used file content bytes.

If we look at the stack contents in hex, it gets clearer what’s happening:

lstack[8,8,33]• is lstack[0x0000000000000008,0x000000000000008,0x0000000000000021]• in hex.

If we rotate the bits of the 1st lstack value by 8 to the right, we get

lstack[8,8,2377900603251621888]•, which is

lstack[0x0000000000000008,0x000000000000008,0x2100000000000000]• in hex.

Instruction f pushes the 1st lstack value on the gstack:

gstack[0x2100000000000000]•

Now comes instruction w: First, the 4-byte words of the gstack get reinterpreted as a stack of UInt8 values:

[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21]•, in little endian order. The MSB is on top of the stack.

Instruction w takes the 1st lstack value as number of bytes taken for the file name, and the 2nd lstack value gives the number of bytes stored in the file. lstack[1,1,1]• means that 1 byte is taken as file name. 0x21 is the ASCII code for !, which is the name of the program itself. The next single byte (lstack 2nd), the value 0x00 is stored as file content, which is not executable as beeswax program.

\$\endgroup\$
1
\$\begingroup\$

Caché ObjectScript, 12 bytes

k ^rOBJ($zn)

Output:

SAMPLES>do ^test

SAMPLES>do ^test

DO ^test
^
<NOROUTINE> *test
\$\endgroup\$
1
\$\begingroup\$

q (14 bytes)

This program deletes itself when run.

hdel hsym .z.f
\$\endgroup\$
1
\$\begingroup\$

Bash (7 characters)

TMOUT=1

It destroying the user session within a second by terminating the shell. See: man bash.

Example:

$ TMOUT=1
timed out waiting for input: auto-logout
Saving session...completed.

[Process completed]
\$\endgroup\$
1
  • \$\begingroup\$ That's not really different from typing exit or ^D. The later key combination will send EOF to the shell making it 0 bytes. \$\endgroup\$
    – kasperd
    Commented Nov 7, 2016 at 16:19
1
\$\begingroup\$

8602/8610 machine code, 4 bytes

assuming that the program is stored at address $0800

78 4C 00 08

breakdown to assembler:

START    sei        // disable interupts
         jmp START  // endless loop

C64 machine code, 3 bytes

4C E2 FC

this jumps to the builtin reset routine of the C64 (which, among other actions, clears the RAM)

run with SYS <address>

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This should be two different answers. \$\endgroup\$
    – MD XF
    Commented May 30, 2017 at 16:25
1
\$\begingroup\$

OSI Challenger 1P

POKE 3,3

There are lots of BASIC answers because BASIC made the mistake of allowing users to directly access all memory. But this is the shortest POKE command yet. If only it was ...

You can try this here - scroll down, hit Run, hit C to cold-boot, then Enter-Enter to get into BASIC mode. Then type POKE 3,3 and watch the PC burn.

\$\endgroup\$
0
1
\$\begingroup\$

Braingolf Inline Interpreter, 28 bytes [non-competing]

'''
open(__file__,'w')
a='''

Braingolf's Inline Interpreter treats the braingolf code as simply a multiline string within the Python3 interpreter.

This allows what is effectively an injection attack, where you can end the multiline string with three single quotes, then insert python code. The code will only run if you then begin a new multiline quote at the end, however.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't have to add non-competing (that rule was removed). \$\endgroup\$ Commented Jul 9, 2017 at 23:36
  • \$\begingroup\$ Also, doesn't this wipe the interpreter file, and not the program file? \$\endgroup\$ Commented Jul 9, 2017 at 23:37
1
\$\begingroup\$

JS, 8B

a=b=>a=0

calling a means a=0, thus a can't be called 2×.

\$\endgroup\$
1
\$\begingroup\$

SmileBASIC

@memory_leak
If shift("this") then @memory_leak

There is a bug where using POP() or SHIFT() on a string literal causes a memory leak, and eventually there will not be enough memory left for the program to run. Trying to run it again will cause an error before the program starts.

IF ... THEN @LABEL is a shortcut for IF ... THEN GOTO @LABEL.

\$\endgroup\$
2
  • \$\begingroup\$ Oh cool to see that its If ... Then @Label in SmileBASIC - it is If ... GoTo Label In the VB family \$\endgroup\$ Commented Mar 13, 2018 at 5:36
  • 1
    \$\begingroup\$ SmileBASIC actually supports both forms, for some reason. (I think I had forgotten about it when I wrote this answer) \$\endgroup\$
    – 12Me21
    Commented Mar 13, 2018 at 17:17
1
\$\begingroup\$

MOO, 24 bytes

delete_verb(this, verb);

Requires that this code is run as an actual verb, rather than through eval (or $code_utils:eval_d).

\$\endgroup\$
1
\$\begingroup\$

AWK, 41 bytes

This script file overwrites itself with an empty string. Tested in mawk and gawk. Be aware that this only work in shell environments where the _ variable is defined.

#!/bin/awk -f
BEGIN{print"">ENVIRON["_"]}
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 9 bytes in console

const a=0

throws an 'identifier already declared error' when run again

\$\endgroup\$
2
  • 1
    \$\begingroup\$ this is in the JavaScript console REPL, right? If so, you'll need to specify that. \$\endgroup\$
    – Razetime
    Commented Mar 4, 2021 at 12:37
  • \$\begingroup\$ let saves two bytes \$\endgroup\$
    – emanresu A
    Commented Aug 13, 2021 at 4:41
1
\$\begingroup\$

Minecraft Function

datapack disable "file/pack"

Must be run as a function in a data pack named pack. Disables the data pack, thus making it impossible to run the function without re-enabling it.

Output from running /function pack:function twice: Executed 1 command(s) from function 'pack:function

\$\endgroup\$
0
\$\begingroup\$

Bash: 2 chars

>a

Using inspiration from a couple of the other solutions, I came up with this solution in only two chars. Put this in a script called a and run it.

Just like the posts I got inspired by, this relies on the calling shell working around the missing #! line. Relying on this workaround is absolutely necessary, since you can't even produce a valid #! line in that space.

This happens if you let the kernel call it without a shell to work around the missing #! line:

$ strace ./a
execve("./a", ["./a"], [/* 47 vars */]) = -1 ENOEXEC (Exec format error)
\$\endgroup\$
6
  • \$\begingroup\$ This script does nothing when I run it. I can run it several times. \$\endgroup\$ Commented Jun 3, 2014 at 18:21
  • \$\begingroup\$ @NicolasBarbulesco Make sure you run it from the directory where it is located. The contents of the script will be gone after the first run, there won't be anything left to run the second time around. \$\endgroup\$
    – kasperd
    Commented Jun 3, 2014 at 18:34
  • \$\begingroup\$ The second run shows nothing, like the first run, so I did not see what happened. In fact, the first run erases the script's content. How is this achieved ? \$\endgroup\$ Commented Jun 7, 2014 at 13:09
  • 2
    \$\begingroup\$ @NicolasBarbulesco > redirects the output of the command on that line to a file. The file name to redirect the output to is a. So the script runs an empty command, and the output of that empty command is written to the file a, which is the script itself. \$\endgroup\$
    – kasperd
    Commented Jun 7, 2014 at 13:55
  • 2
    \$\begingroup\$ @NicolasBarbulesco It is easier to fit something simple into two bytes than to fit something complicated into two bytes. \$\endgroup\$
    – kasperd
    Commented Jun 7, 2014 at 18:19
0
\$\begingroup\$

AutoIt - 27 bytes

Truncs itself to 0:

FileOpen(@ScriptFullPath,2)
\$\endgroup\$
0
\$\begingroup\$

Batch (This isn't code golf)

echo "">%0.bat

Assumes you call it without the extension. If you are calling it with the extension, use echo "">%0.

\$\endgroup\$
0
\$\begingroup\$

Vitsy, 8 bytes (I know it isn't Golf but I like to be concise)

iG" mr",
iG        Push name of self.
  " mr"   Push "rm " in front of it.
       ,  Execute as shell.

This assumes you run on a computer with rm as a command for delete. For an environment with del as its command for delete, it's 9 bytes (I think, I can't really test it):

iG" led",
iG        Push name of self.
  " led"  Push "del " in front of it.
        , Execute as shell.

It basically just deletes itself. :P

\$\endgroup\$
0
\$\begingroup\$

Z80 ASM on TI-83/84 series

AsmPrgm
C3FFFF

If you put this into a TI-83 or 84 (any version, except 84+CE and CSE), the calculator will completely reset itself. Everything in RAM will be reset, including the program at hand.

For the TI84+CSE:

Asm84CPrgm
C3FFFF

Disclaimer: This might not work if you are running any apps that change the functionality of your calculator significantly. (i.e. libraries, non-official OS's)

\$\endgroup\$
-1
\$\begingroup\$

tcl, 7 bytes

vwait v

Explanation

\$\endgroup\$
2
-3
\$\begingroup\$

Bash file linux

#!/bin/bash
shutdown -h now
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Welcome to the site! I do believe that shutdown requires root access (It does when I test it) which is forbidden by the challenge. \$\endgroup\$
    – Wheat Wizard
    Commented Mar 13, 2018 at 5:52
  • \$\begingroup\$ On some/many distros it does NOT require root, any LOCAL user can run shutdown(8). \$\endgroup\$
    – Oskar Skog
    Commented Jun 9, 2021 at 16:26
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.