Bash:
The #1 Skill
for DevOps
Engineers
anders.janmyr@ingka.ikea.com
Bash:
The #1 Skill
for DevOps
Engineers
Ugly
and Quirky
But
Powerful and
Everywhere
Bash: Ugly...
Propositions
It's a script.
It's not a server.
Make it work!
Clean it up!
Move on!
Byte-sized :)
Google
Copy-Paste
Learn!
Make it work locally!
Then on CI server.
BAD!
Don't code in YAML!
GOOD!
Code in script.
first.sh
first.sh
Output
first.sh
Takeaways
# First command that reads
STDIN will wait for input.
# Every script has
STDIN, STDOUT, STDERR
Exit status
# Exit status can be used
in conditionals
# STDOUT can be captured
args.sh
args.sh
simple.sh
Takeaways
set -o errexit
# Exits if a command fails
"$1", "$2",...
"$@"
# Always quote args
# Every line executes a
command
'$1'
# No var expansion
Here docs
<<-
Here docs
Takeaways
"$a_var", "$(ls)"
# variable expansion
# cmd execution
$(cat <<-whatever
"Quote": "without escaping"
set -o pipefail
# Exits if a command fails
in pipe
# Great for generating
JSON
Caching
Cache
Takeaways
# Arithmetic
local sum=$(($a + 5))
if (($age > $max_age)); then
# Conditionals
if [[ -f $file ]]; then
// elif/else/fi
# -d (dir)
# Pipes
trace=$(cat $file | grep Akamai)
Functions
Functions
Takeaways
# Call
ret=my_function abc
out=$(my_function abc)
# Vars
"${file?is required}"
"${1?file is required}"
"${1:-default}"
my_function() {
local v="$1"
echo "out $v"
}
tag.sh
tag.sh
Takeaways
# Print line
before executing
[[ "$TRACE" ]] && set -x
# Trim all 'v' from left
new_version=${1##v}
# '%' from right
# Echo to stderr
echoerr() { echo "$@" 1>&2; }
# Command with if
if ! grep $version ./r.md; then
# AWK
awk "/^#.*$new_version/{flag=1;next}/^#/{flag=0}flag" $file
tag.sh
cont.
tag.sh
Takeaways
# Git multiple -m
git commit -am "Release $new_version" -m "$description"
# Git return values
if ! git diff --quiet HEAD; then
# sed
sed -i.bak -E "s/[0-9]+.[0-9]+.[0-9]+/$new_version/g" $file
Trap
Signals
Trap
Signals
cleanup() {
rm -rf $tmp_template
}
trap cleanup EXIT
# Signals
EXIT - Program Exit
SIGINT - Ctrl-C from the keyboard
SIGQUIT - Ctrl- from the keyboard
Useful
Commands
curl, jq,
tr, awk,
...
Watching files
Periodic
execution
When should
I not use
Bash?
Large Programs
> 100 Lines
Complex structures
Maps/Dicts/Structs
Book
Recommendation
Bash:
Questions
anders.janmyr@ingka.ikea.com

Bash: the #1 skill for Devops Engineers