1 / 29

Review

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

terri
Télécharger la présentation

Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review

  2. 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

  3. Common UNIX utilities • pwd • cat , more, page, head, tail • ls, cd • mv, cp, rm • mkdir, rmdir • file, wc , lp • vi • groups, chgrp, chmod

  4. 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

  5. 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)*

  6. 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]

  7. find Utility • awk • tar Utility • Stream Editor (sed)

  8. $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

  9. $$ 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

  10. 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

  11. Case Structure case expression in pattern)‏ list ;; pattern2)‏ list2 ;; ... *) # default list_n ;; esac

  12. 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

  13. 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

  14. 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

  15. 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

  16. C programming • Operator precedence and associativity • Pointers arithmetic • Relationship between array and pointers • Linked List operations

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. for (expr1; expr2; expr3) statement Statement can be compound: {} expr1; while (expr2) { statement expr3; } Infinite loop: for (;;) break statement continue statement for Loop

  23. return type function-name (parameters) { declarations statements } Function can not return array Pass-by-value Pass-by-reference Functions

  24. 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”};

  25. Compiling and Linking

  26. 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

  27. Error Handling • errno • perror()

  28. File Management • open • read/write • lseek • close • Stat • Fcntl • chmod

  29. Process Management • fork()‏ • getpid()‏ • getppid()‏ • exit()‏ • wait()‏ • exec()

More Related