# Code that will only execute once

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.

• @PeterTaylor a possible Solution would be the a self delete but as the result are showing it isn't the only one. – Lukei May 28 '14 at 7:21
• 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. – orion May 28 '14 at 20:41
• The site is free for you to propose such a question. – ojblass May 30 '14 at 22:40
• echo "If you try to execute me again, it means you are an idiot."; <-- Nobody will execute more than once :P – user3459110 Jun 5 '14 at 12:45
• Would missile-related software have qualified? ;) – rsegal Jun 8 '14 at 4:28

# x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
source incbin __FILE__
len equ $- source SECTION .text global _start _start: pop rdi ;number of parameters pop rdi ;path to executable, parameter of unlink and open mov rax, 87 ;unlink syscall cmp rax, 0 jl error mov rax, 2 ;open mov rsi, 0101o ;O_WRONLY O_CREAT mov rdx, 0600o ;permissions on created file syscall cmp rax, 0 jl error mov rdi, rax ;file (return value of open) mov rax, 1 ;write mov rsi, source mov rdx, len syscall cmp rax, 0 jl error mov rax, 60 ;exit mov rdi, 0 ;return code syscall error: mov rax, 60 mov rdi, 1 syscall  Compile with: nasm -f elf64 FILENAME ld -m elf_x86_64 FILENAME.o -o FILENAME  Or the same thing in C (with inline assembly): #include <stdio.h> extern char src; asm("src: .incbin \"" __FILE__ "\"\n.byte 0"); int main(int argc, char *argv[]) { unlink(argv[0]); FILE *file = fopen(argv[0], "w"); fprintf(file, "%s",&src); fclose(file); return 0; }  When run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code. There must be a better way to do this in C (or maybe not), but I don't know any. Isn't that much better than just having it delete itself! • Funny way to not answer the challenge. +1 for hack value. – Stéphane Gourichon Jan 9 '16 at 6:34 • The C version is not 64-bit specific or even architecture-specific, is it? So it is actually portable (x86 vs MIPS, ARM etc, Linux vs other OS even Windows), isn't it ? – Stéphane Gourichon Jan 9 '16 at 6:35 • @StéphaneGourichon I think so, though I haven't tried it on anything by x86_64 linux. – Ian D. Scott Jan 9 '16 at 16:11 # GW-BASIC, 20 bytes 1 COLOR 3 2 SCREEN 1  This is a fun one. COLOR 3 sets the foreground color to cyan. SCREEN 1 sets the output screen to one that does not have color. Therefore, you can run the program once, but if you try to run it again: # Ruby, 15 14 Put this line to a file (del.rb): File.delete$0


then run it (self-destructive) : ruby del.rb del.rb

• you can save 1 character by using :File.delete $0 – Mhmd May 28 '14 at 7:45 # PHP - 23 <?=fopen(__FILE__,'w');  • I wonder if it is possible to drop the semicolon here. I know it's possible with short tags like this: <?=$var ?>. – nalply Jun 1 '14 at 12:09
• @naiply: No. ?> implies semicolon (you can even get a syntax error mentioning ;), but end of document doesn't. – Konrad Borowski Jun 1 '14 at 14:33

# Coffescript

_=->_=1

compiles to:

var _;

_ = function() {
return _ = 1;
};


# x86 Machine Code (5 bytes)

HEX:

EA0000FFFF


ASM:

JMP FFFF:0000


You can try with debug.exe:

C:\>debug
-a 100
0AE7:0100 jmp ffff:0000
0AE7:0105
-g=100


Explanation: BIOSis always at FFFF:0000 in memory. So this sequence boots the computer, if this is run in a protected command prompt in Windows it makes that process unresponsive.

• Would you mind adding an explanation for those not that familiar with machine code? – Martin Ender Jun 2 '14 at 9:44

# Bash: 4 chars

rm a


Put this in a file named a and run it on your Linux machine.

