7. “Many C/C++/Java candidates, even some with
10+ years of experience, would happily spend a
week writing a 2,500-line program to do
something you could do in 30 seconds with a
simple Unix command.”
– Steve Yegge
https://sites.google.com/site/steveyegge2/five-essential-phone-screen-questions
8. How can shell scripts help me?
● As developers, we make computers work for
others.
● Computers could work a lot for ourselves, too.
● Shell script is a faster way to get work done.
12. How good is shell script?
● Very good for quick, one-off tasks.
● Great for small tasks.
● Also good to deal with a lot of files, download
stuff.
● Great option to automate a set of steps.
● Can get confusing fast.
Peter H. Salus. A Quarter-Century of Unix apud http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
13. “Python [and other languages -me] scales up to
thousand of lines, shell script scales down to a
few keystrokes.”
– Somebody at Stack Overflow
Alas, I couldn’t find the link, not even the name of the user :(
15. Unix Philosophy
● Write programs that do one thing and do it well.
● Write programs to work together.
● Write programs to handle text streams.
Peter H. Salus. A Quarter-Century of Unix apud http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
16. Unix Philosophy
● Small utilities.
● Pipes and redirects.
● “Everything is a file.”
– Including standard input/output/error.
Peter H. Salus. A Quarter-Century of Unix apud http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
27. Quoting and Escaping
$ echo This is my message.
This is my message.
$ echo "This is my message."
This is my message.
$ echo 'This is my message.'
This is my message.
28. $ echo Good times (are coming)
bash: syntax error next to unexpected `token' `('
$ echo "Good times (are coming)"
Good times (are coming)
$ echo 'Good times (are coming)'
Good times (are coming)
Quoting
29. $ echo Good times (are coming)
bash: syntax error next to unexpected `token' `('
$ echo "Good times (are coming)"
Good times (are coming)
$ echo 'Good times (are coming)'
Good times (are coming)
Quoting
30. $ echo This is a little star: *
This is a little star: languages OSes
$ echo 'This is a little star: *'
This is a little star: *
$ echo This is a little star: *
This is a little star: *
Escaping
34. $ echo "This is Dave's situation."
This is Dave's situation.
35. $ echo This is a "situation."
This is a situation.
$ echo 'This is a "situation."'
This is a "situation."
$ echo "This is "Dave's situation.""
This is "Dave's situation".
Quoting
38. $ VAR=this
$ echo My value is $VAR
My value is this
$ echo "My value is $VAR"
My value is this
$ echo 'My value is $VAR'
My value is $VAR
Variables
42. grep
● Search for lines in one or more files.
– Or from standard input.
● “Basic” or “extended” regular expressions.
– Or even plain strings and PERL regexes.
● Many options
– Inverse match.
– Only match.
– Show line numbers...
43. grep
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judite (61) 9 9612 9222
47. sed
● A little weird programming language.
● Most of the time, we use the s/// command.
● Can alter the file inline.
48. sed
$ sed 's/Judite/Judith/' phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judith (61) 9 9612 9222
49. sed
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judite (61) 9 9612 9222
$ sed -i 's/Judite/Judith/' phones.txt
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judith (61) 9 9612 9222
50. awk
● A full-fledged programming language.
– Similar to (predecessor of) Perl, PHP.
– Most programmers would be comfortable with.
● Read files as tables.
– Each line is a record.
– Each space-separated part a column.
51. awk
$ awk '$1 == "Paul"{print}' *
Paul (882) 830 3802
$ awk '$1 ~ "Paul"{print}' *
Paul (882) 830 3802
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
53. Pipes
● Redirect standard output from one command to
another command’s standard input.
● Can also redirect standard error, sure.
● This is how most of the magic happens :)
54. Pipes
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judith (61) 9 9612 9222
55. Pipes
$ cat phones.txt | grep '^P'
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
56. Pipes
$ cat phones.txt | grep '^P' | sed 's/ [0-9]+$/ ****/'
Pedro (81) 9 9653 ****
Paulo (81) 9 8614 ****
Paula (61) 9 8112 ****