CS 114 - Unix Tools - Fall 2004

Assignment 4 - Frequently Answered Answers

Here are some answers to some frequently asked questions.
  1. Don't forget #!/bin/sh and to chmod +x your script.
  2. Quote your variables with double quotes.
  3. sed reads from stdin and writes to stdout. You need to pass the old file name to sed so that it can read it from stdin. Use a pipe (|).
  4. Use backquotes to capture the output of a program. The shell will substitute the output of cmd for `cmd`
  5. The backquote key is usually to the left of the 1.
  6. The sed expression is in $1. The files are in $2, $3, ... Save the sed expression in a variable and use shift to get the file anmes into $1, $2, ...
  7. "$@" is equivalent to "$1" "$2" "$3" ... Try using a for loop over "$@".
  8. $? contains the exit status of the last command executed.
  9. Use exit 1 to exit your script after reporting an error.
  10. Tilde.