Shell Programming Examples: Case Studies

10/30/04

Case Study #1: show certain file characteristics in nice columns

Also illustrates Usage error,
Errors going to standard error,
Printf to output in nice columns,
set to access fields in a line,
bc to calculate with fractions.

Case Study #2: show which files from a list do exist

Also illustrates how to use unique temp files so multiple users can run the script simultaneously,
How to make sure the temp files are deleted when the program is done or if the user aborts it (via INTR or QUIT),
How to use sed to add chars to the start of the line,
Using comm to compare two sorted lists,
Also illustrates that trap variables are evaluated when trap code is defined so we make trap call a subroutine so that variables will be evaluated when the subroutine is invoked. We use this technique to return a valid zero vs. non-zero exit status.

Case Study #3: How to output to different files from one script

Case Study #4: how to ask for user input until valid input is given.

Also illustrates how to check if no characters were input,
check if input exceeds 30 characters in length,
check if input lacks both first and last name,
exit if too many wrong answers.

Case Study #5: How to process a list of files, generated by several methods

Case Study #6: How to remember where stdout is going, redirect stdout somewhere else, then restore stdout back to what it was

Case Study #7: Access each word in each line of a file

Also illustrates how to redirect stdin to a while loop,
how to use "set" and "for" to access each word in the line,

Case Study #8: Using the case statement to implement a menu

Also illustrates how echo can output multiple lines,
How to use a shell function/subroutine,
How to backup the system using cpio,
How to save and report the status of the cpio backup,
How to save start/end time and errors in a log,
How to force output to the screen (/dev/tty) even if stdout is redirected away from the screen.