• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
The bash – defensive scripting
 

The bash – defensive scripting

on

  • 1,252 views

Fairly basic introduction to some principles and techniques for creating safe, fault-tolerant unix scripts using the Bash environment.

Fairly basic introduction to some principles and techniques for creating safe, fault-tolerant unix scripts using the Bash environment.

Statistics

Views

Total Views
1,252
Views on SlideShare
1,251
Embed Views
1

Actions

Likes
0
Downloads
12
Comments
0

1 Embed 1

http://www.techgig.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The bash – defensive scripting The bash – defensive scripting Presentation Transcript

    • The Bash: Defensive Scripting The Bash: Defensive Scripting 1Montag, 29. August 11
    • The Bash: Defensive Scripting 2Montag, 29. August 11
    • The Bash: Defensive Scripting Unix 2Montag, 29. August 11
    • The Bash: Defensive Scripting Unix Interfaces 2Montag, 29. August 11
    • The Bash: Defensive Scripting Unix Interfaces Re-use 2Montag, 29. August 11
    • The Bash: Defensive Scripting Unix Interfaces Re-use Defensive Programming 2Montag, 29. August 11
    • The Bash: Defensive Scripting Unix Interfaces Re-use Defensive Programming Readings 2Montag, 29. August 11
    • The Bash: Defensive Scripting 3Montag, 29. August 11
    • The Bash: Defensive Scripting Unix 3Montag, 29. August 11
    • The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit 3Montag, 29. August 11
    • The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily 3Montag, 29. August 11
    • The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily • One tool for each task 3Montag, 29. August 11
    • The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily • One tool for each task • Human-readable data and protocols 3Montag, 29. August 11
    • The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily • One tool for each task • Human-readable data and protocols • Do the simplest thing that will do 3Montag, 29. August 11
    • The Bash: Defensive Scripting 4Montag, 29. August 11
    • The Bash: Defensive Scripting Unix: The Unix Toolbox 4Montag, 29. August 11
    • The Bash: Defensive Scripting Unix: The Unix Toolbox Get familiar with cat, cut, tee, uniq, sort, head, tail, but also grep, sed, diff, find and possibly even awk. They will save you a whole lot of work. 4Montag, 29. August 11
    • The Bash: Defensive Scripting Unix: The Unix Toolbox Get familiar with cat, cut, tee, uniq, sort, head, tail, but also grep, sed, diff, find and possibly even awk. They will save you a whole lot of work. For more inspiration: ls /usr/bin 4Montag, 29. August 11
    • The Bash: Defensive Scripting Unix: The Unix Toolbox Get familiar with cat, cut, tee, uniq, sort, head, tail, but also grep, sed, diff, find and possibly even awk. They will save you a whole lot of work. For more inspiration: ls /usr/bin Extend your Unix Toolbox 4Montag, 29. August 11
    • The Bash: Defensive Scripting 5Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces 5Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code 5Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code • Favour plain text 5Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code • Favour plain text • Treat comments as comments 5Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code • Favour plain text • Treat comments as comments • Filter, Source, Sink, Cantrip, Compiler 5Montag, 29. August 11
    • The Bash: Defensive Scripting 6Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Filter 6Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Filter Read from stdin, write to stdout 6Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Filter Read from stdin, write to stdout Perfect for pipes 6Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Filter Read from stdin, write to stdout Perfect for pipes Example: cut 6Montag, 29. August 11
    • The Bash: Defensive Scripting 7Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Source 7Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Source Read from file, write to stdout 7Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Source Read from file, write to stdout Interface to outer world, beginning of a pipe sequence 7Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Source Read from file, write to stdout Interface to outer world, beginning of a pipe sequence Example: cat 7Montag, 29. August 11
    • The Bash: Defensive Scripting 8Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Sink 8Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Sink Read from stdin, write to file 8Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Sink Read from stdin, write to file Interface to outer world, end of a pipe sequence 8Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Sink Read from stdin, write to file Interface to outer world, end of a pipe sequence Example: > 8Montag, 29. August 11
    • The Bash: Defensive Scripting 9Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Cantrip 9Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Cantrip Do sth. and return nothing but a status code (or errors) 9Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Cantrip Do sth. and return nothing but a status code (or errors) This is super scriptable 9Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Cantrip Do sth. and return nothing but a status code (or errors) This is super scriptable Example: rm 9Montag, 29. August 11
    • The Bash: Defensive Scripting 10Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Compiler 10Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Compiler Read from file, write to another file 10Montag, 29. August 11
    • The Bash: Defensive Scripting Interfaces: Compiler Read from file, write to another file Example: tar 10Montag, 29. August 11
    • The Bash: Defensive Scripting 11Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use 11Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use • Functions 11Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use • Functions • Wrappers 11Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use • Functions • Wrappers • $() (aka ``) 11Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use • Functions • Wrappers • $() (aka ``) • source (aka ‘.’) 11Montag, 29. August 11
    • The Bash: Defensive Scripting 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 } 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 } log “Cannot connect” 12Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 } log “Cannot connect” # Do 4 Aug 2011 10:38:58 CEST ERR Cannot connect 12Montag, 29. August 11
    • The Bash: Defensive Scripting 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 newdir=”$1” 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 newdir=”$1” test -d $newdir || mkdir $newdir 13Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 newdir=”$1” test -d $newdir || mkdir $newdir cd $newdir 13Montag, 29. August 11
    • The Bash: Defensive Scripting 14Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: $() 14Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: $() More readable than `backticks` 14Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: $() More readable than `backticks` myfiles=$(ls $HOME) 14Montag, 29. August 11
    • The Bash: Defensive Scripting 15Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: source 15Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: source More readable than “.” 15Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: source More readable than “.” source $HOME/lib/basics.sh 15Montag, 29. August 11
    • The Bash: Defensive Scripting Re-use: source More readable than “.” source $HOME/lib/basics.sh source /etc/conf/script.cnf 15Montag, 29. August 11
    • The Bash: Defensive Scripting 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap • log, stderr 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap • log, stderr • --help, --version, usage() 16Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap • log, stderr • --help, --version, usage() • Use full path names 16Montag, 29. August 11
    • The Bash: Defensive Scripting 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” test -w “$destfile” 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” test -w “$destfile” test -n “$variable” 17Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” test -w “$destfile” test -n “$variable” test -z “$nothing” 17Montag, 29. August 11
    • The Bash: Defensive Scripting 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” else 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” else echo “cannot read $sourcefile” 18Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” else echo “cannot read $sourcefile” fi 18Montag, 29. August 11
    • The Bash: Defensive Scripting 19Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || 19Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || exit 1 19Montag, 29. August 11
    • The Bash: Defensive Scripting 20Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || 20Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { 20Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { echo “Cannot read source file” >&2 20Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { echo “Cannot read source file” >&2 exit 1 20Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { echo “Cannot read source file” >&2 exit 1 } 20Montag, 29. August 11
    • The Bash: Defensive Scripting 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { echo “Cannot read source file” >&2 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { echo “Cannot read source file” >&2 exit 1 21Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { echo “Cannot read source file” >&2 exit 1 } 21Montag, 29. August 11
    • The Bash: Defensive Scripting 22Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: find 22Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: find Don’t rely on a file being in some exact place. If it needs to be, tell the user. 22Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: find Don’t rely on a file being in some exact place. If it needs to be, tell the user. find Syntax is a bit peculiar. 22Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: find Don’t rely on a file being in some exact place. If it needs to be, tell the user. find Syntax is a bit peculiar. find $install_dir -name “*README*” -type f -exec mv {} $install_dir/docs/ ; 22Montag, 29. August 11
    • The Bash: Defensive Scripting 23Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: trap 23Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: trap trap “/bin/rm -f $tmpf; exit” EXIT 23Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: trap trap “/bin/rm -f $tmpf; exit” EXIT trap “source $config” SIGHUP 23Montag, 29. August 11
    • The Bash: Defensive Scripting function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: Logs function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: Logs Write status to stderr. stderr is not for *errors* only, but for all kind of metainformation function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: Logs Write status to stderr. stderr is not for *errors* only, but for all kind of metainformation Use syslog/logger or your own logging function function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24Montag, 29. August 11
    • The Bash: Defensive Scripting 25Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: 25Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: --help, --version 25Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: --help, --version • Simplify the use of your program by providing --help and --version 25Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: --help, --version • Simplify the use of your program by providing --help and --version • On error print a usage message 25Montag, 29. August 11
    • The Bash: Defensive Scripting 26Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: 26Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: use full path names 26Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install 26Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install srcf=$base/source.txt 26Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install srcf=$base/source.txt dstf=$base/destination.txt 26Montag, 29. August 11
    • The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install srcf=$base/source.txt dstf=$base/destination.txt test -r $srcf && cp -i $srcf $dstf 26Montag, 29. August 11
    • The Bash: Defensive Scripting 27Montag, 29. August 11
    • The Bash: Defensive Scripting Readings 27Montag, 29. August 11
    • The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ 27Montag, 29. August 11
    • The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ • The Art of Unix Programming (by Eric S. Raymond; http://www.faqs.org/docs/artu/) 27Montag, 29. August 11
    • The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ • The Art of Unix Programming (by Eric S. Raymond; http://www.faqs.org/docs/artu/) • Wicked Cool Shell Scripts (No Starch Press) 27Montag, 29. August 11
    • The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ • The Art of Unix Programming (by Eric S. Raymond; http://www.faqs.org/docs/artu/) • Wicked Cool Shell Scripts (No Starch Press) • Patrick Jezek: Bash Techtalk (08/2010) http://slides.liip.ch/shell_scripting/ index.html 27Montag, 29. August 11