Slide 1

Slide 1 text

UNLOCKING THE POWER OF THE UNIX SHELL Renan Ranelli

Slide 2

Slide 2 text

Milhouse (@renanranelli)

Slide 3

Slide 3 text

Software Engineer @ Milhouse (@renanranelli)

Slide 4

Slide 4 text

(former) Software Engineer @ Milhouse (@renanranelli)

Slide 5

Slide 5 text

Milhouse (@renanranelli)

Slide 6

Slide 6 text

AGENDA • Why should we understand shell scripting. • A little bit of what I've done with shell. • The main elements of Bash. • Building a very limited shell with Ruby. • Breaking down some complex shell commands.

Slide 7

Slide 7 text

WHY SHOULD I UNDERSTAND SHELL SCRIPTING?

Slide 8

Slide 8 text

WHY SHOULD I UNDERSTAND THE SHELL? • You can always count on the shell to be there. • We use it every day. And it is great to “glue” things together • System administrators use it for too many things. You will someday need to understand something they done. • It's a gateway drug to many other interesting things about programming and operating systems. • Its fun.

Slide 9

Slide 9 text

WHAT CAN BE DONE WITH SHELL?

Slide 10

Slide 10 text

THINGS I'VE DONE WITH BASH • A Redis client. • A “thread pool”. • A script that interacts with github's api and `git clone`'s all repositories under a person or organization. • A Password manager. • Parallel deployment of our frontend @ Xerpa. • A “framework” for build CI jobs. • Monitoring plugins for `Sensu`.

Slide 11

Slide 11 text

AND I DOCUMENTED THE EXPERIENCE: • Redis client: – http://milhouseonsoftware.com/2015/07/27/writing-a-redis-client-in-pure-bash-part-1/ – http://milhouseonsoftware.com/2015/08/11/writing-a-redis-client-in-pure-bash-part-2/ • Repository cloner – http://milhouseonsoftware.com/2015/11/20/writing-a-process-pool-in-bash/ – http://milhouseonsoftware.com/2015/12/27/building-a-repository-cloner-with-bash/ • Password manager – http://milhouseonsoftware.com/2015/10/26/write-your-own-password-manager/

Slide 12

Slide 12 text

This was written in March 2005

Slide 13

Slide 13 text

THE MAIN ELEMENTS OF BASH

Slide 14

Slide 14 text

THE MAIN ELEMENTS OF BASH • Shell (interpreter) TTY, Terminal (device) ~ • Bash is untyped. (like assembly, unlike C) • Every value is a string. • Strings are interpreted as commands. • (the best way to really grok Bash is to read its man page. `man bash` is all you need. I'm serious) • `explainshell.com` is pretty good too.

Slide 15

Slide 15 text

THE MAIN ELEMENTS OF BASH • Bash is untyped. (like assembly, unlike C) • Every value is a string. • Strings are interpreted as commands.

Slide 16

Slide 16 text

THE MAIN ELEMENTS OF BASH • Programs are composed of commands. Everything in the Shell is a command. • This idea was described by Antirez (Salvatore Sanfilippo) when explaining the semantics of the TCL language: http://antirez.com/articoli/tclmisunderstood.html

Slide 17

Slide 17 text

THE MAIN ELEMENTS OF BASH

Slide 18

Slide 18 text

THE MAIN ELEMENTS OF BASH • A given command is subjected to “parameter expansion” and “substitution” prior to being “executed”. • Substitution is very close to what happens in Ruby when you pass “variables” as “arguments” to a method. Its value is substituted where we see the variable “name”. • Expansion is hard and tricky. I won't talk about it now.

Slide 19

Slide 19 text

THE MAIN ELEMENTS OF BASH • Variable Expansion: – Is exactly what you expect (like in Ruby):

Slide 20

Slide 20 text

THE MAIN ELEMENTS OF BASH

Slide 21

Slide 21 text

MEÇA SUAS QUOTES PARÇA

Slide 22

Slide 22 text

MEÇA SUAS QUOTES PARÇA NOT WHAT YOU WANT

Slide 23

Slide 23 text

THE “EXECUTABLE” IS NOT SPECIAL

Slide 24

Slide 24 text

THE MAIN ELEMENTS OF BASH: IO

Slide 25

Slide 25 text

THE MAIN ELEMENTS OF BASH: IO

Slide 26

Slide 26 text

IO REDIRECTION

Slide 27

Slide 27 text

IO REDIRECTION

Slide 28

Slide 28 text

STRING & PROCESS SUBSTITUTION

Slide 29

Slide 29 text

THE MAGIC OF $( … )

Slide 30

Slide 30 text

THE MAGIC OF <( … )

Slide 31

Slide 31 text

ENVIRONMENT VARIABLES • Every process in your OS has a group of “environment variables”. (You can inspect them reading the file `/proc/$pid/environ`). • When you “fork” a process, the child process “inherits” the values of its parent's environment variables.

Slide 32

Slide 32 text

I WILL STOP HERE. • There are other aspects to Bash more akin to what we do with “programming languages”, like loops, conditionals and things like that. • I won't talk about those because you already know enough to explore them by yourself. • When in doubt, check the `man page` or the Bash manual. `explainshell.com` is also your friend.

Slide 33

Slide 33 text

BUILDING OUR OWN SHELL (THAT'S WHERE THE FUN STARTS)

Slide 34

Slide 34 text

READING SOME (COMPLICATED) BASH PROGRAMS

Slide 35

Slide 35 text

OBRIGADO !

Slide 36

Slide 36 text

@renanranelli /rranelli Renan Ranelli (Milhouse) milhouseonsofware.com