itec 400 more perl and unix/linux files

62
1 ITEC 400 More Perl and Unix/Linux Files George Vaughan Franklin University

Upload: raina

Post on 05-Jan-2016

48 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ITEC 400 More Perl and Unix/Linux Files

1

ITEC 400More Perl and Unix/Linux Files

George VaughanFranklin University

Page 2: ITEC 400 More Perl and Unix/Linux Files

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

Page 3: ITEC 400 More Perl and 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”;

}

Page 4: ITEC 400 More Perl and Unix/Linux Files

4

Comparison Operator <=> and cmp

• For Strings: cmp– example:

if ( (“apple” cmp “cherry”) == -1) {print “apple is less than cherry\n”;

}

Page 5: ITEC 400 More Perl and Unix/Linux Files

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;

Page 6: ITEC 400 More Perl and Unix/Linux Files

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.

Page 7: ITEC 400 More Perl and Unix/Linux Files

7

Example ex04000001 #!/usr/bin/perl 00020003 ###########0004 # File: ex04000005 ###########00060007 print "original string: ",0008 $ARGV[0],"\n";00090010 print "upper case : ",0011 uc($ARGV[0]),"\n";00120013 print "lower case : ",0014 lc($ARGV[0]),"\n";00150016 $ARGV[0] =~ s/hot/cold/;0017 print "substitution 1 : ",0018 $ARGV[0],"\n";00190020 $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 Augustupper case : IT IS HOT IN AUGUSTlower case : it is hot in augustsubstitution 1 : It is cold in Augustsubstitution 2 : It Is cold In August

Page 8: ITEC 400 More Perl and Unix/Linux Files

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.

Page 9: ITEC 400 More Perl and Unix/Linux Files

9

Example ex04100001 #!/usr/bin/perl 0002 #####0003 # File ex04100004 ####00050006 $area = areaOfRectangle(3, 4);0007 print "Area = $area\n";00080009 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.

Page 10: ITEC 400 More Perl and Unix/Linux Files

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 }

Page 11: ITEC 400 More Perl and Unix/Linux Files

11

example ex04200001 #!/usr/bin/perl 0002 #####0003 # File ex04200004 ####00050006 $area = areaOfRectangle(3, 4);0007 print "Area = $area\n";00080009 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.

Page 12: ITEC 400 More Perl and Unix/Linux Files

12

Name Scope

• In general, variable names have global scope.

• A variable defined in the calling function is visible in the called function.

Page 13: ITEC 400 More Perl and Unix/Linux Files

13

Example ex04300001 #!/usr/bin/perl 0002 #########0003 #File ex04300004 #########00050006 $apple = 4;0007 mysub();0008 print '$apple = ', $apple, "\n";00090010 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

Page 14: ITEC 400 More Perl and Unix/Linux Files

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;

Page 15: ITEC 400 More Perl and Unix/Linux Files

15

Example ex04400001 #!/usr/bin/perl 0002 ##########0003 # File ex04400004 ##########00050006 $apple = 4;0007 mysub();0008 print '$apple = ', $apple, "\n";00090010 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

Page 16: ITEC 400 More Perl and Unix/Linux Files

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.

Page 17: ITEC 400 More Perl and Unix/Linux Files

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

Page 18: ITEC 400 More Perl and Unix/Linux Files

18

Example ex04500001 #!/usr/bin/perl 0002 ###########0003 # File ex04500004 ###########00050006 #Examples of ASCII sort0007 @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";00130014 @upperWords = map {uc} @sortedWords;0015 print join(', ', @upperWords), "\n";00160017 #Examples of numerical sort00180019 @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:>ex0450bluegreenorangeyellowblue green orange yellowblue, green, orange, yellowyellow, orange, green, blueBLUE, GREEN, ORANGE, YELLOW3, 4, 7, 13

Page 19: ITEC 400 More Perl and Unix/Linux Files

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.

Page 20: ITEC 400 More Perl and Unix/Linux Files

20

Example ex04600001 #!/usr/bin/perl -w0002 #############0003 # File ex04600004 ############00050006 print "scalar case: ",0007 times2(4), "\n";00080009 print "list case: ",0010 join(', ', times2 (3, 4, 5)), "\n";00110012 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]

Page 21: ITEC 400 More Perl and Unix/Linux Files

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/

Page 22: ITEC 400 More Perl and Unix/Linux Files

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;

Page 23: ITEC 400 More Perl and Unix/Linux Files

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)

Page 24: ITEC 400 More Perl and Unix/Linux Files

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.

Page 25: ITEC 400 More Perl and Unix/Linux Files

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.

Page 26: ITEC 400 More Perl and Unix/Linux Files

26

Here Document

• “Shell Expansion” format (label in double quotes):

print << “MY_LABEL”;line 1line 2

