1 of 38

Shell Scripting

Lecture 2

laksith, trinityc

2 of 38

Course Resources

  • Your facilitators!
  • Ed, Gradescope
  • OCF Slack (ocf.io/slack) or Discord (ocf.io/discord) #decal-general
  • All materials available at decal.ocf.io
  • Ask questions / work on lab with us during live sessions!

3 of 38

Engaging with this lecture

  • Connect to the shell (follow along!)
    • ssh $OCF_USERNAME@ssh.ocf.berkeley.edu
  • Ask questions!
    • During live sessions
    • On #decal-general

4 of 38

Topics

What’s on the menu?

  1. Bash
  2. Variables
  3. Conditionals
  4. Loops
  5. Functions
  6. Streams

5 of 38

But WHY should I learn to script?

  • You’re a sysadmin
  • You have to run some commands all the time
  • But you want to be lazy DRY (Don’t Repeat Yourself)
  • Describe your task as a step-by-step set of instructions so that a computer can do it for you!

6 of 38

7 of 38

Topics

What’s on the menu?

  • Bash
  • Variables
  • Conditionals
  • Loops
  • Functions
  • Streams

8 of 38

Bash

A shell...

And also a programming language!

9 of 38

Comments

Use a pound/sharp/hashtag

# This is a comment

10 of 38

shell variables

  • Whitespace matters!
  • Variable interpolation with $
  • Display text with echo

NAME=”value”

echo$NAME

11 of 38

shell variables

  • Types? What types?
  • Bash variables are untyped
  • Operations are contextual

FOO=1

$FOO + 1

12 of 38

shell variables

  • Types? What types?
  • Bash variables are untyped
  • Operations are contextual

FOO=1

$FOO + 1

error!

13 of 38

shell variables

  • Use the expr command to evaluate expressions

FOO=1

expr $FOO + 1

2

14 of 38

User input

  • Use the read command get user input
  • “-p” is for the optional prompt

read -p “send: ” FOO

# type “hi” and enter

echo “sent: $FOO

sent: hi

15 of 38

subshell

  • Command substitution allows you to use another command’s output to replace the text of the command

FOO=$(expr 1 + 1)

echo $FOO

2

16 of 38

Topics

What’s on the menu?

  • Bash
  • Variables
  • Conditionals
  • Loops
  • Functions
  • Streams

17 of 38

test

  • Evaluates an expression
  • Also synonymous with []
  • Sets exit status to
    • 0 (true)
    • 1 (false)

(Yup you read that right)

18 of 38

test

-eq ==

-ne !=

-gt >

-ge >=

-lt <

-le <=

19 of 38

test

test zero = zero; echo $?

0 # 0 means true

test zero = one; echo $?

1 # 1 means false

20 of 38

if

What if...?

if [ $1 -eq 69 ];

then

echo “nice”

fi

21 of 38

if-else

...And what ifn’t

if [ $1 -eq 69 ];

then

echo “nice”

else

echo “darn”

fi

22 of 38

elif

...And what ifn’t but if

if [ $1 -eq 69 ];

then

echo “nice”

elif [ $1 -eq 42 ];

then

echo “the answer!”

else

echo “wat r numbers”

fi

23 of 38

case

No one likes long if statements…

read -p "are you 21?" ANSWER

case "$ANSWER" in

“yes”)

echo "i give u cookie";;

“no”)

echo "thats illegal";;

“are you?”)

echo “lets not”;;

*)

echo "please answer"

esac

24 of 38

Topics

What’s on the menu?

  • Bash
  • Variables
  • Conditionals
  • Loops
  • Functions
  • Streams

25 of 38

for loops

for all your stuff in stuffs

SHEEP=("one" "dos" "tre")

for S in $SHEEP

do

echo "$S sheep..."

done

26 of 38

while loops

the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never the end is never

while true

do

echo "the end is never "

done

27 of 38

Time to Exercise!

Let’s write a script that renames files in our current directory

> ls

a.txt b.txt c.txt

> ./mycoolscript.sh

> ls

new_a.txt new_b.txt new_c.txt

28 of 38

Time to Exercise!

Let’s write a script that renames files in our current directory

#! /bin/sh

FILES=$(ls *.txt)

for FILE in $FILES

do

mv $FILE new_$FILE

done

29 of 38

Topics

What’s on the menu?

  • Bash
  • Variables
  • Conditionals
  • Loops
  • Functions
  • Streams

30 of 38

functions

fun

function greet() {

echo "hey there $1"

}

greet “Richard”

hey there Richard

31 of 38

Topics

What’s on the menu?

  • Bash
  • Variables
  • Conditionals
  • Loops
  • Functions
  • Streams

32 of 38

Redirection

echo "hello" > out.txt

Use > to output to somewhere else,�like a text file!

33 of 38

Redirection

sort < file

Use < to take input�from a file!

34 of 38

Pipes

command1 | command2

Take output of first command and “pipe” it into the second one, connecting stdin and stdout

35 of 38

Additional Notes

  • Python
    • argparse: easy CLI
    • fabric: easy deployment
    • salt: generally useful for infrastructure-related tasks
    • psutil: monitor system info
  • Use bash when the functionality you want is easily expressed as a composition of command line tools
    • Common file manipulation operations
  • Use Python when you need “heavy lifting” with complex control structures, messy state, recursion, OOP, etc.

36 of 38

Other Resources

37 of 38

OCF logo assets

Copy-paste these!!!

DO NOT MODIFY

38 of 38

Individually cropped sticker assets

Copy-paste these!!!

DO NOT MODIFY