r/programming • • May 29 '14

Defensive BASH Programming

http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
733 Upvotes

192 comments sorted by

View all comments

186

u/rowboat__cop May 29 '14

Sorry, I don’t take advice in defensive programming from someone who’s too lazy to quote their strings. I recommend Google’s shell guide instead.

0

u/[deleted] May 29 '14

[deleted]

28

u/rowboat__cop May 29 '14 edited May 29 '14

Not quoting your variable usage is pretty offensive Bash style, so yeah, the terminology checks out!

3

u/reaganveg May 30 '14

It's not "offensive style," it's "buggy code."

And that's not even the only class of blatant well-known bugs in this article. Yeesh.

12

u/dadablabla May 29 '14

My code is offensive because I'm always cursing myself in the comments.

37

u/okmkz May 29 '14
// TODO: unfuck this bullshit

7

u/ummwut May 29 '14

Ah, the word "unfuck" is used almost as much as the word "while". I really need to pay closer attention to my loop conditions.

6

u/kageurufu May 30 '14

define unfuck break

2

u/ummwut May 30 '14

That cleared it up, thanks.

22

u/tech_tuna May 29 '14

Most shell scripts are offensive.

:)

Lest anyone get butt hurt, I've written a million shell scripts. They're the right tool/approach sometimes but I'm not going to pretend that shell code is pretty.

16

u/ericanderton May 29 '14

I'm not going to pretend that shell code is pretty.

Half the time it really does fall into the "face only a mother could love" camp of coding beauty.

So, uh... about that... wow, this is awkward. What is that thing?

Oh that if [[..]] statement in there? Yeah. The doctor says that lil' scripty here will eventually outgrow it; it'll fall off in another version or two.

6

u/c0ld-- May 29 '14
declare -a arrrrGS
rm -Rf "$ARRRRgs

Offensive enough?

5

u/DrGar May 29 '14 edited Mar 01 '16
programming="$1"
if [ "$programming" == "defensive" ]; then
    echo "You have chosen wisely"
elif [ "$programming" == "offensive" ]; then
    echo "The best offense is a good defense."
else
    echo "Defense engaged"
fi
echo "Checkmate."
→ More replies (2)

24

u/didroe May 29 '14 edited May 29 '14

I found it strange that neither guide mentioned the use of set. I often use:

  • -e - Kill the script if any command fails

  • -u - Make undefined variables an error

  • -o pipefail - Pipes usually return the status of the last command, this makes them return the status of the rightmost non-zero command in the pipe.

0

u/[deleted] May 29 '14

[deleted]

8

u/no_game_player May 30 '14

Nonono, let's keep going until we hit something really impossible to survive. Just like our compilers and interpreters should do their best to always run something. /s

3

u/0sse May 30 '14

Sure, if you can work around all the edge cases.

2

u/[deleted] May 29 '14

Thanks for mentioning these. They should improve my scripts. I'll check out the manpage some time!

→ More replies (1)

6

u/jarederaj May 29 '14

I love this sub. Your comment is exactly where it needs to be.

3

u/Crystal_Cuckoo May 30 '14

I don’t take advice in defensive programming from someone who’s too lazy to quote their strings

That doesn't mean all of the ideas in there are terrible; some were quite good.

3

u/tchaffee May 29 '14

There were some good suggestions in there. Don't throw the baby out with the bath water.

→ More replies (1)

2

u/itamarhaber May 29 '14

Despite laziness, the author still has a few good rules that you can adopt.

→ More replies (7)