1 / 62

ITEC 400 More Perl and Unix/Linux Files

ITEC 400 More Perl and Unix/Linux Files. George Vaughan Franklin University. Topics. Perl Comparison and Conditional Operators String Manipulation Functions Subroutines Name Scope Scalar and List Context Subroutines with List and Scalar Context Modules Getopt Module “Here” Document

raina
Download Presentation

ITEC 400 More Perl and Unix/Linux Files

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 400More Perl and Unix/Linux Files George Vaughan Franklin University

  2. Topics • Perl • Comparison and Conditional Operators • String Manipulation Functions • Subroutines • Name Scope • Scalar and List Context • Subroutines with List and Scalar Context • Modules • Getopt Module • “Here” Document • Unix/Linux Files

  3. Comparison Operator <=> and cmp • Useful with sort function • Return Value: • Returns -1 if left operand is less than right operand • Returns 0 if operands are equal • Returns 1 if left operand is greater than right operand. • For Numbers: <=> • example: if ( (1 <=> 3) == -1) { print “1 is less than 3\n”; }

  4. Comparison Operator <=> and cmp • For Strings: cmp • example: if ( (“apple” cmp “cherry”) == -1) { print “apple is less than cherry\n”; }

  5. Conditional Operator ?: • Returns different values depending condition. • Format: condition ? result1 : result2 • If condition is true, conditional operator returns result1 else if condition is false, conditional operator returns result2 • Example: $biggest = ($a > $b) ? $a : $b;

  6. String Manipulation Functions • uc(string) : Convert all letters in string to upper case. • lc(string) : Convert all letters in string to lower case. • string =~ s/reg_expr/replacement/ : Replace the first substring that match the regular expression with the replacement substring. • string =~ s/reg_expr/replacement/g : Replace the all substrings that match the regular expression with the replacement substring.

  7. 0001 #!/usr/bin/perl 0002 0003 ########### 0004 # File: ex0400 0005 ########### 0006 0007 print "original string: ", 0008 $ARGV[0],"\n"; 0009 0010 print "upper case : ", 0011 uc($ARGV[0]),"\n"; 0012 0013 print "lower case : ", 0014 lc($ARGV[0]),"\n"; 0015 0016 $ARGV[0] =~ s/hot/cold/; 0017 print "substitution 1 : ", 0018 $ARGV[0],"\n"; 0019 0020 $ARGV[0] =~ s/i/I/g; 0021 print "substitution 2 : ", 0022 $ARGV[0],"\n"; Lines 7-8: print original string Lines 10-11: print string converted to uppercase. Lines 13-14: print string converted to lower case. Line 16: substitute the FIRST occurrence of hot to cold. Note that the binding operator “=~” causes the string in ARGV[0] to be modified. Line 20: substitute ALL occurrences of “i” to “I”. Note that the binding operator “=~” causes the string in ARGV[0] to be modified. Example: >ex0400 "It is hot in August" original string: It is hot in August upper case : IT IS HOT IN AUGUST lower case : it is hot in august substitution 1 : It is cold in August substitution 2 : It Is cold In August Example ex0400

  8. Subroutines • Like many other languages, perl allows us define subroutines (similar to methods in Java). • In Perl, a subroutine can have an arbitrary number of arguments. • Arguments are NOT named. Rather, they are contained in an array named “@_” • Therefore, the first argument is in $_[0], the second in $_[1] and so on.

  9. 0001 #!/usr/bin/perl 0002 ##### 0003 # File ex0410 0004 #### 0005 0006 $area = areaOfRectangle(3, 4); 0007 print "Area = $area\n"; 0008 0009 sub areaOfRectangle { 0010 return($_[0] * $_[1]); 0011 } Line 6: invoke the function. Lines 9-11: function definition Line 10: calculate area of rectangle using first 2 arguments to function and return area. Example ex0410

  10. Subroutines • In our previous example, we saw how to use function parameters in “@_” : 0009 sub areaOfRectangle { 0010 return($_[0] * $_[1]); 0011 } • If we chose to, we can make our subroutine more readable by setting a list of variables with meaningful names to the values of “@_” (see ex0420): 0009 sub areaOfRectangle { 0010 ($length, $width) = @_; 0011 return($length * $width); 0012 }

  11. 0001 #!/usr/bin/perl 0002 ##### 0003 # File ex0420 0004 #### 0005 0006 $area = areaOfRectangle(3, 4); 0007 print "Area = $area\n"; 0008 0009 sub areaOfRectangle { 0010 ($length, $width) = @_; 0011 return($length * $width); 0012 } Line 6: invoke the function. Lines 9-12: function definition Line 10: copy the subroutine parameters to variables with readable names. Line 11: calculate area of rectangle using first 2 arguments to function and return area. example ex0420

  12. Name Scope • In general, variable names have global scope. • A variable defined in the calling function is visible in the called function.

  13. 0001 #!/usr/bin/perl 0002 ######### 0003 #File ex0430 0004 ######### 0005 0006 $apple = 4; 0007 mysub(); 0008 print '$apple = ', $apple, "\n"; 0009 0010 sub mysub { 0011 $apple = 5; 0012 } Line 6: set value of $apple to 4. Line 7: invoke “mysub()” Line 11: Change the value of $apple to 5 (the same variable defined on line 6) Line 8: Print value of $apple Example: >ex0430 $apple = 5 Example ex0430

  14. Name Scope • In example ex0430, we saw that changing the value of $apple in the subroutine affected the value of $apple in the calling function - this is because $apple has global scope. • Sometimes a routine may want its variables to have local scope. • This can be achieved by prefixing the key word “my” in front of variable when it is declared or first defined: my $apple = 5;

  15. 0001 #!/usr/bin/perl 0002 ########## 0003 # File ex0440 0004 ########## 0005 0006 $apple = 4; 0007 mysub(); 0008 print '$apple = ', $apple, "\n"; 0009 0010 sub mysub { 0011 my $apple = 5; 0012 } Line 6: set value of $apple to 4. Line 7: invoke “mysub()” Line 11: Declare $apple to be local (using “my” keyword). Line 11: Change the value of $apple to 5 ($apple is now local to “mysub”) Line 8: Print value of $apple Example: >ex0440 $apple = 4 Example ex0440

  16. Scalar and List Context • Scalar values: something that has a single value, like singular variable such as $myvar or a literal value such as 13. • List values: something that represents multiple values such as plural variables (like an array or hash) or a literal list such as (2, 9, 4, 170) • In Perl, operators and function can exhibit different behavior depending on whether the operand(s) are scalar or lists.

  17. Scalar and List Context • Perl provides a set of functions to operate on lists: • reverse LIST: reverses elements in list • foreach LIST: loops through elements of list • print LIST: prints a list to standard out • sort LIST: sorts a list of elements • keys HASH_LIST: returns a list of keys from a hash • join (delimiter, LIST): returns a string with a delimiter (like a space, comma or colon or whatever) between each element in the list. • map {sub function} LIST: returns a list by applying the function in {} to each element in the list

  18. 0001 #!/usr/bin/perl 0002 ########### 0003 # File ex0450 0004 ########### 0005 0006 #Examples of ASCII sort 0007 @words = (yellow, blue, green, orange); 0008 @sortedWords = sort @words; 0009 print @sortedWords, "\n"; 0010 print join(' ', @sortedWords), "\n"; 0011 print join(', ', @sortedWords), "\n"; 0012 print join(', ', reverse @sortedWords), "\n"; 0013 0014 @upperWords = map {uc} @sortedWords; 0015 print join(', ', @upperWords), "\n"; 0016 0017 #Examples of numerical sort 0018 0019 @numbers = (13, 4, 7, 3); 0020 @sortedNumbers = sort { $a <=> $b } @numbers; 0021 print join(', ', @sortedNumbers), "\n"; Line 8: ASCII sort of words Line 9: print elements of @sortedWords as 1 long string Line 10: print elements of @sortedWords, each element separated by a space. Line 11: print elements of @sortedWords, each element separated by a comma and space. Line 12: print elements of @sortedWords, in REVERSE order, each element separated by a comma and space. Line 14: use map function to convert each character of each element in @upperWords to upper case. Line 20: NUMERICAL sort Example: >ex0450 bluegreenorangeyellow blue green orange yellow blue, green, orange, yellow yellow, orange, green, blue BLUE, GREEN, ORANGE, YELLOW 3, 4, 7, 13 Example ex0450

  19. Subroutines with List and Scalar Context • We saw earlier that there are Perl functions that can return a scalar value if a scalar argument is used and a list value if a list argument is used. • We can design the same capability in our own functions. • In the next example, our function multiplies the argument by 2. • If a scalar argument is used, then a scalar value is returned. • If a list argument is used, then a list value is returned.

  20. 0001 #!/usr/bin/perl -w 0002 ############# 0003 # File ex0460 0004 ############ 0005 0006 print "scalar case: ", 0007 times2(4), "\n"; 0008 0009 print "list case: ", 0010 join(', ', times2 (3, 4, 5)), "\n"; 0011 0012 sub times2 { 0013 my @params = @_; 0014 for ($i=0; $i < scalar(@params); ++$i) { 0015 $params[$i] *= 2; 0016 } 0017 return wantarray ? @params : $params[0]; 0018 } Line 7: invoke times2() with a scalar argument. Line 10: invoke times2() with a list argument. Lines 14-16: multiple each element times 2. Line 17: The function “wantarray” is a Perl function to test if return value should be a scalar or list (depending on argument) Line 17: If result is to be a list, return @params, else return the scalar value $params[0] Example ex0460

  21. Perl Modules • Useful Perl code can be packaged into a Module. • A module is a package of reusable subroutines and variables. • A module is imported with the ‘use’ command. • The book provides a description of the standard Perl modules in chapter 32. • There are many useful modules defined at the Comprehensive Perl Archive Network (CPAN) • http://www.cpan.org/

  22. Getopt Module • The Getopt Module facilitates the processing of script options including: • options that take arguments. • options that do not take arguments. • The following statement loads in the Getopt Module: use Getopt::Std;

  23. Getopt Module • The following statement is an example of option definitions: getopts("hvd:f:"); • options h and v do not take arguments • options d and f do take arguments (followed by colons) • Results are returned by variables with names like: $opt_v, $opt_f • value is Boolean if option takes no argument ($opt_v) • value is argument if option does take argument ($opt_f)

  24. Here Document • The “Here” document is a construct that allows multiple lines of input to processed. • Very useful for generating HTML in a CGI script • Useful for outputting multiple lines of text, such as text used to describe the usage of a command. • There are 3 forms, Literal, Shell Expansion and Execution formats.

  25. Here Document Literal format (label in single quotes): print << ‘MY_LABEL’; line 1 line 2 . . . line n MY_LABEL • Lines 1 through n are processed literally, no shell expansion.

  26. Here Document • “Shell Expansion” format (label in double quotes): print << “MY_LABEL”; line 1 line 2 . . . line n MY_LABEL • Lines 1 through n are processed literally, except Shell variables are expanded and text in back quotes are executed.

  27. Here Document • Execution format (label in back quotes): print << `MY_LABEL`; line 1 line 2 . . . line n MY_LABEL • Lines 1 through n are executed.

  28. 0001 #!/usr/bin/perl -w 0002 ########### 0003 # File ex0470 0004 ########### 0005 0006 $time=`date`; 0007 0008 print << 'LITERAL_EXAMPLE'; 0009 Literal line 1 0010 Literal line 2 $time 0011 Literal line 3 0012 LITERAL_EXAMPLE 0013 0014 print << "SHELL_EXP_EXAMPLE"; 0015 Expansion line 1 0016 Expansion line 2 $time 0017 Expansion line 3 0018 SHELL_EXP_EXAMPLE 0019 0020 print << `EXECUTION_EXAMPLE`; 0021 date 0022 ls | wc -l 0023 EXECUTION_EXAMPLE Line 6: Save current time in $time Lines 8-12: Literal “Here” document Line 10, notice that $time will not be expanded. Lines 14-18: “Here” document with shell expansion. Line 16: $time will be expanded Lines 20-23: “Here document that is executed. Example: >ex0470 Literal line 1 Literal line 2 $time Literal line 3 Expansion line 1 Expansion line 2 Sun Sep 26 13:19:54 EDT 2004 Expansion line 3 Sun Sep 26 13:19:54 EDT 2004 31 Example ex0470

  29. Putting It All Together • Assume we want to write a script that can be used to evaluate file types. • We will use the Unix ‘file’ command (see manpage) • The file command evaluates the contents of a file and determines its type.

  30. Putting It All Together • Furthermore, assume we want these capabilities: • If the user specifies no options, the script will evaluate the files in the current directory • If the user specifies the –f option with an argument (a filename), then only that file will be evaluated. • If the user specifies a directory with the –d option, then all the files in the specified directory are evaluated. • Furthermore, we should force –f and –d to be mutually exclusive.

  31. 0001: #!/usr/bin/perl 0002: 0003: use Getopt::Std; 0004: 0005: sub usage { 0006: print STDERR << "EOF"; 0007: usage: $0 [-h] [-f file] [-d dir] 0008: -h : this (help) message 0009: -d dir : scan directory specified 0010: by dir. If this option is 0011: not used, then the 0012: current directory. 0013: -f file : file to be analyzed. If 0014: this option is not used, 0015: then all files in 0016: directory scanned. 0017: EOF 0018: } 0019: Line 3: import the Getopt module Lines 5-18: Subroutine to print usage statement. Line 6-17: “Here” Document. This is a single print statement Example ex0480

  32. 0020: 0021: getopts("hd:f:") or usage() and exit; 0022: 0023: usage() and exit if $opt_h; 0024: 0025: $dir = $opt_d; 0026: $file = $opt_f; 0027: 0028: usage() and exit if ($file && $dir); 0029: 0030: if ($file) { 0031: print `file $file`; 0032: } 0033: elsif ($dir) { 0034: print `file $dir/*`; 0035: } 0036: else { 0037: print `file *`; 0038: } Line 21: Define and collect options. If invalid option specified, generate usage message and exit. Line 23: if use specified help option (-h), generate usage message and exit. Lines 25-26: Map option variables to reader friendly variables. Line 28: If user specified both file and directory option, this is an error – so generate usage message and exit. Lines 30-38: Do the real work… Example ex0480

  33. Example ex0480 • The next several slides illustrate the use of options with ex0480 • Case 1 – no options: $ ex0480 ex0480: a /usr/bin/perl script text executable ex0480.num: ASCII English text. • Case 2 – user specifies file: $ex0480 -f /etc/passwd /etc/passwd: ASCII text

  34. Example ex0480 • Case 3 – User specifies directory: $ ex0480 -d /etc/rc.d /etc/rc.d/init.d: directory /etc/rc.d/rc: Bourne-Again shell script text executable /etc/rc.d/rc0.d: directory /etc/rc.d/rc1.d: directory /etc/rc.d/rc2.d: directory /etc/rc.d/rc3.d: directory /etc/rc.d/rc4.d: directory /etc/rc.d/rc5.d: directory /etc/rc.d/rc6.d: directory /etc/rc.d/rc.local: Bourne shell script text executable /etc/rc.d/rc.sysinit: Bourne-Again shell script text executable

  35. Example ex0480 • Remaining Cases - User either: • specifies help option (-h) • Specifies an illegal option (e.g. –z) • specifies both the directory and file options: usage: ex0480 [-h] [-f file] [-d dir] -h : this (help) message -d dir : scan directory specified by dir. If this option is not used, then the current directory. -f file : file to be analyzed. If this option is not used, then all files in directory scanned.

  36. Unix/Linux Files • Directories • User and Group Names • File Related Commands • chmod • chgrp • ln • ln –s

  37. Directories • root (‘/’): • All Unix file systems are represented by a directory tree with root (‘/’) at the top of the tree. • This is why the path returned by ‘pwd’ command always begins with ‘/’: $pwd /export/home/vaughang • home directory • defined in /etc/passwd • Executing the ‘cd’ command without arguments always returns you to your home directory.

  38. User and Group IDs Group Name user name • ls –l drwx------ 3 vaughang faculty 512 Jan 3 21:07 backup drwxr-xr-x 2 vaughang faculty 512 Jan 3 20:13 bin lrwxrwxrwx 1 vaughang faculty 19 Jan 6 22:06 itec400 -> public_html/itec400 -rw-r--r-- 1 vaughang faculty 42 Jan 23 20:21 junk • User Names • defined in /etc/passwd • In the listing above, all files are owned by user gvaughan • Notice that we view directories as a type of file.

  39. User and Group IDs drwxr-x--- 2 vaughang faculty 512 Jan 3 20:13 bin • Group Name • A group is a collection of one more User Names • Allows for assigning file permissions for entire groups of users. • In the file above, the group with privileges to this file is ‘faculty’. • Groups are defined in /etc/group. • A User Name may be a member of more than one group.

  40. File Modes • File Modes determine who has permission to do what to a given file. • File modes can be viewed using ‘ls –l’ drwx------ 3 vaughang faculty 512 Jan 3 21:07 backup drwxr-xr-x 2 vaughang faculty 512 Jan 3 20:13 bin lrwxrwxrwx 1 vaughang faculty 19 Jan 6 22:06 itec400 -> public_html/itec400 -rw-r--r-- 1 vaughang faculty 42 Jan 23 20:21 junk • The first character in column 1 is file type. • The remainder of column 1 is the file mode. File Type File Mode

  41. File Modes drwx------ 3 vaughang faculty 512 Jan 3 21:07 backup -rwxr-x--- 2 vaughang faculty 512 Jan 3 20:13 junk • Permissions are granted to three categories of users: owner (User Name), group (Group Name), and everyone else (other). • Each category is represented by 3 bits which indicate read-write-execute permissions (rwx). • In the file ‘junk’ above, vaughang has rwx permissions, faculty has rx permissions and everyone else has no permissions.

  42. File Modes • File permissions have different effects on directories and files • Useful File Modes: • data files: 644 • programs: 755 • directories: 755 • We’ll soon see how umask can help

  43. chmod (relative mode) • chmod – used to change the file mode on a file or directory. • chmod can be used to change the file mode relative to category of user and/or current mode • Example 1: -r--r----- 2 vaughang faculty 512 Jan 3 20:13 junk • What are the current permissions? • Example 2: • We can give everybody execute permissions: chmod +x junk; ls –l junk -r-xr-x--x 2 vaughang faculty 512 Jan 3 20:13 junk

  44. chmod (relative mode) • Example 3: • We can give the owner (User Name) write permissions: chmod u+w junk; ls –l junk -rwxr-x--x 2 vaughang faculty 512 Jan 3 20:13 junk • Book has more examples of using chmod with relative modes.

  45. chmod (absolute mode) • chmod can also be used to specify an absolute file mode. • The 9 bits used to specify mode can be viewed as 3 octal digits, the first digit applying mode for owner, the second digit for group, and the third digit for others. • For example, assume we want user to have all permissions, group to have read and others have no permissions on myfile, we would type: chmod 740 myfile

  46. 0001 vaughang>ls -n 0002 total 2 0003 dr--r--r-- 2 512 Feb 4 19:05 dir1 0004 ---------- 1 0 Feb 4 17:20 file1 0005 vaughang>cat file1 0006 cat: cannot open file1 0007 vaughang>chmod 444 file1 0008 0009 0010 0011 vaughang>ls / > file1 0012 sh: file1: cannot create 0013 vaughang>chmod 644 file1 0014 vaughang>ls / > file1 0015 vaughang>ls -n 0016 total 4 0017 dr--r--r-- 2 512 Feb 4 19:05 dir1 0018 -rw-r--r-- 1 212 Feb 4 19:12 file1 Line 4: nobody has any permissions for file1 Line 5: Can’t write to file1 Line 7: Give everybody has read permissions for file1 Line 11: Try writing to file1 Line 13: Give owner write permissions. Line 14: Now we can write to file1 Example chmod

  47. 0019 vaughang>ls dir1 0020 trash 0021 vaughang>cd dir1 0022 sh: dir1: permission denied 0023 vaughang>chmod 544 dir1 0024 vaughang>cd dir1 0025 vaughang/dir1>ls 0026 trash 0027 vaughang/dir1>rm trash 0028 rm: trash not removed: Permission denied 0029 vaughang/dir1>chmod 755 . 0030 vaughang/dir1>ls -n . 0031 drwxr-xr-x 2 512 Feb 4 19:05 . 0032 vaughang/dir1>rm trash 0033 vaughang/dir1>cd .. 0034 vaughang>ls -n 0035 total 4 0036 drwxr-xr-x 2 512 Feb 4 19:15 dir1 0037 -rw-r--r-- 1 212 Feb 4 19:12 file1 Line 19: list dir1 (why does this work?) Line 20: try to cd to dir1 (why does this fail?) Line23: Give owner execute permission to dir1 Line 24: Now we can cd to dir1 Line 27: Try to rmove file trash (why does this fail?) Line 29: Give owner write permission to current directory. Line 32: Now we can remove file trash Example chmod

  48. chgrp • chgrp – allows the group to be changed for a file: • root: can change any group for any file. • everyone else: can only change to a group that the owner is already a member of.

  49. umask • umask • used to display or change default file mode • mask value is 1’s complement of file mode (i.e. a mask value of 022 will result in a default file mode of 644 for a regular file. • When using bash in Linux, and when using Solaris, the system wide umask is defined in: /etc/profile.

  50. 0001 vaughang>umask 0002 022 0003 vaughang>touch file1 0004 vaughang>mkdir dir1 0005 vaughang>ls -n 0006 total 2 0007 drwxr-xr-x 2 512 Feb 4 17:20 dir1 0008 -rw-r--r-- 1 0 Feb 4 17:20 file1 0009 vaughang>umask 027 0010 vaughang>umask 0011 027 0012 vaughang>touch file2 0013 vaughang>mkdir dir2 0014 vaughang>ls -n 0015 total 4 0016 drwxr-xr-x 2 512 Feb 4 17:20 dir1 0017 drwxr-x--- 2 512 Feb 4 17:22 dir2 0018 -rw-r--r-- 1 0 Feb 4 17:20 file1 0019 -rw-r----- 1 0 Feb 4 17:21 file2 Line 1: check current mask value Line 3,4: create file junk1 and dir1 Line 5: check file mode of junk1 and dir1 Line 9: change value of mask Line 12,13: create file junk2 and dir2 line 13: check value of file2 Line 14: display current value of mask For files, notice that mask is subtracted from 666 For Directories, notice that mask is subtracted from 777 Example of umask

More Related