• rm * will work for every filename – Antonio Ragagnin May 28 '14 at 12:24
• @AntonioRagagnin It's better to stick to non-destructive scripts – Daniel May 28 '14 at 12:52
• rm $0 will also work for all filenames, at the cost of one character. – fNek May 31 '14 at 19:05 • @AntonioRagagnin So will rm -rf / --no-preserve-root, but better ;) – Cole Johnson May 31 '14 at 21:49 • @fNek No, it won't. Only for those without a whitespace in their name. – glglgl May 31 '14 at 21:57 # 𝔼𝕊𝕄𝕚𝕟 (noncompetitive) ɟhtml”Ĭ)  Try it here (Firefox only). Translates to $('html').remove() in Javascript.

• What if my HTML page has no <html> element? – NoOneIsHere Jun 7 '16 at 20:18
• ESMin runs in the online interpreter, so that shouldn't be a problem – Mama Fun Roll Jun 8 '16 at 16:35

## AutoHotKey, 39 bytes

FileDelete %A_ScriptDir%\%A_ScriptName%


(Yes, I'm aware this is an ancient thread)

# Dyalog APL (non-competing because it is too late)

f←⎕EX'f'∘⊣


f can only be called once. It needs an argument, which is ignored:

      f←⎕EX'f'∘⊣
f 'dummy argument'
f 'dummy argument'
VALUE ERROR
f'dummy argument'
∧


It is a so-called "atop", which is a train of two functions. The left one is applied on the result of the right one.

The right function is 'f'∘⊣ which replaces any given argument with the string 'f'.

The left function is ⎕EX, which EXpunges the object named in its argument.

The first time around, 'f'∘⊣ returns 'f', which causes ⎕EX to erase f. The second time around, calling f results in an error because f does not exist.

Here is one that can be tried online:

f←{f∘←¨}


After defining the function, the first time that it is called (with a dummy argument), e.g. f 4, the function ignores the argument, and instead redefines itself to be an operator (higher-order function). Now, operators need at least a left operand so when it is called again, the call will fail with a syntax error.

• Who said "too late" means "non-competitive"? – Erik the Outgolfer Apr 23 '16 at 7:54
• @ΈρικΚωνσταντόπουλος Mama Fun Roll. But you did make me take a look at what I had written, and I found a typo. Thanks! – Adám Apr 25 '16 at 17:27

# Linux, 8 bytes

killall5


Similar to this answer, but this works on pretty much any Linux since System V and you don't need to be root. Don't believe me? Try it out on your Linux system! You'll want to save any work first...

I actually found this by accident when I was 11 messing around with Linux for the first time.

# Commodore 64 BASIC, 7 bytes

POKE 1,3


I've had a lot of fun in various BASIC dialects POKE-ing around in the first few memory storage locations. Here's the effect of this one:

Try it online! You'll have to type it in yourself.

Some other POKEs:

• POKE 1,1 will freeze the system.
• POKE 1,2 will clear the screen.
• POKE 1,4 completely kills the system - the power button won't even work.
• POKE 1,5 through POKE 1,9 do the same as POKE 1,4, but with varying levels of destruction.

# R, 7 Bytes

==$  The code makes the "=" (used for assigning) become the "$" (used for subseting). After that it gives error.

• I don't think this is valid. If I put it in a file I can run it as many times as I want, right? – Esolanging Fruit Jul 9 '17 at 23:41
• @EsolangingFruit, no on repeat execution this throws the error Error in = = $ : object of type 'special' is not subsettable as validated in R version 3.4.3 (2017-11-30) – Taylor Scott Mar 13 '18 at 5:34 # Error safe termination (Python 2) from contextlib import contextmanager @contextmanager def suicide(): try: yield finally: open(__file__ , 'w'); open(__file__+'c', 'w'); #-------------------------------------- # Error safe code ;) #------------------------------------- with suicide(): print "Goodbye cruel world!" jump_off_building_______crash  • I like how this "suicides". – Esolanging Fruit Jul 9 '17 at 23:39 ## Plain TeX - 31 (24) chars \openout1=\jobname\write1{}\bye  If you save it as file a.tex then: \openout1=a\write1{}\bye  # Bash This deletes the file and makes sure that you can't recover it. So it definitely can't execute more than once. shred "$0"


shred is a program to securely delete files by overwriting them. It's in coreutils.

JAVASCRIPT (35 26 bytes when minified, in case that matters.) I know this is a late entry, but I had a situation where I considered being able to kill a function and decided it'd make a good entry here. (Still not sure if I'll actually use it.)

