Everyone says they’re coding in Rust these days, but they’re not. Every system has some bash-flavoured blu-tack hiding in the corners. But how well do you know this arcane, abstruse, annoying programming language? In 5 minutes, I’ll educate, entertain, and inform you about a core part of the bash language - the simple conditional statement. This irreverent and funny talk, while yes, insulting bash quite a lot, will hopefully provide a whole bunch of “oh, that’s why it does that” moments too.
1. Bash is Testing @mt165pro
Testing in Bash
Matt Turner
@mt165pro
mt165.co.uk
DevOps Days London
September 2018
aka Bash is Testing
2. Bash is Testing @mt165pro
# A command has has a stdout, a stderr, and an exit code
2
3. Bash is Testing @mt165pro
# All commands have a return value
# No matter what they write to stdout or stderr
$ ls
foo bar
$ echo $?
0
3
4. Bash is Testing @mt165pro
# Usually commands succeed
# Even when they don’t, you often don’t care
$ grep “needle” haystack
grep: haystack: No such file or directory
$ echo $?
2
4
5. Bash is Testing @mt165pro
# How do you test success / failure in a script?
$ if true; then echo “yes”; else echo “no”; fi
yes
5
6. Bash is Testing @mt165pro
# if is a piece of bash syntax
# it “takes precisely one argument: a command that returns 0 or non-0”
# … a command that returns 0 or non-0
6
7. Bash is Testing @mt165pro
$ which true
/bin/true
# true: “does nothing, successfully” [man(1) true, 1991]
$ file /bin/true
/bin/true: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV),
dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for
GNU/Linux 3.2.0, BuildID[sha1]=cae1cea2c8b5f2d151aceb44bcbe1d8415bc06c5,
stripped
7
8. Bash is Testing @mt165pro
$ if which flibble; then echo “yes”; echo echo “no” fi
No
# which is a command. A unary one
$ which which
/usr/bin/which
8
9. Bash is Testing @mt165pro
# These commands can be combined with boolean operators
# which are part of the bash syntax
# they are: ! && ||
$ if which foo || which bar; then echo “yes”; fi
9
10. Bash is Testing @mt165pro
# you may have seen script like this:
$ if [ “a” == “b” ]; then echo “hello”; fi
10
12. Bash is Testing @mt165pro
$ if [ -x foo ]; then echo “yes”; fi
# /usr/bin/[ is the command
# its arguments are: -x, foo, … and ]
13
13. Bash is Testing @mt165pro
# what does this print?
$ if [ a && b ]; then echo “yes”; else echo “no”; fi
no
16
14. Bash is Testing @mt165pro
# what does this print?
$ if true -a ls; then echo “yes”; else echo “no”; fi
yes
17
15. Bash is Testing @mt165pro
# what about this one?
$ if [ false ]; then echo “yes”; else echo “no”; fi
yes
18
16. Bash is Testing @mt165pro
# what about this one?
$ if [ false ]; then echo “yes”; else echo “no”; fi
yes
19
17. Bash is Testing @mt165pro
# syntaces can be combined
$ if [ -n “$foo” -a -f bar ] && which baz; then echo “yes”; fi
# but remember that code’s primary purpose is to be read...
20
18. Bash is Testing @mt165pro
# turns out, && and || are short-cct
$ if true || rm -rf /; then echo “you’re fine”; fi
# no if statement required
$ grep “needle” haystack && echo “found it”
# commonly used with /bin/[
$ [ -x foo ] && ./foo
# || even binds less tightly
$ [ -x foo ] && ./foo || echo “foo not executable”
# yay BODMAS
21
19. Bash is Testing @mt165pro
# turns out, && and || are short-cct
$ if true || rm -rf /; then echo “you’re fine”; fi
# no if statement required
$ grep “needle” haystack && echo “found it”
# commonly used with /bin/[
$ [ -x foo ] && ./foo
# || even binds less tightly
$ [ -x foo ] && ./foo || echo “foo not executable”
# yay BODMAS
22