.

.

.line nMY_LABEL

• Lines 1 through n are processed literally, except Shell variables are expanded and text in back quotes are executed.

Page 27: ITEC 400 More Perl and Unix/Linux Files

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.

Page 28: ITEC 400 More Perl and Unix/Linux Files

28

Example ex04700001 #!/usr/bin/perl -w0002 ###########0003 # File ex04700004 ###########00050006 $time=`date`;00070008 print << 'LITERAL_EXAMPLE';0009 Literal line 10010 Literal line 2 $time0011 Literal line 30012 LITERAL_EXAMPLE00130014 print << "SHELL_EXP_EXAMPLE";0015 Expansion line 10016 Expansion line 2 $time0017 Expansion line 30018 SHELL_EXP_EXAMPLE00190020 print << `EXECUTION_EXAMPLE`;0021 date0022 ls | wc -l0023 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:>ex0470Literal line 1Literal line 2 $time Literal line 3Expansion line 1Expansion line 2 Sun Sep 26 13:19:54 EDT 2004

Expansion line 3Sun Sep 26 13:19:54 EDT 2004 31

Page 29: ITEC 400 More Perl and Unix/Linux Files

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.

Page 30: ITEC 400 More Perl and Unix/Linux Files

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.

Page 31: ITEC 400 More Perl and Unix/Linux Files

31

Example ex04800001: #!/usr/bin/perl0002:0003: use Getopt::Std;0004:0005: sub usage {0006: print STDERR << "EOF";0007: usage: $0 [-h] [-f file] [-d dir]0008: -h : this (help) message0009: -d dir : scan directory specified0010: by dir. If this option is0011: not used, then the0012: current directory.0013: -f file : file to be analyzed. If0014: this option is not used,0015: then all files in0016: directory scanned.0017: EOF0018: }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

Page 32: ITEC 400 More Perl and Unix/Linux Files

32

Example ex04800020: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…

Page 33: ITEC 400 More Perl and Unix/Linux Files

33

Example ex0480

• The next several slides illustrate the use of options with ex0480

• Case 1 – no options:$ ex0480ex0480: a /usr/bin/perl script text executableex0480.num: ASCII English text.

• Case 2 – user specifies file:$ex0480 -f /etc/passwd/etc/passwd: ASCII text

Page 34: ITEC 400 More Perl and Unix/Linux Files

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

Page 35: ITEC 400 More Perl and Unix/Linux Files

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.

Page 36: ITEC 400 More Perl and Unix/Linux Files

36

Unix/Linux Files

• Directories

• User and Group Names

• File Related Commands– chmod– chgrp– ln– ln –s

Page 37: ITEC 400 More Perl and Unix/Linux Files

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.

Page 38: ITEC 400 More Perl and Unix/Linux Files

38

User and Group IDs

• ls –ldrwx------ 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.

user name Group Name

Page 39: ITEC 400 More Perl and Unix/Linux Files

39

User and Group IDsdrwxr-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.

Page 40: ITEC 400 More Perl and Unix/Linux Files

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 backupdrwxr-xr-x 2 vaughang faculty 512 Jan 3 20:13 binlrwxrwxrwx 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

Page 41: ITEC 400 More Perl and Unix/Linux Files

41

File Modesdrwx------ 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.

Page 42: ITEC 400 More Perl and Unix/Linux Files

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

Mode File Directory

r

(read)

view contents

(cat)

search – read directory

(ls)

w

(write)

change contents

of existing file (vi oldfile)

create and delete files

(vi newfile, rm)

x (execute)

script

executable

(my.sh, a.out)

make it current dir (cd)

Page 43: ITEC 400 More Perl and Unix/Linux Files

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

Page 44: ITEC 400 More Perl and Unix/Linux Files

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.

Page 45: ITEC 400 More Perl and Unix/Linux Files

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

Owner Group Others

r w x r w x r w x

1 1 1 1 0 0 0 0 0

7 4 0

Page 46: ITEC 400 More Perl and Unix/Linux Files

46

Example chmod0001 vaughang>ls -n0002 total 20003 dr--r--r-- 2 512 Feb 4 19:05 dir10004 ---------- 1 0 Feb 4 17:20 file10005 vaughang>cat file10006 cat: cannot open file10007 vaughang>chmod 444 file10008000900100011 vaughang>ls / > file10012 sh: file1: cannot create0013 vaughang>chmod 644 file10014 vaughang>ls / > file10015 vaughang>ls -n0016 total 40017 dr--r--r-- 2 512 Feb 4 19:05 dir10018 -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

Page 47: ITEC 400 More Perl and Unix/Linux Files

47

