SlideShare a Scribd company logo
Bash is Testing @mt165pro
Testing in Bash
Matt Turner
@mt165pro
mt165.co.uk
DevOps Days London
September 2018
aka Bash is Testing
Bash is Testing @mt165pro
# A command has has a stdout, a stderr, and an exit code
2
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
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
Bash is Testing @mt165pro
# How do you test success / failure in a script?
$ if true; then echo “yes”; else echo “no”; fi
yes
5
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
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
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
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
Bash is Testing @mt165pro
# you may have seen script like this:
$ if [ “a” == “b” ]; then echo “hello”; fi
10
Bash is Testing @mt165pro
$ which [
/usr/bin/[
11
Bash is Testing @mt165pro
$ if [ -x foo ]; then echo “yes”; fi
# /usr/bin/[ is the command
# its arguments are: -x, foo, … and ]
13
Bash is Testing @mt165pro
# what does this print?
$ if [ a && b ]; then echo “yes”; else echo “no”; fi
no
16
Bash is Testing @mt165pro
# what does this print?
$ if true -a ls; then echo “yes”; else echo “no”; fi
yes
17
Bash is Testing @mt165pro
# what about this one?
$ if [ false ]; then echo “yes”; else echo “no”; fi
yes
18
Bash is Testing @mt165pro
# what about this one?
$ if [ false ]; then echo “yes”; else echo “no”; fi
yes
19
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
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
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
Bash is Testing @mt165pro
Bash is Testing
26

More Related Content

PPTX
Qt Translations
ODP
Maybe you do not know that ...
PDF
Iteration
PDF
ATS language overview
PPTX
C-spirit reborn: why Go was bound to be created
PDF
Python Basic
PPS
An approach to Programming Contests with C++
PPTX
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
Qt Translations
Maybe you do not know that ...
Iteration
ATS language overview
C-spirit reborn: why Go was bound to be created
Python Basic
An approach to Programming Contests with C++
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...

Similar to Bash is Testing (20)

PPTX
shellScriptAlt.pptx
PPTX
AOS_Module_3ssssssssssssssssssssssssssss.pptx
PPTX
Chapter 2: Introduction to Bash Scripting
PPTX
Bash and regular expressions
ODP
Bash and regular expressions
PPT
ShellAdvanced shell scripting programm.ppt
PPT
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
PPTX
Linux Shell Scripting
PDF
Bash production guide
PPT
ShellProgramming and Script in operating system
PPT
Bash Programming
PPTX
KT on Bash Script.pptx
PPT
2-introduction_to_shell_scripting
PDF
Module 03 Programming on Linux
PPTX
Linux Shell Scripting.pptx
PPTX
Bash Shell Scripting
PPT
Unix shell scripting basics
PPT
Unix Shell Scripting Basics
DOCX
What is a shell script
PDF
Operating_System_Lab_ClassOperating_System_2.pdf
shellScriptAlt.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
Chapter 2: Introduction to Bash Scripting
Bash and regular expressions
Bash and regular expressions
ShellAdvanced shell scripting programm.ppt
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
Linux Shell Scripting
Bash production guide
ShellProgramming and Script in operating system
Bash Programming
KT on Bash Script.pptx
2-introduction_to_shell_scripting
Module 03 Programming on Linux
Linux Shell Scripting.pptx
Bash Shell Scripting
Unix shell scripting basics
Unix Shell Scripting Basics
What is a shell script
Operating_System_Lab_ClassOperating_System_2.pdf
Ad

More from Matt Turner (20)

PDF
The Life of a Packet through Istio III
PDF
Automated Cloud-Native Incident Response with Kubernetes and Service Mesh
PDF
apiserver-Only "Clusters" for fun and profit
PDF
Istio + SPIRE for cross-domain traffic trust in hybrid-cloud scenarios
PDF
Why Is Istio That Shape?
PDF
Dynamically Testing Individual Microservice Releases In Production
PDF
Gateway APIs, Envoy Gateway, and API Gateways
PDF
The Life of a Packet III - Service Mesh London
PDF
Cloud-Native Progressive Delivery
PDF
An Introduction to Bazel
PDF
Networks, Linux, Containers, Pods
PDF
What is a Service Mesh and what can it do for your Microservices
PDF
Debugging an RBAC Problem in Istio
PDF
Running Resillient Workloads with Istio - KubeCon China 2019
PDF
Software Networking and Interfaces on Linux
PDF
Running Resillient Workloads with Istio - OpenInfra Days 2019
PDF
The Life of a Packet through Istio - DevExperience Romania, April 2019
PDF
The life of a packet through Istio - QCon London 2019
PDF
Do You Need a Service Mesh? @ London Devops, January 2019
PDF
Istio, The Packet's-Eye View - KubeCon NA 2018
The Life of a Packet through Istio III
Automated Cloud-Native Incident Response with Kubernetes and Service Mesh
apiserver-Only "Clusters" for fun and profit
Istio + SPIRE for cross-domain traffic trust in hybrid-cloud scenarios
Why Is Istio That Shape?
Dynamically Testing Individual Microservice Releases In Production
Gateway APIs, Envoy Gateway, and API Gateways
The Life of a Packet III - Service Mesh London
Cloud-Native Progressive Delivery
An Introduction to Bazel
Networks, Linux, Containers, Pods
What is a Service Mesh and what can it do for your Microservices
Debugging an RBAC Problem in Istio
Running Resillient Workloads with Istio - KubeCon China 2019
Software Networking and Interfaces on Linux
Running Resillient Workloads with Istio - OpenInfra Days 2019
The Life of a Packet through Istio - DevExperience Romania, April 2019
The life of a packet through Istio - QCon London 2019
Do You Need a Service Mesh? @ London Devops, January 2019
Istio, The Packet's-Eye View - KubeCon NA 2018
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPT
Teaching material agriculture food technology
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Teaching material agriculture food technology
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
sap open course for s4hana steps from ECC to s4
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”

Bash is Testing

  • 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
  • 11. Bash is Testing @mt165pro $ which [ /usr/bin/[ 11
  • 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
  • 20. Bash is Testing @mt165pro Bash is Testing 26