TDC Floripa 2016 - Unlocking the power of the Unix Shell

TDC Floripa 2016 - Unlocking the power of the Unix Shell

4569aec00cb223b3fbf484f9e7ba1256?s=128

Renan Ranelli

May 14, 2016
Tweet

Transcript

  1. UNLOCKING THE POWER OF THE UNIX SHELL Renan Ranelli

  2. Milhouse (@renanranelli)

  3. Software Engineer @ Milhouse (@renanranelli)

  4. (former) Software Engineer @ Milhouse (@renanranelli)

  5. Milhouse (@renanranelli)

  6. 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.
  7. WHY SHOULD I UNDERSTAND SHELL SCRIPTING?

  8. 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.
  9. WHAT CAN BE DONE WITH SHELL?

  10. 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`.
  11. 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/
  12. This was written in March 2005

  13. THE MAIN ELEMENTS OF BASH

  14. 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.
  15. THE MAIN ELEMENTS OF BASH • Bash is untyped. (like

    assembly, unlike C) • Every value is a string. • Strings are interpreted as commands.
  16. 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
  17. THE MAIN ELEMENTS OF BASH

  18. 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.
  19. THE MAIN ELEMENTS OF BASH • Variable Expansion: – Is

    exactly what you expect (like in Ruby):
  20. THE MAIN ELEMENTS OF BASH

  21. MEÇA SUAS QUOTES PARÇA

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

  23. THE “EXECUTABLE” IS NOT SPECIAL

  24. THE MAIN ELEMENTS OF BASH: IO

  25. THE MAIN ELEMENTS OF BASH: IO

  26. IO REDIRECTION

  27. IO REDIRECTION

  28. STRING & PROCESS SUBSTITUTION

  29. THE MAGIC OF $( … )

  30. THE MAGIC OF <( … )

  31. 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.
  32. 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.
  33. BUILDING OUR OWN SHELL (THAT'S WHERE THE FUN STARTS)

  34. READING SOME (COMPLICATED) BASH PROGRAMS

  35. OBRIGADO !

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