Example chmod0019 vaughang>ls dir10020 trash0021 vaughang>cd dir10022 sh: dir1: permission denied0023 vaughang>chmod 544 dir10024 vaughang>cd dir10025 vaughang/dir1>ls0026 trash0027 vaughang/dir1>rm trash0028 rm: trash not removed: Permission

denied0029 vaughang/dir1>chmod 755 .0030 vaughang/dir1>ls -n .0031 drwxr-xr-x 2 512 Feb 4 19:05 .0032 vaughang/dir1>rm trash0033 vaughang/dir1>cd ..0034 vaughang>ls -n0035 total 40036 drwxr-xr-x 2 512 Feb 4 19:15 dir10037 -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

Page 48: ITEC 400 More Perl and Unix/Linux Files

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.

Page 49: ITEC 400 More Perl and Unix/Linux Files

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.

Page 50: ITEC 400 More Perl and Unix/Linux Files

50

Example of umask0001 vaughang>umask0002 0220003 vaughang>touch file10004 vaughang>mkdir dir10005 vaughang>ls -n0006 total 20007 drwxr-xr-x 2 512 Feb 4 17:20 dir10008 -rw-r--r-- 1 0 Feb 4 17:20 file10009 vaughang>umask 0270010 vaughang>umask0011 0270012 vaughang>touch file20013 vaughang>mkdir dir20014 vaughang>ls -n0015 total 40016 drwxr-xr-x 2 512 Feb 4 17:20 dir10017 drwxr-x--- 2 512 Feb 4 17:22 dir20018 -rw-r--r-- 1 0 Feb 4 17:20 file10019 -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

Page 51: ITEC 400 More Perl and Unix/Linux Files

51

Appendix

• The following slides illustrate the use of Perl in a large, industrial strength program.

Page 52: ITEC 400 More Perl and Unix/Linux Files

52

Process Manager (in Perl)

• The next several slides illustrate the use of using Perl for writing a realistic script for System Administration.

• Write a PERL script that will: – match process names given a basic regular

expression (BRE). – The matched process will be presented to the user. – If more than one process matches the BRE then a

numbered list of all matching processes is presented to the user.

– The user then either selects a single process to kill or exits without killing the process.

Page 53: ITEC 400 More Perl and Unix/Linux Files

53

Process Manager (in Perl)• For example, let’s say we want to kill a process that contains “sh”:$ ex0020_4.pl sh P# - USER PID STIME COMMAND 1 - root 9 Jan25 bdflush 2 - root 1795 Jan25 sshd 3 - root 2196 Jan25 ssh-agent 4 - root 2247 Jan25 bash 5 - root 2287 Jan25 sshd 6 - gvaughan 2289 Jan25 sshd 7 - gvaughan 2290 Jan25 bash 8 - root 2330 Jan25 sshd 9 - gvaughan 2332 Jan25 sshd 10 - gvaughan 2333 Jan25 bash 11 - root 2373 Jan25 sshd 12 - gvaughan 2375 Jan25 sshd 13 - gvaughan 2376 Jan25 bash 14 - root 2420 Jan25 sshd 15 - gvaughan 2422 Jan25 sshd 16 - gvaughan 2423 Jan25 bashEnter P# of process to kill or q to quit:

Page 54: ITEC 400 More Perl and Unix/Linux Files

54

Process Manager (in Perl)• Or, let us assume we want to kill a process owned by “gvaughan”:$ ex0020_4.pl -u gvaughan sh P# - USER PID STIME COMMAND 1 - gvaughan 2289 Jan25 sshd 2 - gvaughan 2290 Jan25 bash 3 - gvaughan 2332 Jan25 sshd 4 - gvaughan 2333 Jan25 bash 5 - gvaughan 2375 Jan25 sshd 6 - gvaughan 2376 Jan25 bash 7 - gvaughan 2422 Jan25 sshd 8 - gvaughan 2423 Jan25 bashEnter P# of process to kill or q to quit:

Page 55: ITEC 400 More Perl and Unix/Linux Files

55

Process Manager (in Perl)

• Or, maybe we want to kill a process owned by gvaughan whose name matches “sleep” with signal KILL:

$ ex0020_4.pl -s KILL -u gvaughan sleep P# - USER PID STIME COMMAND 1 - gvaughan 17272 00:25 sleepEnter P# of process to kill or q to quit: 1kill -KILL 17272

• Or, we want to kill a process owned by gvaughan whose name matches “sleep” and without query:

$ ex0020_4.pl -s KILL -u gvaughan -n sleepkilling 17277 with signal KILL

Page 56: ITEC 400 More Perl and Unix/Linux Files

56

Process Manager (in Perl)

• What follows is the annotated source

• The script is a little long – 88 lines long. Wouldn’t be surprised if a Perl guru could do it with a “one-liner”

• Provides a good example of:– Option handling– Pattern matching

