Bash Programming: Andrew Vandever RHC (T, E, I, X)
Bash Programming: Andrew Vandever RHC (T, E, I, X)
Bash Programming
Andrew Vandever Technical Training Resources Scripting Basics Expansion Loops Conditional Statements I/O Redirection User Input System V Init Scripts
Scripting Basics
Bourne Again Shell Posix-Compliant Interactive Shell Script Automation, Repetition NOT exhaustive NOT covering external programs Check out guides on tldp.org
Scripting Basics
Magic - #!/bin/bash Add -x to debug Comments - # ; = EOL \ escapes EOL chmod +x echo $PATH
Expansion
VARIABLE=value
All-caps not needed, just recommended {} not always needed, but stops problems $ is necessary Specifies variable is an integer
echo ${VARIABLE}
declare -i VARIABLE=value
Expansion
Math
Curly-brace expansion
Expansion
Command Substitution
$(command) `command` ~ = your home directory ~user = user's home directory man 7 glob
Tilde
Wildcard globs
Expansion
Prevention
\ escapes a single special character escapes all but $, ` and \ '' escapes all special characters
Loops
for VAR in items in list; do commands; done exit status - $? test - [ condition ] while [ test ]; do commands; done until [ test ]; do commands; done
Conditional Statements
Operators
&& ||
Conditional Statements
commands ;; commands ;;
*)
esac
I/O Redirection
Channel 2 STDERR
Channel 1 - STDOUT
I/O Redirection
Combining 1 and 2
Channel 0 STDIN
User Input
Arguments
$1, $2, ${10}, etc. $* $# $0 Force your users to give you the data you want in the form you want
read
System V Scripts
Help start and stop background processes at boot time and shutdown Necessary components:
chkconfig line to tell position, default levels case statement to accept typical service arguments start, stop, restart, status
Examples in /etc/init.d
Bash Programming