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.
83 Answers 83
Here's a couple. Unlike many, these are not destructive, just creative and unorthodox.
Bash
#!/usr/bin/env bash
read <<< ''
printf "$REPLY" > "0ドル"
This one's pretty simple. The variable $REPLY
is created implicitly by read
, but filled with an empty herestring. That empty string is then printf
'ed into the current script. It is a rather obfuscated equivalent of the following:
#!/usr/bin/env bash
cat <<< '' > "0ドル" # probably a lot more portable
Windows Batch
copy con %0 < nul
The second one basically copies the console input, read from nul
, into the current file.
-
\$\begingroup\$ Posted after this became a popularity contest \$\endgroup\$Claudia– Claudia2014年05月30日 05:31:21 +00:00Commented May 30, 2014 at 5:31
-
1\$\begingroup\$ The first one won't work without root permissions. Why are you trying to replace the null device? When run as root, this would actually make the system behave erratically. You are also overwriting the null while writing into it :) Why not just move it anywhere else? Such as mv "0ドル" "${0}x" or something? It should do the trick. \$\endgroup\$orion– orion2014年05月30日 09:14:11 +00:00Commented May 30, 2014 at 9:14
-
\$\begingroup\$ Does the file still exist afterwards? \$\endgroup\$Claudia– Claudia2014年05月30日 16:34:53 +00:00Commented May 30, 2014 at 16:34
-
\$\begingroup\$ I now completely changed the Bash example. \$\endgroup\$Claudia– Claudia2014年05月30日 18:03:39 +00:00Commented May 30, 2014 at 18:03
-
\$\begingroup\$ How are these not destructive? \$\endgroup\$ysap– ysap2014年06月05日 23:05:26 +00:00Commented Jun 5, 2014 at 23:05
Minecraft Command Block
setblock ~ ~ ~ air
When you run this in a command block, it destroys itself.
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: BIOS
is 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.
-
2\$\begingroup\$ Would you mind adding an explanation for those not that familiar with machine code? \$\endgroup\$Martin Ender– Martin Ender2014年06月02日 09:44:12 +00:00Commented Jun 2, 2014 at 9:44
ARM Linux full standalone ELF, (削除) 50 (削除ここまで) (削除) 46 (削除ここまで) 45 bytes
7f 45 4c 46 01 00 00 00 00 00 00 00 00 00 01 00
02 00 28 00 21 00 01 00 21 00 01 00 04 00 00 00
01 98 0a 27 00 df 01 27 00 df 20 00 01
Adapted version of the code used in A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux.
Assembler source (with raw encoded ELF header and comments, most of which are the same as in the article)
@ Adaptation of
@ https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
@ for an ARM binary which deletes itself. XD
@ $ arm-none-eabi-as -march=armv4t -mthumb runonce-arm.s -o runonce-arm.o
@ $ arm-none-eabi-ld -Ttext 0x00010000 runonce-arm.o -o runonce-arm.elf
@ $ arm-none-eabi-objcopy -O binary runonce-arm.elf runonce-arm
.text @ A lie.
.arch armv4t @ Set architecture version
.thumb @ Needs to be Thumb to fit. ARM is too thicc.
.syntax unified
.org 0 @ Start at offset 0
Elf32_Ehdr:
.byte 0x7F,'E','L','F' @ e_ident
Elf32_Phdr:
.word 1 @ p_type
.word 0 @ p_offset
.word 0x00010000 @ same as -Ttext @ p_vaddr
.short 2 @ ET_EXEC @ e_type @ p_paddr
.short 40 @ EM_ARM @ e_machine
.word _start @ e_version @ p_filesz
.word _start @ e_entry @ p_memsz
.word 4 @ Elf32_Phdr @ e_phoff @ p_flags
@ Here is some space to put our code in.
.thumb_func
.globl _start
_start:
@ r0 = argv[0] from _start state
ldr r0, [sp, #4] @ e_shoff @ p_align
@ unlink(argv[0])
movs r7, #0x0a @ unlink
svc #0 @ syscall @ e_flags
@ exit(dontcare)
movs r7, #0x01 @ exit
svc #0 @ syscall @ e_ehsize
@ unreachable
@ finish the header
.short 0x20 @ e_phentsize
.byte 1 @ e_phnum
@ Truncated header because for some reason, Linux accepts this.
Basically, I took this program:
#include <unistd.h>
int main(int argc, char *argv[])
{
unlink(argv[0]);
}
Or if it is easier to read,
main(c,v)int*v;{unlink(*v);}
And squeezed it into an ELF header using the method in that article, so it is 45 bytes with no dependencies on libc.
Works every time except when it is called from $PATH
.
Error safe termination (Python 2/3)
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
-
\$\begingroup\$ I like how this "suicides". \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年07月09日 23:39:42 +00:00Commented Jul 9, 2017 at 23:39
Ruby, (削除) 15 (削除ここまで) 14
Put this line to a file (del.rb
):
File.delete 0ドル
then run it (self-destructive) :
ruby del.rb del.rb
-
1\$\begingroup\$ you can save 1 character by using :
File.delete 0ドル
\$\endgroup\$Mohammad– Mohammad2014年05月28日 07:45:02 +00:00Commented May 28, 2014 at 7:45
PHP - 23
<?=fopen(__FILE__,'w');
-
\$\begingroup\$ I wonder if it is possible to drop the semicolon here. I know it's possible with short tags like this:
<?= $var ?>
. \$\endgroup\$nalply– nalply2014年06月01日 12:09:29 +00:00Commented Jun 1, 2014 at 12:09 -
\$\begingroup\$ @naiply: No.
?>
implies semicolon (you can even get a syntax error mentioning;
), but end of document doesn't. \$\endgroup\$null– null2014年06月01日 14:33:23 +00:00Commented Jun 1, 2014 at 14:33
Coffescript
_=->_=1
compiles to:
var _;
_ = function() {
return _ = 1;
};
ESMin (noncompetitive)
ɟ`html"Ĭ)
Translates to $('html').remove()
in Javascript.
-
\$\begingroup\$ What if my HTML page has no
<html>
element? \$\endgroup\$NoOneIsHere– NoOneIsHere2016年06月07日 20:18:57 +00:00Commented Jun 7, 2016 at 20:18 -
1\$\begingroup\$ ESMin runs in the online interpreter, so that shouldn't be a problem \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年06月08日 16:35:55 +00:00Commented Jun 8, 2016 at 16:35
R, 7 Bytes
`=`=`$`
The code makes the "=" (used for assigning) become the "$" (used for subseting). After that it gives error.
-
\$\begingroup\$ 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? \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年07月09日 23:41:02 +00:00Commented Jul 9, 2017 at 23:41
-
1\$\begingroup\$ @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日) \$\endgroup\$Taylor Raine– Taylor Raine2018年03月13日 05:34:47 +00:00Commented Mar 13, 2018 at 5:34
Bash: 4 chars
rm a
Put this in a file named a and run it on your Linux machine.
-
13\$\begingroup\$
rm *
will work for every filename \$\endgroup\$Antonio Ragagnin– Antonio Ragagnin2014年05月28日 12:24:51 +00:00Commented May 28, 2014 at 12:24 -
3\$\begingroup\$ @AntonioRagagnin It's better to stick to non-destructive scripts \$\endgroup\$Daniel– Daniel2014年05月28日 12:52:01 +00:00Commented May 28, 2014 at 12:52
-
1\$\begingroup\$ rm 0ドル will also work for all filenames, at the cost of one character. \$\endgroup\$elaforma– elaforma2014年05月31日 19:05:50 +00:00Commented May 31, 2014 at 19:05
-
7\$\begingroup\$ @AntonioRagagnin So will
rm -rf / --no-preserve-root
, but better ;) \$\endgroup\$Cole Tobin– Cole Tobin2014年05月31日 21:49:15 +00:00Commented May 31, 2014 at 21:49 -
6\$\begingroup\$ @fNek No, it won't. Only for those without a whitespace in their name. \$\endgroup\$glglgl– glglgl2014年05月31日 21:57:17 +00:00Commented May 31, 2014 at 21:57
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.
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.
-
\$\begingroup\$ Who said "too late" means "non-competitive"? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年04月23日 07:54:17 +00:00Commented Apr 23, 2016 at 7:54
-
\$\begingroup\$ @ΈρικΚωνσταντόπουλος Mama Fun Roll. But you did make me take a look at what I had written, and I found a typo. Thanks! \$\endgroup\$Adám– Adám2016年04月25日 17:27:31 +00:00Commented Apr 25, 2016 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.
Links: killall5 manpage, recording
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 POKE
s:
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
throughPOKE 1,9
do the same asPOKE 1,4
, but with varying levels of destruction.
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.
-
1\$\begingroup\$ Invalid, I'm afraid. From the challenge: "Any answers just initiating a reboot or halt will be disqualified." \$\endgroup\$steenbergh– steenbergh2017年11月15日 16:22:54 +00:00Commented Nov 15, 2017 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
-
\$\begingroup\$ Woah, I never thought I'd see this language here. \$\endgroup\$12Me21– 12Me212018年03月13日 19:45:40 +00:00Commented Mar 13, 2018 at 19:45
-
\$\begingroup\$ @12Me21 Never say that around PPCG. Ever. We use everything ;) \$\endgroup\$moonheart08– moonheart082018年03月16日 06:53:18 +00:00Commented Mar 16, 2018 at 6:53
-
\$\begingroup\$ 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. \$\endgroup\$12Me21– 12Me212018年03月16日 15:21:50 +00:00Commented Mar 16, 2018 at 15:21 -
\$\begingroup\$ @12Me21 I can confirm he hasn't started over. He's just busy. Also, join #powder on freenode. \$\endgroup\$moonheart08– moonheart082018年03月16日 20:01:12 +00:00Commented Mar 16, 2018 at 20:01
-
\$\begingroup\$ I believe the instruction pointer actually points to the next memory cell, not the current one. \$\endgroup\$12Me21– 12Me212018年03月16日 20:17:51 +00:00Commented Mar 16, 2018 at 20:17
Wolfram Language (Mathematica): 37 Characters
Export[#,""]&/@FileNames[All,"C:/",∞]
Never tested this fully, but it should overwrite every single file on a Windows computer, including the Mathematica installation and the file the program is saved in by replacing all data with an empty string. Actually useful for quickly clearing out a folder, but most definitely not something I want to run.
Haskell - 80 bytes
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
-
\$\begingroup\$ It's now a popularity contest. \$\endgroup\$Claudia– Claudia2014年05月30日 05:41:08 +00:00Commented May 30, 2014 at 5:41
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(){
alert(0);
window.funny = '';
};
funny();
funny();
minified
a=function(){alert(0);window.a=""};
Alert isn't really needed
a=function(){window.a=""};
-
\$\begingroup\$ Just do
function a(){a=0}
\$\endgroup\$minmaxavg– minmaxavg2016年03月30日 22:05:23 +00:00Commented Mar 30, 2016 at 22:05
Python 2.7
import inspect
import os
for x in range(0, 2):
os.remove(inspect.stack()[0][1])
print "am I dead yet?"
It only runs 1/2 of a time.
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.
-
1\$\begingroup\$ 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 \$\endgroup\$user16402– user164022014年06月09日 19:26:56 +00:00Commented Jun 9, 2014 at 19:26 -
3\$\begingroup\$ 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. \$\endgroup\$Stéphane Gourichon– Stéphane Gourichon2016年01月09日 06:31:10 +00:00Commented Jan 9, 2016 at 6:31
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).
-
\$\begingroup\$ 54 bytes \$\endgroup\$ceilingcat– ceilingcat2019年04月01日 04:08:57 +00:00Commented Apr 1, 2019 at 4:08
-
\$\begingroup\$ why not just
unlink(*v)
orremove(*v)
? \$\endgroup\$landfill baby– landfill baby2023年08月06日 09:00:50 +00:00Commented Aug 6, 2023 at 9:00
oldschool DOS
You don't even need a compiler or interpreter, just the prompt. Not even reboot will let it run again once it completes (note: do not try on something you don't want to sacrifice. OS likely won't boot either.)
deltree C:\
Windows batch, 4 bytes
.>%0
This will attempt to execute '.' as a command, while redirecting stdout to the original file, resulting in destruction of the original file.
Scratch, 49 bytes
when gf clicked
wait until<(n)<(1
change[n v]by(2
Assuming n=0
, which is the initial state of a variable when it is created.
Python 24
Name the file 'q'
import os
os.remove("q")
-
5\$\begingroup\$ try
os.remove(__file__)
instead. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年04月23日 07:50:26 +00:00Commented Apr 23, 2016 at 7:50
vi
in a single terminal environment has this effect, no escape from it unless you reboot :) Just a joke here. \$\endgroup\$echo "If you try to execute me again, it means you are an idiot.";
<-- Nobody will execute more than once :P \$\endgroup\$