2. About Me
I’m a programmer (not a sysadmin)
● I like simple, readable, well tested code
● My background: Ruby, Java, C, R, PHP, bash
● I try to use the right tool for the right job
● I have seen bad code in every language
3. (My) Definitions
Shell Script
● Primarily used as command line interpreter
● Can also be used to write scripts/programs
● e.g. sh, csh, bash, tcsh, ksh, powershell
● Shell language of choice: bash
Programming Language
● Primarily for writing programs
● Rich set of libraries
● Compiled or uncompiled
● e.g. C, C++, Java, Python, Ruby, Perl, PHP
4. Why Shell Scripts are Good
● Quick to write
● Use commands you know (cp, mv, ps, etc)
● Complex tasks can be done in a short script
● Easy to try out on command prompt
● Power of unix pipes for free
5. Shell Programming Issues
What’s bad about programming in shell scripts
● Confusing syntax
● Untyped (everything is a string)
● Slow
● Not always portable
● No exception handling
6. Confusing Syntax 1
Bash
● Outputs: “No”
● Comparison operator “>” does string comparison
● Should have used “-eq” instead
7. Confusing Syntax 2
● Outputs: “No”
● Is actually a syntax error!!
● $A must be wrapped in double quotes
8. Confusing Syntax!
● Non standard comparison operators
● Variables need to be quoted... sometimes
● Variables need to be prepended with "$"...
sometimes
● Syntax errors can be caused by variable
contents
9. Untyped
● All variables behave like strings
● Some integer operations are allowed
● No built in floating point operators!!
● Can’t define your own types
10. Speed - Which is faster?
18 seconds (touch is an external command)
1 second (echo is a bash builtin)
11. Slow
● Built in commands are very limited
● You must use external commands often
● External commands are SLOW
● Slow code is not obvious. This takes 6 seconds:
● Equivalent code takes 0.1 seconds in ruby
12. Not Reliably Portable
Shell on my computer != Shell your computer
● ksh, tcsh and zsh are not common
● bash is common (but not universal)
● bourne shell (sh) is universal but weak
● None of them are likely to work on Windows
● Strong reliance on OS commands - which differ
across computers
13. No Exception Handling
● There is no built in exception handling
● There are (complicated) ways to work around
this
● Errors are hard to debug
14. When To Use Shell Scripts
Shell (Scripts)
● Working on the command line
● Wrapper to start a program
● One off hack to automate tedious task
● Keep it SHORT! (under 50 lines)
Programming Languages
● Anything with complicated logic
● Anything that needs to be fast
● Anything that needs to be portable
● Anything that should be tested
15. Generic
● Python
● Ruby
● Perl
What To Use Instead
Building / Compiling
● Make
● Rake
● Ant
● Maven
Deployment
● Capistrano
● Bamboo
● Jenkins
● Chef / Puppet
Dev-Ops
● Chef
● Puppet
● Vagrant
16. Conclusions
Understand your tools
● Shell scripts are very useful
● But - they are not a replacement for
programming languages
● Think about your choice - pick the right tool
● Happy scripting