Slide 1

Slide 1 text

Shell Scripting Lessons from a year administering remote machines

Slide 2

Slide 2 text

Basic Concepts

Slide 3

Slide 3 text

Shell Anatomy 101 • time php test.php 2>&1 | grep $NEEDLE > results.txt Shell Builtin Executables Arguments Redirection Pipe Environment Variable output builtin: a regular command, except provided by your shell instead of an executable executables: a file in your path marked as executable arguments: you all know what this means Redirection: Swapping around the standard streams

Slide 4

Slide 4 text

Process Pipes, a review Process stdin stdout stderr 2 1

Slide 5

Slide 5 text

2>&1 Process stdin stdout stderr 2 1

Slide 6

Slide 6 text

Shell Anatomy 101 • time php test.php 2>&1 | grep $NEEDLE > results.txt Shell Builtin Executables Arguments Redirection Pipe Environment Variable output Pipe: Special case of redirection, connects stdout to stdin of the other side Environment variable: a variable that gets the value substituted by the shell

Slide 7

Slide 7 text

Tools to Know About

Slide 8

Slide 8 text

man • read the manual page for a given command • $ man grep info also exists, but that means using emacs-like bindings… ugh!

Slide 9

Slide 9 text

cat • Puts the contents of a file onto stdout • $ cat “hello.txt”
 Hello, world

Slide 10

Slide 10 text

head/tail • Puts the beginning or end of a file on stdout • great for dealing with logs, especially with -f • $ tail -f /var/log/system.log
 Hello, world

Slide 11

Slide 11 text

ps • list information about processes • $ ps axu | grep grep
 bnicholas 4521 0.0 0.0 2453264 692 s000 S+ 10:26PM 0:00.00 grep grep

Slide 12

Slide 12 text

kill/killall • send a signal to processes by pid or name • $ kill -SIGUSR1 2048 • $ killall perl

Slide 13

Slide 13 text

sort • Takes lines from stdin and sorts them on stdout • $ cat file.txt | sort • $ cat numbers.txt | sort -n

Slide 14

Slide 14 text

uniq • Removes duplicate lines from stdin to stdout • $ cat file.txt | sort | uniq • $ cat ~/history | sort | uniq -c | sort -n

Slide 15

Slide 15 text

grep • Search for a regular expression • can be used on stdin or files • $ cat /var/log/product.log | grep -i “error”

Slide 16

Slide 16 text

tr • “trim” text, replacing characters with others • $ cat sentence.txt | tr ‘ ‘ ‘\n’
 My
 name
 is
 ben.

Slide 17

Slide 17 text

sed • stream editor: a mini text editor • Similar to vim find+replace syntax, but so much more too • $ echo “hello world!” | sed -e “s/ w.*d/bn.u/“
 hello bn.u!

Slide 18

Slide 18 text

find • A file finder with extra • $ find . -name “*.orig” -print - delete • $ find . -type f —mtime -1 -print This will find all the files ending in .orig (merge files from git), print them out, and then delete them exec also allows for running any command selectors let you pick files very concisely

Slide 19

Slide 19 text

awk • a small programming language for dealing with columns • ps aux | grep grep | awk ‘{print $2}’
 6845 So much more complicated and powerful than I have time for I’ve only scratched the surface, but it’s really helpful for certain things this example prints the pid of any grep processes

Slide 20

Slide 20 text

Control Flow

Slide 21

Slide 21 text

conditionals • if [ $VAR = “foo” ]
 then
 echo “bar”
 else
 echo “baz”
 fi explain test, and how dumb it can be sometimes -eq for integer equality, -gt -lt -ne…

Slide 22

Slide 22 text

Better conditionals • Use && and || when possible • cat a.txt | grep “hello” && echo “world”

Slide 23

Slide 23 text

For loops • for FILE in `ls`
 do
 cat $FILE
 done

Slide 24

Slide 24 text

SSH The magic glue that holds things together

Slide 25

Slide 25 text

Key-based auth • More secure and convenient than passwords • ssh-copy-id @ • Last time you’ll need to type a password • Also applies to git, scp, sftp, and others

Slide 26

Slide 26 text

~/.ssh/config • Set up usernames across computers • Keep connections alive longer • Set up tunnels tunnels: local ports actually go through to the remote server good things like accessing servers that are firewall’d out

Slide 27

Slide 27 text

Great Tricks/Tools

Slide 28

Slide 28 text

autocomplete stop typing and hit tab. 95% of the time it’s gonna do what you want. trust your shell.

Slide 29

Slide 29 text

fish If you’re still using bash, stop it, install fish, make all of your config 100000x easier autocomplete by default, web config, syntax highlighting and checking, sane scripting, small config files (instead of one monolithic _THING_), man page completion

Slide 30

Slide 30 text

ack http://beyondgrep.com it’s faster grep that ignores the files you don’t want. Simple as that.

Slide 31

Slide 31 text

tmux If you’re working on a remote machine, this will save you so many times (worked on a laptop, connection on and off, long running tasks that never died)

Slide 32

Slide 32 text

vim This is going to start a war, I know, but knowing a command line editor helps a lot vim is the one of the most powerful, nano, pico, emacs all work, but why?

Slide 33

Slide 33 text

dsh Elliot and James swear by it, I don’t know if it’s worth the config time, but it depends on how you work

Slide 34

Slide 34 text

Some example problems