Shell Scripting
Ian McDonald
Sign In Link: decal.ocf.berkeley.edu/signin
Magic Word:
the motivation
● You’re a sysadmin
the motivation
● You’re a sysadmin
● You have to run some commands all the time
the motivation
● You’re a sysadmin
● You have to run some commands all the time
● But you you wanna stay DRY
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
with great power comes…
xkcd.com/974
Bash Is...
● A shell...
Bash Is...
● A shell…
● A programming language!
shell variables and types
● NAME=”value”
○ Whitespace matters!
● echo “$NAME”
○ Variable interpolation (value of a variable vs.
string literal)
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
shell variables and types
● FOO=1
● $FOO + 1
○ bash: 1: command not found
shell variables and types
● FOO=1
● $FOO + 1
○ bash: 1: command not found
● expr $FOO + 1
○ 2
test
● test evaluates an expression
○ synonymous with [ ]
● Sets exit status to 0 (true) or 1 (false)
● (Yes, 0 is true and 1 is false)
test - integer comparison
$ test 0 -eq 0; echo $? # exit code 0 means true
0
$ test 0 -eq 1; echo $? # exit code 1 means false
1
test - string comparison
$ test zero = zero; echo $? # exit code 0 means true
0
$ test zero = one; echo $? # exit code 1 means false
1
test - integer comparison
$ [ 0 -eq 0 ]; echo $? # exit code 0 means true
0
$ [ 0 -eq 1 ]; echo $? # exit code 1 means false
1
test - string comparison
$ [ zero = zero ]; echo $? # exit code 0 means true
0
$ [ zero = one ]; echo $? # exit code 1 means false
1
if-then
if TEST-COMMANDS; then
CONSEQUENT-COMMANDS
elif MORE-TEST-COMMANDS; then
MORE-CONSEQUENT-COMMANDS
else
ALTERNATE-CONSEQUENT-COMMANDS;
fi
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
if-then
$ ./awesome_shell_script 0 0
args are equal
$ ./awesome_shell_script 0 1
args are not equal
while
while TEST-COMMANDS; do
CONSEQUENT-COMMANDS
done
while
#!/bin/bash
# contents of awesome_shell_script
n=”$1”
while [ “$n” -gt 0 ]; do
echo “$n”
let n=”$n-1”
done
while
$ ./awesome_shell_script 5
5
4
3
2
1
command substitution
$ A=`expr 1 + 1`
$ echo $A
2
functions
name_of_function() {
FUNCTION_BODY
name_of_function $arg1 $arg2 ... $argN
functions
#!/bin/bash
# contents of awesome_shell_script
foo() {
echo hello “$1”
}
foo “$1”
functions
$ ./awesome_shell_script world
hello world
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”
functions
$ ./fibonacci 10
55
the case for python
● That last slide looked painful
● Complex control structures, functions, closures,
etc. are a hassle in Bash
● Solution: Use Python!
the case for python
● argparse
○ Easy CLI
● fabric
○ Easy deployment
● salt
○ Generally useful for infrastructure-related tasks
● Psutil
○ Monitor system info
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
the case for python
Use Python when:
● You need “heavy lifting” with complex control
structures, messy state, recursion, OOP, etc.
shebang!
● #!/path/to/something
● #!/bin/sh
● #!/bin/bash
● #!/bin/sed
● #!/usr/bin/python
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
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
running your script
$ path/to/interpreter my-shell-script
● $ bash my-shell-script
● $ python my-shell-script
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
other resources
● LDP (Linux Documentation Project)
○ Bash Guide for Beginners
■ http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
○ Advanced Bash Scripting Guide
■ http://tldp.org/LDP/abs/html/
● Scripting topic guide
○ Attached to the lab!
lab assignment
● Lab has more detail than this lecture
● Make a Unix phonebook!
● decal.ocf.berkeley.edu/labs/b3