1 / 50

ITEC 400 Perl and Man Pages

ITEC 400 Perl and Man Pages. George Vaughan Franklin University. Topics. What is Perl? Types Storage Concepts File Interaction Flow Control Pattern Matching Man Pages. What is Perl?. Perl – Practical Extraction and Report Language Created by Larry Wall Evolving Language

jacoba
Télécharger la présentation

ITEC 400 Perl and Man Pages

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. ITEC 400Perl and Man Pages George Vaughan Franklin University

  2. Topics • What is Perl? • Types • Storage Concepts • File Interaction • Flow Control • Pattern Matching • Man Pages

  3. What is Perl? • Perl – Practical Extraction and Report Language • Created by Larry Wall • Evolving Language • Current version is Version 5 • Type: perl –v to find the version in your environment

  4. Quick Notes on Syntax • Quotes behave like those in shell scripts: • Double Quotes: treat contents as a single value (allowing for variable interpolation). • Single Quotes: treat contents literally. • Backwards Quotes: execute contents • Backslash interpolation: can be used to encode special characters • (carriage return = \n).

  5. Types • Perl does not require you to declare variables or variable type. • Perl will interpret values such as string, integer, float, character, command, etc. based on context. • Variables not yet defined have values of either “” or zero.

  6. Storage Concepts • At a simplified level, Perl views variables of being one of two types: • Singular – variables that hold a single value (scalar). Example: a number or string. • Plural – variables that represent multiple values (arrays, hashes). Example: an array of numbers or array of strings.

  7. Singular (Scalar) Variables • Singular (scalar) variables are prefixed with a dollar ($) symbol: $myVar = “A String”; print $myVar, “\n”; • Notice that the Perl variable name is prefixed with “$” during assignment in addition to reference (unlike shell).

  8. A Note On Examples • Source for all examples in this and future lectures are on codd (einstein) at: /export/home/vaughang/public_html/itec400/examples • and on the web at: • http://cs.franklin.edu/~vaughang/itec400/examples/ • All shell examples are written in Korn Shell (ksh) although they should be quite compatible with Bash. • In all examples, the ‘greater-than’ symbol, ‘>’ will be used to indicate the shell prompt. So, if we execute the date command, >date Thu Sep 2 20:43:58 EDT 2004 we only type ‘date’ and not ‘>date’. • All scripts should be able to execute on codd (einstein) which is a Solaris machine, or any Linux distribution that supports Korn shell, located at /bin/ksh.

  9. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0310 0005 # 0006 # A simple Perl script that demonstrates 0007 # the use of singular (scalar) variables 0008 ########### 0009 $time = `date`; 0010 $text = "Hello, the time is $time\n"; 0011 print $text; Line 1: note to shell that this is a perl script Use path specified by “which perl”. Line 9: capture output of date command in scalar variable $time. Line 11: print contents of $text to stdout. Notice that that each statement is terminated with a semicolon. OUTPUT: >ex00310 Hello, the time is Sun Sep 19 12:33:19 EDT 2004 Example ex0310

  10. How To Execute A Perl Script • There are several ways: • Type: perl ex0310 • Adding “#!/usr/bin/perl -w” to top of script and setting permissions for execution allows us to simply type: ex0310 • The perl ‘-w’ option will produce warning messages (handy for debugging)…

  11. Plural Variables • 2 Types: arrays and hashes • Arrays – useful when using numeric position to retrieve information (example: room number). • hashes – useful when using a key to retrieve information from a list (example: license plate number)

  12. Arrays • An ordered list of numbers, strings, and/or references to arrays or hashes. • Array variables are prefixed with an ‘at’ symbol (@). • Array length is not declared and arrays can grow dynamically at run time. • $length_of_myArray = scalar(@myArray); • In the next example, you will see how to: • assign a list to an array • assign an array to a list • assign a value to an element • use an array as a stack

  13. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0320 0005 # 0006 # A simple Perl script that demonstrates 0007 # the use of array (plural) variables 0008 ########### 0009 0010 @days = ("mon", "tue", "wed", "thu"); 0011 ($day1, $day2, $day3) = @days; 0012 print "The third day = $day3\n"; 0013 $days[4] = "fri"; 0014 print "The fifth day = $days[4]\n"; 0015 push @days, "sat"; 0016 push @days, "sun"; 0017 print "The sixth day = $days[5]\n"; 0018 print "The seventh day = ", 0019 pop @days, "\n"; Line 10: assign a list of literals to an array. Line 11: assign first 3 elements of array to list of singular variables. Line 13: assign a value to an array element (note: element is scalar). Array indices start at zero. Lines 15 and 16: Treat array as stack and push 2 values on stack. Line 17: print array element Liine 18 and 19: pop stack and print its value. OUTPUT: The third day = wed The fifth day = fri The sixth day = sat The seventh day = sun Example ex0320

  14. Hashes • Internally implemented as a hash table. • Cannot be treated like a stack. • Collection of “key-value” pairs. • When assigning a list to a hash, the list must also be a list of “key-value” pairs. • Hash variables are prefixed with the percent sign (%).

  15. Hashes • Lists can be assigned to hashes using 2 different forms of syntax: %myHash = (key1, val1, key2, val2, ….); %myHash = ( key1 => val1, key2 => val2, … );

  16. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0330 0005 # 0006 # A simple Perl script that demonstrates 0007 # the use of hash (plural) variables 0008 ########### 0009 0010 %states_1 = ( "oh", "Ohio", "fl", "Florida", 0011 "de", "Delaware" ); 0012 print "I live in $states_1{oh}\n"; 0013 0014 %states_2 = ( 0015 "oh" => "Ohio", 0016 "fl" => "Florida", 0017 "de" => "Delaware" 0018 ); 0019 print "I was born in $states_2{de}\n"; Line 10: assign key-value pair list to hash. Line 14: assign key-value pair list to hash using an alternative syntax format. OUPUT: I live in Ohio I was born in Delaware Example ex0330

  17. Array and Hash Slices • It is possible to grab slices (subsets) of arrays and hashes. • The slice itself is always an array. • Arrays: @myArray[n..m] #n and m is index range @myArray[n, m, o] #n, m, and o are indices • Hashes: @myHash{‘n’, ‘m’} #n and m are keys # The @ indicates that # the result is an array.

  18. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0340 0005 # array and hash slices 0006 ########### 0007 0008 @days = ("mon", "tue", "wed", "thu", 0009 "fri", "sat", "sun"); 0010 @weekdays = @days[0..4]; 0011 print "weekdays = @weekdays\n"; 0012 @tdays = @days[1,3]; 0013 print "tdays = @tdays\n"; 0014 %states_1 = ( "ca", "California", "fl", 0015 "Florida", "de", "Delaware" ); 0016 @eastStates = @states_1{'fl', 'de'}; 0017 print "eastStates = @eastStates\n"; Line 8: create array “days” Line 10: grab slice of array “days” (i.e. elements 0 through 4). Line 12: grab slice of array “days” (i.e. elements 1 and 3). Line 14: create hash “states_1” Line 16: grab slice of hash (entries for Florida and Delaware) OUTPUT: $ex0340 weekdays = mon tue wed thu fri tdays = tue thu eastStates = Florida Delaware Example ex0340

  19. File Interaction • Opening a file for Reading • Reading from a file • Opening a file for Writing • Writing to a file

  20. File Handles • A file handle is nothing more than a program defined name for a file. • The file handle name does not have to match the file name. • The file handle is bound to a file or pipe with the open statement.

  21. Opening Files and Pipes • Read from an existing file: • open(MY_FILE, “my_file”), open(MY_FILE, “<my_file”) • Create a file and write to it: • open(MY_FILE, “>my_file”) • Append to and existing file: • open(MY_FILE, “>>my_file”) • Set up an output filter: • open(MY_FILE, “| command”) • Set up an input filter: • open(MY_FILE, “command |”) • Special File Handles: STDIN, STDOUT and STDERR

  22. Closing a File or Pipe • close FILE_HANDLE • A file will be closed automatically if its file handle is used in another open statement. • All files will be closed upon completion of the script.

  23. Reading from a File or Pipe • The angle brackets are used for reading a line of input. • $myVar = <MY_FILE> will read one line of text from the file referenced by MY_FILE. • $myVar = <STDIN> will read a line from standard in (usually the keyboard). • $myVar = <> will read a line from one or more files on the command line (or STDIN if no files were specified). • Reading a line from a file or STDIN will also read the new-line character (/n). Use “chomp()” to remove new-line character.

  24. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0350 0005 # 0006 # A simple Perl script that demonstrates 0007 # the use of working with files 0008 ########### 0009 0010 open(PASSWD, "/etc/passwd") or die "Can't open passwd: $!\n"; 0011 $total = 0; 0012 while ($line = <PASSWD>) { 0013 @fields = split(":", $line); 0014 ($uid, $name) = ($fields[0], $fields[4]); 0015 print "$uid, $name\n"; 0016 ++$total; 0017 } 0018 print "total = $total\n"; Line 10: open file Line 12: For each loop, assign the next line of input to $line Line 13: Break up input along the field separator, ‘:’, and assign each piece to an array element. Line 14: multiple assignments OUPUT (subset): vaughang, George Vaughan swartwod, Don Swartwout pandarir, Raghavender R. Pandari ettefagm, Mohamad Ettefagh fleigd, David Fleig forsheyr, Ralph Forshey total = 255 Example ex0350

  25. String Operators • Most of the familiar operators are described in the book. • String Concatenation Operator “.” $a = $b . $c; $a .= $b; • String Multiplication Operator “x” $a = “a” x 3; #$a = “aaa”

  26. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0355 0005 # Create histogram of processes per user 0006 ########### 0007 open(PROCESSES, "ps -ef | sed '/UID/d' |") 0008 or die "Can't open processes: $!\n"; 0009 $marker = "*"; 0010 while ($line = <PROCESSES>) { 0011 @fields = split(" ", $line); 0012 $uid = $fields[0]; 0013 $processes{$uid}++; 0014 } 0015 0016 foreach $uid (sort keys %processes) { 0017 printf ("%8s: %4d ", $uid, $processes{$uid}); 0018 print $marker x $processes{$uid}, "\n"; 0019 } Line 7: Open pipe using output of ‘ps’ Lines 10-14: count number of processes per user Lines 16-19: For each user on the system, print name, number of processes and chart. Line 17: Notice use of printf function to format output. Line 18: string multiplication OUTPUT (Subset): vaughang: 4 **** velez04: 1 * wachir01: 1 * wangt: 1 * yang05: 2 ** yeh04: 2 ** yehpo: 2 ** Example ex0355

  27. Math Operators • Very similar to C. • Book provides good overview

  28. Comparison Operators

  29. Conditional File Operators

  30. Flow Control • “if” and “if, else” • “unless” • “while” • “for” • for each”

  31. “if” if (conditional_expr) { statement_list } “if, else” if (conditional_expr) { statement_list } else { statement_list } Note: curly braces are mandatory “if” and “if, else”

  32. “elsif” if (conditional_expr) { statement_list } elsif (conditional_expr) { statement_list } “unless” unless (conditional_expr) { statement_list } “elsif” statements can be daisy-chained. an “elsif” statement can be followed by an “else” statement. “unless” is like saying: if something is false, then execute block. “elsif” and “unless”

  33. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0360 0005 # Demo of if, elsif, unless 0006 ########### 0007 unless (scalar(@ARGV)==2) { 0008 print "usage: ex0360 v1 v2", 0009 "\n"; 0010 exit 1; 0011 } 0012 if ($ARGV[0] > $ARGV[1]) { 0013 print "number $ARGV[0] is greater "; 0014 print "than $ARGV[1]\n"; 0015 } 0016 else { 0017 print "number $ARGV[0] is less than"; 0018 print " or equal to $ARGV[1]\n"; 0019 } 0020 exit 0; Line 7: execute block if condition is false. Line 7: test if there are exactly 2 command line arguments. Line 10: exit with return status of 1. Line 12: Numerical comparison of command line argument 1 and 2. Output 1: >ex0360 3 2 number 3 is greater than 2 Output 2: >ex0360 2 usage: ex0360 v1 v2 Example: ex0360

  34. “while” while (conditional_expr) { statement_list } “do while” do { statement_list } while (conditional_expr) ; See ex0350 for example of “while” “while” loop will execute zero or more times. “do while” loop will execute one or more times. “while” and “do while” Loops

  35. “until” and “do until” Loops • “until” until (conditional_expr) { statement_list } • “do until” do { statement_list } until (conditional_expr) ; • “until” loops are like “while” loops except we loop as long as the conditional expression is false. • “until” loop will execute zero or more times. • “do until” loop will execute one or more times.

  36. for (a; b; c) { statement_list } where: a = initial conditions b= conditional expression c = iterative action Same format as C, C++ and Java. “for” Loops

  37. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0365 0005 # Demo of 'for' loop 0006 ########### 0007 0008 #print out the numbers 3 to 6 0009 for ($i=3; $i <=6; ++$i) { 0010 print $i, "\n"; 0011 } 0012 0013 print "\n"; 0014 0015 #print the even number 0016 #between 2 and 10 0017 for ($i=2; $i<10; $i=$i+2) { 0018 print $i, "\n"; 0019 } Lines 9 – 11: for loop print number from 3 to 6 Lines 17-19: for loop prints even numbers between 2 and 10 OUTPUT: >ex0365 3 4 5 6 2 4 6 8 Example: ex0365

  38. foreach $var (list) { statement_list } Loop for each element in list. For each iteration, set $var to be an alias to the next element in the list. See ex0355 “for each” Loops

  39. Pattern Matching • “//” is the pattern matching operator - used to match regular expressions. • /good/ - match on literal string “good” • if ($myVar =~ /good/) • true if $myVar contains “good” (e.g. “good dog”, “goody”) • “=~“ is the pattern binding operator • if (/good/) • true if default string ($_) contains “good”.

  40. Regular Expressions • Previous slide showed matching on literals. • We can also match on regular expressions: • /a[a-g]/ - match on a string that contains a substring that starts with “a” and is followed by a letter between “a” and “g”, inclusive. Example: “ab” • /a[a-g]*z/ - match on a string that contains a substring that starts with “a”, followed by zero or more characters from “a” to “g” inclusive and ends with “z”. Examples: “aaz”, “abz”, “yabababz”, “gaz” • /a[a-g]+z/ - match on a string that contains a substring that starts with “a”, followed by one or more characters from “a” to “g” inclusive and ends with “z”. Examples: “aaz”, “abz”, “yabababz”, NOT “gaz”

  41. 0001 #!/usr/bin/perl -w 0002 0003 ########### 0004 # File: ex0370 0005 # Demo of if, elsif, unless 0006 ########### 0007 $_ = $ARGV[0]; 0008 if ($ARGV[0] =~ /a[a-g]/){ 0009 print "$ARGV[0] is in the "; 0010 print "language \"a[a-g]\"\n"; 0011 } 0012 if (/a[a-g]*z/){ 0013 print "$ARGV[0] is in the "; 0014 print "language \"\[a-g]*z\"\n"; 0015 } 0016 if (/a[a-g]+z/){ 0017 print "$ARGV[0] is in the "; 0018 print "language \"a[a-g]+z\"\n"; 0019 } Line 7: assign the first command line argument to the default string. Examples: $ex0370 ab ab is in the language "a[a-g]“ $ex0370 az az is in the language "[a-g]*z“ $ex0370 abz abz is in the language "a[a-g]" abz is in the language "[a-g]*z" abz is in the language "a[a-g]+z" Example ex0370

  42. Man Pages • Man pages are online documents that describe the use of commands, system calls and other functions. • All Unix systems have a set of man pages for Unix commands, system calls, functions etc. • Applications may also come with their own set of man pages which are usually maintained in an area separate from system man pages.

  43. Man Page Sections • Man Pages are broken up into different categories. • The categories are identified by section number (Linux and Solaris):

  44. Man Page Usage • To use, simply type: man command • Example: man sleep • Sometimes a name will appear in more than one section (name maybe command and function). • To specify a specific section to search, type: man –s2 time (will show system call version instead of command version) • Solaris uses lower case ‘s’ and Linux uses capital ‘S’ in –s option

  45. More Than One Set • It is quite common that applications have their own sets of man pages. • If many applications are installed, users may be interested in having access to multiple sets of man pages. • The different sets of man pages that will be searched and the order they are searched are stored in the environment variable: $MANPATH

  46. More Than One Set • A user may add a new set by appending the path of the man pages to $MANPATH. • Example: MANPATH=$MANPATH:$LOGNAME/itec400/man export MANPATH

  47. How Do You Create Your Own Man Pages • First, you need to create a directory to hold the man pages: mkdir ~/itec400/man • Next, you need to create subdirectories for those sections that might have man pages. • Assume you will only have commands: mkdir ~/itec400/man/man1

  48. How Do You Create Your Own Man Pages • Next you need to create a file that describes the command. • Assume the command name is ‘mycommand’ • the man file name for this command would be: ‘mycommand.1’ • So, ‘mycommand.1’ would be stored in ~/itec400/man/man1 • Finally, we need to change $MANPATH: MANPATH=$MANPATH:~/itec400/man export MANPATH • Now we can type: man mycommand

  49. How Do You Create Your Own Man Pages • Man page files are text files, however, they use a special formatting language: nroff • nroff has its man page • Sometimes it is easiest to copy and modify the source file of an existing man page • For example, the source file for gcc is: /usr/local/man/man1/gcc.1

  50. References • “Programming Perl”, Larry Wall, Tom Christiansen and Jon Orwant, 2000 • “Essential System Administration”, by Aeleen Frisch, 2002

More Related