Chapter 13: Shell Programming
Boiled down:
test |
if |
case |
script variables |
loops |
functions |
Writing a shell script
- .bash_profile to set PATH
- add . to PATH
- Source .bash_profile to run profile
- put Unix commands into file to create a script
- Start with #!/bin/sh
- chmod u+x file to make script executable
- ./script to run file
Parameters and Variables
- myvar=abc
- to access user defined variables - $myvar
- Keyword Variables - $HOME, $PATH
- * is used as wildcard. If a directory listing included two files
that start with m (m1 and m2), then ls m* would display m1 and m2
- readonly myvar
- cannot give myvar a value
- Variable attributes can be assigned with the declare statement
- declare -r myvar="I am a constant"
- -a declares as an array
- -f declares as function name
- -r makes variable read only
- myvar=`whoami`
Conditional Scripting examples
Try writing this Scripting
Loop Scripting examples
Advanced Scripting examples
Practical Scripting examples
All work herein is subject to copyright. Original content to Dr. Deborah Whitfield, text content (Your UNIX/Linux) to Prentice Hall publishing.