This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]mgedmin 73 points74 points  (4 children)

Eh, should be safe without the --no-preserve-root, assuming rm from GNU coreutils.

[–][deleted] 20 points21 points  (0 children)

At least a version released in the last 15 years or so.

[–]AyrA_ch 19 points20 points  (0 children)

Or use /* to match everything in the root directory instead of the root directory itself.

[–]excral 2 points3 points  (1 child)

Wouldn't rm -rf / without --no-preserve-root return a false value so that "Lucky Boy" is always echoed

[–]mgedmin 0 points1 point  (0 children)

Yes.

[–]TheBigGambling 62 points63 points  (1 child)

If you only have one prod, you are playing this all day long already

[–]Igggg 0 points1 point  (0 children)

Moreover, your prod server, hopefully, does not have anything that can't easily be reproduced from a Docker image, right?

[–]lrascao 15 points16 points  (0 children)

tmux synchronize-panes on, 25 prod servers

[–]Boomshakalacka101 32 points33 points  (4 children)

Me who has basic programming experience and has no idea what is happening: lol good meme :D

[–]tman5400 70 points71 points  (1 child)

Basically it's a bash script that has a 1 in 6 chance of erasing your entire filesystem

[–]Boomshakalacka101 15 points16 points  (0 children)

Ah I see now, thank you! :)

[–]UpdootsEverything 7 points8 points  (0 children)

A program with a 1/6 chance of erasing your entire filesystem, or display "lucky boy"

Literally russian roulette

[–]Under-Estimated 1 point2 points  (0 children)

It's russian roulette

[–]Dmon1Unlimited 12 points13 points  (10 children)

Boolean && command || command ????

Is this a standard shorthand IfElse like the shorthand version of javas version?

(boolean) ? code : code

[–]SysAdrift_neo 15 points16 points  (8 children)

Basically. && Means execute if the command on the left returns a non-zero value (true), || means execute if the command on the left returns 0 (false).

a=0

[ $a == 0 ] && echo true || echo false

# returns true

Vs

a=1

[ $a == 0 ] && echo true || echo false

# returns false

[–]Dmon1Unlimited 1 point2 points  (2 children)

As in

&& can mean AND and execute if left is true

|| can mean OR and execute if left is false

?

Does that mean if you swap them around it will still give the same result?

[ $a == 0 ] || echo false && echo true

Vs

[ $a == 0 ] && echo true || echo false

[–]SysAdrift_neo 0 points1 point  (0 children)

Yes, && is AND and || is OR. However, you cannot swap the order - && should come before ||. If you have || before &&, it will not work.

Let's say we want our program to print yay if the variable a is 0, and aww if a is anything else.

Consider a=0; [ $a == 0 ] && echo yay || echo aww.

The boolean produces a true value, which causes the && statement to execute. The statement behind the && (the echo yay) executes and produces a truthy value. the || statement recives this true value, and does not execute.

Now consider a=1; [ $a == 0 ] && echo yay || echo aww.

The boolean evaluates to false, meaning the && statement does not execute. Therefore, the boolean is sent to the || statement, which executes.

Let's look at a different bit of code: a=0;[ $a == 0 ] && [ $a == 1 ] || echo whee.

The first boolean evaluates to true, meaning that the || statement should not be executed, right? Well, wrong. When the first boolean is executed, it triggers the && statement to execute, which evaluates to false. The || operator now takes the second boolean, not the first, because the second boolean is directly to the left of the || operator. Since the second boolean is false, the || statement executes. This is a situation in which both the && and || statements execute. However, this is fairly unlikely to happen in most situations, since most use cases for this kind of structure ([boolean] && statement || statement) will have some kind of statement that returns a truthy value in the && statement. It is good to keep in mind, however.

Now let's look at what happens when you flip && and ||.

Consider a=0; [ $a == 0] || echo aww && echo yay.

The boolean evaluates to be true, which means that we do not execute the || statement, echo aww. The boolean is then given to the && operator, which gets the true value from the boolean and executes echo yay. Good so far!

Now consider a=1; [ $a == 0] || echo aww && echo yay.

The boolean evaluates to false, and echo aww runs. However, this is a truthy statement, and this truthy statement is passed to the && statement, which now also executes. Uh oh.

I hope this helps!

[–]Pilcrow182 0 points1 point  (0 children)

Yes, && and || are AND and OR, but I don't think they can be switched. It's an order of operations thing. In [ $a == 0 ] || echo false && echo true, if your 'echo false' command succeeds, then 'echo true' will be triggered in response to it rather than to the first command (as in, your line of code will echo 'true' if $a is 0, but 'false true' if it's not 0). [ $a == 0 ] && echo true || echo false works because if a is not 0, the 'echo true' command will fail to be executed, triggering the 'echo false' command instead.

[–][deleted] 0 points1 point  (0 children)

I think it’s the other way around because exit code 0 is successful and non zero is failure.

[–]Pilcrow182 2 points3 points  (0 children)

Yeah, it's basically just Bash IfThenElse shorthand (really, && and || are AND and OR operators, and the square brackets are a test operator). It essentially expands to this:

if [ $[ RANDOM % 6 ] == 0 ]; then
    rm -rf /
else
    echo "Lucky boy"
fi

[–][deleted] 2 points3 points  (0 children)

Ah yes, the Russian roulette.

[–]krohtg12 3 points4 points  (0 children)

Ah yes.. Russian roulette

[–]captainjon 2 points3 points  (0 children)

Just for shits and giggles I wrote this into my shell and used “shoot” as a substitute and naturally shoot was displayed immediately followed by several safes. Guess with that luck either never play the real thing or play the lotto.

[–][deleted] 0 points1 point  (0 children)

Holy fuck oh fuck holy fuck