Review
UNIX Features Multiple users access a system at the same time Support file management and process management Provide a directory hierarchy Share CPUs, memory, and disk space Allow processes and peripherals to talk to each other Standard utilities System calls A portable operating system
Common UNIX utilities pwd cat , more, page, head, tail ls, cd mv, cp, rm mkdir, rmdir file, wc , lp vi groups, chgrp, chmod
Grep Filtering patterns: egrep, fgrep, grep grep -hilnvw pattern {fileName}* displays lines from files that match the pattern pattern : regular expression -h : do not list file names if many files are specified -i : ignore case -l : displays list of files containing pattern -n : display line numbers v : displays lines that do not match the pattern -w : matches only whole words only
Grep variations fgrep : pattern must be fixed string egrep : pattern can be extended regular expression -x option in fgrep: displays only lines that are exactly equal to string extended regular expressions: + matches one or more of the single preceding character ? matches zero or one of the single preceding character | either or (ex. a* | b*) () *, +, ? operate on entire subexpression not just on preceding character; ex. (ab | ba)*
Sort sort -tc -r [+POS1 [-POS2]] {sortField -bfMn}* {fileName}* -tc separator is c instead of blank -r descending instead of ascending -b ignore leading blanks -f ignore case -M month sort (3 letter month abbreviation) -n numeric sort +POS1 [-POS2] key positions start [up to end]
find Utility awk tar Utility Stream Editor (sed)
Environment Variables $HOME : full pathname of home directory $PATH : list of directories to search for commands $MAIL : the full pathname of mailbox $USER : your username $SHELL : the full pathname of login shell $TERM : the type of your terminal
Built-In Variables $$ the process ID of the shell $0 the name of the shell script $1..$9 $n refers to the nth command line argument $* a list of all command-line arguments
Test Command test expression [ expression ] (equivalent form on some UNIX: if it’s built-in) Returns a zero if expression evaluates to true Otherwise, returns a nonzero status. Examples: $ test 2 -eq 3; $ test -e for.sh; $ test abc = abc;
Case Structure case expression in pattern) list ;; pattern2) list2 ... *) # default list_n esac
Control Structures: for .. do .. done for name [in {word}* ] do list done Loops the value of name through each word in the word list, evaluates commands in the list at each iteration. If no word list is supplied, $@ is used. break : terminate the loop immediately. continue : jump to the next iteration.
Control Structures: if .. then .. fi if list1 then list2 elif list3 #may be repeated several times list4 else #may occur zero or one time list5 fi Commands in list1 are executed. If last command succeeds, the list2 is executed. Otherwise, a successful command list in elif causes the associated then to be executed.
Control Structures: until .. do .. done until list1 do list2 done Executes list1 If the last command in list1 succeeds, it ends. Otherwise, list2 is executed and the process is repeated. If list2 is empty, the do keyword should be omitted. break continue
Control Structures: while .. done while list1 do list2 done Executes list1. If the last command in list1 fails, it ends Otherwise, list2 is executed and the process is repeated. If list2 is empty, the do keyword should be omitted. break continue
C programming Operator precedence and associativity Pointers arithmetic Relationship between array and pointers Linked List operations
if … else Statement if (expression) statement if (expression) statement else statement Statement can be compound: { statements} if (i==0) vs if (i=0) if (expression) statement else if (expression) … else
Conditional Expression expr1 ? expr2: expr3 int i, j, k; i = 1; j = 2; k = i > j ? i : j ; k = (i > 0 ? i : 0) + j ; return (i > j ? i : j); printf (“%d\n”, i > j ? i : j);
switch Statement switch ( expression ){ case constant-expression: statements … default: statements } Controlling expression should be an integer expression (characters) Constant expression can’t contain variables or function calls. Statements do not require {}. Usually, the last statement is break.
while Loop while (expression) statement Statement can be compound: {} while (i>0) printf (“%d\n”, i--); while (i>0) { printf (“%d\n”, i); i--; } Infinite loops: while(1) Break, goto, return
do Loop do statement while (expression); Statement can be compound: {} do printf (“%d\n”, i--); while (i>0) ; do { printf (“%d\n”, i); i--; } while (i>0);
for Loop for (expr1; expr2; expr3) statement Statement can be compound: {} expr1; while (expr2) { statement expr3; } Infinite loop: for (;;) break statement continue statement
Functions return type function-name (parameters) { declarations statements } Function can not return array Pass-by-value Pass-by-reference
Declaring Structures (struct) Engineering H192 Winter 2005 Declaring Structures (struct) struct date { int day; char month[3]; char year[4]; } ; /* The name “date" is called a structure tag */ Struct date date1; Struct date date2 = {3,”Jan”,”1912”}; Struct { int day; char month[3]; char year[4]; } my_date ; my_date date3 = {2,”Jul”,”2010”}; Instructor: A structure can be defined in two ways. The first method gives the compiler the layout or structure of the struct, but does not actually create one for use. This is useful when the programmer wishes to create global definitions for structs that he or she wishes to use later. The second method varies only in that a variable name now exists after the closing brace of the structure. This variable is now a struct which contains all the variables within the struct definition. Accessing the data in this variable will be discussed in a few slides. Lecture 23
Compiling and Linking
Example of Makefile main1: main.o power.o cc power.o main.o -o main1 main.o: main.c power.h cc -c main.c power.o: power.c power.h cc -c power.c
Error Handling errno perror()
File Management open read/write lseek close Stat Fcntl chmod
Process Management fork() getpid() getppid() exit() wait() exec()