------------------------ Week 8 Notes for DAT2330 ------------------------ -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) ------ Review ------ In Week 7 (week07notes.txt) you learned about gzip/gunzip and bzip2/bunzip2. You understand how your umask affects the permissions of new files and directories. You know how to set PATH and umask at the start of a shell script. You can write basic shell IF/ELIF/ELSE/FI, WHILE/DO/DONE, and FOR/IN/DO/DONE control structures. You can compile and run a basic C and C++ program. You now know some basic meaning and usage of these Unix/Linux commands: bash passwd pwd cd mkdir rmdir rm ls vim cat more less man echo cp stty du wc date chmod who mesg mv head tail touch sort grep file diff sleep which whereis ^Z fg bg kill ln tar ssh scp ftp umask if/else/elsif/fi while/do/done for/in/do/done gzip gunzip bzip2 bunzip2 zcat zless zgrep ------------------ This Week (Week 8) ------------------ In-Lab Exercise: You did an in-lab Exercise #7 and submitted it in the lab. (See the Notes readings, below, for the sample solution.) Unix commands used this week (RTFM): if/else/elif/fi while/do/done 1. Readings in Running Linux: * "Shell Programming" Chapter 13, p.448 2. Read these files under the Notes button on the course home page: script_practice1.txt - Practice Script #1 Specifications labweek8.sh.txt - Sample solution for script_practice1.txt exit_status.txt - Return Code, Exit Status, test, if, and while shell_script_execution.txt - How a Shell Script is "Executed" quick_tests.txt - Return Code, Exit Status, ||, &&, test, and if less_code.txt - Less Code is Better Code - stop cutting and pasting script_style.txt - Shell Script Programming Conventions and Style deep_nesting.txt - Avoiding deeply nested IF statements redirection.txt - Unix Shell I/O Redirection (including Input) script_checklist.txt - Shell Script Checklist - things to verify *.sh.txt - many sample shell scripts 3. Start work on DAT2330 Exercise #8 (when available). Submit it by the due date. Notes: - The DAT2330 script header is documented in script_style.txt . - The four parts of a Good Error Message are in script_style.txt . Make sure error messages appear on standard error, not standard output. Shell variables: $# - count of command line arguments (never negative) $0 - name of the currently executing shell script $? - exit status of the previous command $* - a list of all arguments on the command line $1,$2,$3,... - first, second, third argument, ... DOUBLE QUOTE ALL VARIABLES to protect contents from unwanted shell GLOBbing. Double quotes permit the shell to expand the variables; but, they stop GLOB metacharacters from being expanded. Quoted strings are always one single argument, except for "$@" special case. Use variables to hold constants (like you use #define in C) - reduce maintenance: how many edits needed to change a constant? Command substitution: - similar to variable substitution; but, it is the output of the given Unix command that is substituted - syntax: $( any Unix command goes between parentheses ) - *all* the standard output of the command(s) betwen parentheses is captured - typical use is to assign command output to a shell variable: $ users=$( who | wc -l ) $ echo "Number of users logged in: $users" $ words=$( wc -w &2 "$0: This is an error message on standard error" - you can interpret "1>&2" to mean "redirect the output of this command line from unit 1 (stdout) to unit 2 (stderr)" More vim (Chapter 9): - yank and put: yy and p and P - replace one character: r