Page 57: ITEC 400 More Perl and Unix/Linux Files

57

Example ex04900001: #!/usr/bin/perl0002:0003: sub usage {0004: print STDERR << "EOF";0005: usage: $0 -h [-n x] [-d y] process_name0006: -h : this (help) message0007: -n : disable confirmation0008: -u user_name : only return those

processes owned by user_name0009: -s signal : send the given signal to the

matching processes0010: process_name : a BRE used to match

currently active processes0011: EOF0012: }0013:0014: use Getopt::Std;0015: getopts("hnu:s:") or usage() and exit;0016:0017: usage() and exit if $opt_h;0018:0019: $no_confirmation = $opt_n if $opt_n;

• Lines 3-12: usage subroutine with “here” document.

• Line 14: Include Getopts module• Line 15: specify and collect options• Line 19: Map to readable variable

Page 58: ITEC 400 More Perl and Unix/Linux Files

58

Example ex04900020:0021: $user_name_arg = $opt_u if $opt_u;0022:0023: @legal_signals = split(" ", `kill -l`);0024: $num_of_signals =

scalar(@legal_signals);0025:0026: if ($opt_s) {0027: for ($i=0; $i < $num_of_signals; ++$i) {0028: if ($opt_s eq $legal_signals[$i]) {0029: $signal = $opt_s;0030: last;0031: }0032: }0033: }0034: else {0035: $signal = "TERM";0036: }0037:0038: $signal or die "$opt_s is not a legal signal.

See man page for kill";

• Line 21: Map to readable variable• Lines 23-24: Construct list of legal

signals from “kill” command.• Lines 26-33:If user specified a signal,

make sure it is legal. • Lines 34-36: If user did not specify a

signal, default to TERM• Line 38: If specified signal is illegal,

terminate.

Page 59: ITEC 400 More Perl and Unix/Linux Files

59

Example ex04900039:0040: $bre = $ARGV[$#ARGV];0041:0042: open (PROCESS_LIST, "ps -e -o

ruser,pid,stime,comm |")0043: or die "cannot execute ps command";0044:0045: if (! $no_confirmation ) {0046: printf("%4s - %-15s %6s %8s %s\n",

"P#", "USER",0047: "PID", "STIME", "COMMAND");0048: }0049:

• Line 40: Fetch “Basic Regular Expression” from command line (last value).

• Line 42-43: Open input pipe from ps command.

• Lines 45-48: If user did not choose “no_confirmation” then user wants confirmation. So, generate header for process list.

Page 60: ITEC 400 More Perl and Unix/Linux Files

60

Example ex04900050: for ($i=1; $line = <PROCESS_LIST>; ) {0051: if ($line =~ /^ RUSER/) {0052: next;0053: }0054: @fields = split(" ", $line);0055: $user_name = $fields[0];0056: $pid = $fields[1];0057: $stime = $fields[2];0058: $command = $fields[3];0059: #print "command=*$command*,

bre=*$bre*\n";0060: if (! $command) {0061: next;0062: }0063: if (! ($command =~ /$bre/)) {0064: next;0065: }0066: if ($user_name_arg && $user_name ne

$user_name_arg) {0067: next;0068: }

• Line 50-70: Loop through all processes in process list.

• Lines 54-58: Fetch all meaningful fields from ps output.

• Line 59: Print statement for debugging

• Line 60: If command field is blank, skip to next process

• Line 63: If command doesn’t match BRE, skip to next process.

• Line 66: If user name is specified and process is not owned by user, skip to next process.

Page 61: ITEC 400 More Perl and Unix/Linux Files

61

Example ex04900069: if ( $no_confirmation ) {0070: print "killing $pid with signal $signal\n";0071: system("kill -$signal $pid");0072: }0073: else {0074: $process_list[$i] = $pid;0075: printf("%4d - %-15s %6d %8s %s\n", $i++,

$user_name,0076: $pid, $stime, $command);0077: }0078: }0079:0080: if ( (! $no_confirmation) && ($i > 1)) {0081: print "Enter P# of process to kill or q to quit: ";0082: $response = <STDIN>;0083: chomp($response);0084: if ($response =~ /^\d+$/) {0085: print "kill -$signal $process_list[$response]\

n";0086: system("kill -$signal

$process_list[$response]");0087: }0088: }

• Line 69: If user doesn’t want confirmation, don’t generate process list, just send a signal to process.

• Line 73: Otherwise, print process info on process list.

• Line 80-82: Let user pick process number from list.

• 84-87: If user entered a valid process number, then kill the associated process.61

Page 62: ITEC 400 More Perl and Unix/Linux Files

62

References

• “Programming Perl”, Larry Wall, Tom Christiansen and Jon Orwant, 2000

• “Essential System Administration”, by Aeleen Frisch, 2002