1 of 40

Shell Scripting

Ian McDonald

Sign In Link: decal.ocf.berkeley.edu/signin

Magic Word:

2 of 40

the motivation

  • You’re a sysadmin

3 of 40

the motivation

  • You’re a sysadmin
  • You have to run some commands all the time

4 of 40

the motivation

  • You’re a sysadmin
  • You have to run some commands all the time
  • But you you wanna stay DRY

5 of 40

the motivation

  • You’re a sysadmin
  • You have to run some commands all the time
  • But you you wanna stay DRY
  • Describe your task as a step by step set of instructions so that a computer can do the dirty work for you

6 of 40

with great power comes…

xkcd.com/974

7 of 40

Bash Is...

  • A shell...

8 of 40

Bash Is...

  • A shell…
  • A programming language!

9 of 40

shell variables and types

  • NAME=”value”
    • Whitespace matters!
  • echo “$NAME”
    • Variable interpolation (value of a variable vs. string literal)

10 of 40

shell variables and types

  • Types? What types?
  • Bash variables are untyped
  • Arithmetic and other operations are contextual
    • Does this text contain a digit?
  • FOO=1
  • $FOO + 1

11 of 40

shell variables and types

  • FOO=1
  • $FOO + 1
    • bash: 1: command not found

12 of 40

shell variables and types

  • FOO=1
  • $FOO + 1
    • bash: 1: command not found
  • expr $FOO + 1
    • 2

13 of 40

test

  • test evaluates an expression
    • synonymous with [ ]
  • Sets exit status to 0 (true) or 1 (false)
  • (Yes, 0 is true and 1 is false)

14 of 40

test - integer comparison

$ test 0 -eq 0; echo $? # exit code 0 means true0$ test 0 -eq 1; echo $? # exit code 1 means false1

15 of 40

test - string comparison

$ test zero = zero; echo $? # exit code 0 means true�0$ test zero = one; echo $? # exit code 1 means false1

16 of 40

test - integer comparison

$ [ 0 -eq 0 ]; echo $? # exit code 0 means true0$ [ 0 -eq 1 ]; echo $? # exit code 1 means false1

17 of 40

test - string comparison

$ [ zero = zero ]; echo $? # exit code 0 means true�0$ [ zero = one ]; echo $? # exit code 1 means false1

18 of 40

if-then

if TEST-COMMANDS; then�� CONSEQUENT-COMMANDS��elif MORE-TEST-COMMANDS; then�� MORE-CONSEQUENT-COMMANDS��else �� ALTERNATE-CONSEQUENT-COMMANDS;��fi

19 of 40

if-then

#!/bin/bash�# contents of awesome_shell_script��if [ “$1” -eq “$2” ]; then� echo “args are equal”�else� echo “args are not equal”�fi

20 of 40

if-then

$ ./awesome_shell_script 0 0�args are equal�$ ./awesome_shell_script 0 1�args are not equal

21 of 40

while

while TEST-COMMANDS; do�� CONSEQUENT-COMMANDS��done

22 of 40

while

#!/bin/bash�# contents of awesome_shell_script��n=”$1”�while [ “$n” -gt 0 ]; do� echo “$n”� let n=”$n-1”�done

23 of 40

while

$ ./awesome_shell_script 5�5�4�3�2�1

24 of 40

command substitution

$ A=`expr 1 + 1`

$ echo $A

2

25 of 40

functions

name_of_function() {�� FUNCTION_BODY��}

name_of_function $arg1 $arg2 ... $argN

26 of 40

functions

#!/bin/bash�# contents of awesome_shell_script��foo() {� echo hello “$1”�}��foo “$1”

27 of 40

functions

$ ./awesome_shell_script world�hello world

28 of 40

functions

fib() {� N=”$1”� if [ “$N” -eq 0 ]; then� echo 0� elif [ “$N” -eq 1 ]; then� echo 1� else� echo $(($(fib $(($N-2))) + $(fib $(($N-1)))))� fi�}��fib “$1”

29 of 40

functions

$ ./fibonacci 10�55

30 of 40

the case for python

  • That last slide looked painful
  • Complex control structures, functions, closures, etc. are a hassle in Bash
  • Solution: Use Python!

31 of 40

the case for python

  • argparse
    • Easy CLI
  • fabric
    • Easy deployment
  • salt
    • Generally useful for infrastructure-related tasks
  • Psutil
    • Monitor system info

32 of 40

the case for python

Use Bash when:

  • The functionality you want is easily expressed as a composition of command line tools
  • Common file manipulation operations

33 of 40

the case for python

Use Python when:

  • You need “heavy lifting” with complex control structures, messy state, recursion, OOP, etc.

34 of 40

shebang!

  • #!/path/to/something
  • #!/bin/sh
  • #!/bin/bash
  • #!/bin/sed
  • #!/usr/bin/python

35 of 40

shebang!

  • #!/path/to/interpreter
  • If you call your script as an executable, this line is what determines the program used to execute the lines below it

36 of 40

She bang! (Tangent)

  • Make sure your script is consistent with it’s shebang
  • If you use bash-specific features (bashisms) in a script with #!/bin/sh, you’re gonna have a bad time
  • checkbashisms can help

37 of 40

running your script

$ path/to/interpreter my-shell-script

  • $ bash my-shell-script
  • $ python my-shell-script

38 of 40

running your script

$ path/to/my-shell-script

  • $ ./my-shell-script
  • where #! matters
  • must be executable by you (or whoever else you want running your script)
  • chmod u+x my-shell-script

39 of 40

other resources

  • LDP (Linux Documentation Project)
    • Bash Guide for Beginners
    • Advanced Bash Scripting Guide
      • http://tldp.org/LDP/abs/html/
  • Scripting topic guide
    • Attached to the lab!

40 of 40

lab assignment

  • Lab has more detail than this lecture
  • Make a Unix phonebook!
  • decal.ocf.berkeley.edu/labs/b3