1 of 46

Slide Notes

DownloadGo Live

Copia de Shell Games

Slides for a talk on shell-scripting I gave to the awesome group Women in Linux on June 15, 2016.

I went over the basics of going from shell usage to encapsulating it into a script, then expanding it with variables to make a one-off script generally useful. Finally, I show how adding control structures and conditionals can add further sanity checking and usability to the scripts, because one day you will forget why you did that thing in that way.

Along the way we chatted about unix toolbox philosophy and how shell scripting extends that.

Makes the most sense if you are look at the corresponding Github files.

PRESENTATION OUTLINE

Shell Games

Shell Scripting for Fun and Profit

Why SHELL Scripting?

The REaL Question: Why Automate?
Photo by transmediale

Why Automate?

  • reduce errors.
  • Save Time.
  • Eliminate 'Toil'.
  • ...Fun.

Shell SCRIPTING VS the World

  • *NIX-native Way to perform Actions
  • Extends Naturally from shell commands
  • 'Just Enough' Programming

Limitations

  • BASH Syntax is Kinda Garbage
  • Higher-Order Data Structures Are Cumbersome
  • painful to debug/troubleshoot
Photo by JeepersMedia

OVERALL

  • quick and dirty way to leverage unix tool philosophy
  • Not a programming replacement

The Philosophy of Unix Tools

It's  All About the Pipe

small focused tools chained together for big effect
Photo by kenmainr

EXERCISE 0

Swiss Army Knives VS Toolboxes
Photo by herzogbr

Swiss Army Knives VS ToolbOxes

  • At Shell prompt: info coreutils
  • /toolbox
  • on 'Opening the Software Toolbox', hit enter.

Swiss Army Knives Vs Toolboxes, cont.

  • Scroll down to Toolbox introduction
  • Read until I/O Redirection
  • Done!
  • Let's Discuss

BAsh Scripting

Is a way to use the toolbox, not build Swiss Army Knives.
Photo by TOMTEC

Foundations

Basics of Shell Scripting

  • Variables (user, environment, Built-In)
  • operators
  • Control Structures (if/THEN, While, For)
  • functions

Dassit!

But that's All You need
Photo by Kit4na

EXERCISE 1

encapsulating the cli

Encapsulating the CLI (SETUP)

Encapsulating the CLI

Encapsulating the CLI

  • this script is 'static'
  • We have to change the script for each new user..
  • Or do we?

Variables

to the rescue
Photo by jronaldlee

Encapsulating the CLI

  • $0 , $1, $X ...
  • MYVARIABLE='', ${MYVARIABLE}

Encapsulating the CLI

  • Variables add flexibility to procedures
  • Makes one off commands repeatable and generally useful

Exercise 2

Altering the Flow

Altering the Flow

  • conditionals and control structures
  • if this, then That
  • while this happens... do this
  • for each X in Y...do this
  • In Case X...do this

Altering the Flow

  • the power to respond to different input differently.

Altering the Flow

  • We can prevent or enable actions based on conditions
  • We can check for problems...

#1 REason to script (IMO)

automate but also sanity check
Photo by rishibando

altering the Flow

  • $? - get exit status code of previous command
  • you can now make your own skeleton directory for home dir templates.
  • mkdir /etc/random_skel
  • We can take one or two parameters as needed.

Exercise 3

Adding more structure

Adding More Structure

  • needing to remember the order and types of the arguments...
  • Some reminders would help.

Adding More Structure

  • use $#
  • use while...do
  • use case
  • et voila!

adding more structure

  • This makes the script easier to come back to
  • you can ask it for help and it will tell you
  • seems like a bother, but worth the time.

Adding More Structure

  • Functions help you avoid repetition
  • also help organization

Wrapping UP

Click to add more text here

Bash is Suprisingly intricate

but be careful about going too deep
Photo by Amy Loves Yah

Keep Expanding Tricks

makes cli work more productive
Photo by Cyol Ternyan

Know when to Quit

Or really, upgrade to higher-level language
Photo by 36Bananapies

Have Fun

shell scripting can be a great party trick.
Photo by monkeyc.net

Thank YOU!

Questions?
Photo by Stefan Baudy