290 likes | 428 Views
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
E N D
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)
$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 Environment 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 Built-In Variables
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; Test Command
Case Structure case expression in pattern) list ;; pattern2) list2 ;; ... *) # default list_n ;; esac
forname [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: for .. do .. done
iflist1 then list2 eliflist3 #may be repeated several times then 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: if .. then .. fi
untillist1 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: until .. do .. done
whilelist1 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 Control Structures: while .. done
C programming • Operator precedence and associativity • Pointers arithmetic • Relationship between array and pointers • Linked List operations
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) statement … else statement if … else Statement
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); Conditional Expression
switch ( expression ){ case constant-expression: statements … 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. switch Statement
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 while 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); do Loop
for (expr1; expr2; expr3) statement Statement can be compound: {} expr1; while (expr2) { statement expr3; } Infinite loop: for (;;) break statement continue statement for Loop
return type function-name (parameters) { declarations statements } Function can not return array Pass-by-value Pass-by-reference Functions
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”};
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()