funny = function(){
window.funny = '';
};

funny();
funny();


minified

a=function(){alert(0);window.a=""};


a=function(){window.a=""};

• Just do function a(){a=0} – minmaxavg Mar 30 '16 at 22:05

# Python 2.7

import inspect
import os
for x in range(0, 2):
os.remove(inspect.stack()[0][1])


It only runs 1/2 of a time.

# C, 56 chars

x[9];main(c,v)int**v;{sprintf(x,"rm %s",*v);system(x);}


Run on a UNIX system. By convention, the first string param passed to a C program is the executable name. This program simply deletes the executable (but leaves the source, which you have to recompile to run it again).

# TIS-100, 6 bytes

@0
HCF


The HCF (or halt and catch fire) command instantly crashes the TIS-100. The only way to run it again is to start it up again.

• Invalid, I'm afraid. From the challenge: "Any answers just initiating a reboot or halt will be disqualified." – steenbergh Nov 15 '17 at 16:22

# R16K1S60 Assembly

mov [ax], ax


Simply overrides itself with with the contents of AX. the mov instruction is only one word large, so it deletes itself. Depends on AX being 0 (most releases guarentee this)

## Safe version

mov [0], ax


Depends on ax not being this specific instruction

• Woah, I never thought I'd see this language here. – 12Me21 Mar 13 '18 at 19:45
• @12Me21 Never say that around PPCG. Ever. We use everything ;) – moonheart08 Mar 16 '18 at 6:53
• I wonder if LBPHacker will ever release the new computer he made. I'm guessing he's either really busy or he's just started over. Anyway, it might be better to use mov [0], ax since it doesn't rely on ax being 0. – 12Me21 Mar 16 '18 at 15:21
• @12Me21 I can confirm he hasn't started over. He's just busy. Also, join #powder on freenode. – moonheart08 Mar 16 '18 at 20:01
• I believe the instruction pointer actually points to the next memory cell, not the current one. – 12Me21 Mar 16 '18 at 20:17

# Python 24

Name the file 'q'

import os
os.remove("q")

• try os.remove(__file__) instead. – Erik the Outgolfer Apr 23 '16 at 7:50

Pre base-4.6.0.0 : May not work on Windows. This depends on how the program is invoked.

import System.Environment
import System.Directory
main=getProgName>>=removeFile


Post base-4.6.0.0 : A bit longer but always works.

import System.Environment
import System.Directory
main=getExecutablePath>>=removeFile

• It's now a popularity contest. – Isiah Meadows May 30 '14 at 5:41

# 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. 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 ! • you know the is optional? so you can just write set contents of front document to "" – dkudriavtsev Oct 10 '16 at 19:51 Shell script (Linux Bash); 4 characters rm *  Warning ! Don’t try this at home. This shell script can be run only once. When run, this shell script will self-destruct. • For non-bash users: * is a glob, which expands to a list of all the files in the current directory. so it will delete everything in the folder the script is in as well as itself – user16402 Jun 9 '14 at 19:26 • This implicitly assumes that the process' "current directory" matches the script directory. The "run once" property will fail in other cases. Example: say script is in ~/a, current directory is ~/b. The script can be run with ../a/thescript and it will only try to delete in ~/b which won't delete the script (it's not in that directory). b may even be already empty before the script runs. – Stéphane Gourichon Jan 9 '16 at 6:31 # 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. ## Caché ObjectScript, 12 bytes k ^rOBJ($zn)


Output:

SAMPLES>do ^test

SAMPLES>do ^test

DO ^test
^
<NOROUTINE> *test


# q (14 bytes)

This program deletes itself when run.

hdel hsym .z.f


### 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]

• That's not really different from typing exit or ^D`. The later key combination will send EOF to the shell making it 0 bytes. – kasperd Nov 7 '16 at 16:19