44
Module 5 Unix and Shell Programming MODULE : 5 (Continuation) (Cont….) The find command with illustrative example. Structure of a perl script. Running a perl script. Variables and operators. String handling functions. Default variables - $_ and $. – representing the current line and current line number. The range operator. Chop() and chomp() functions. Lists and arrays. The @- variable. The splice operator, push(), pop(), split() and join(). File handles and handling file – using open(), close() and die () functions.. Associative arrays – keys and value functions. Overview of decision making loop control structures – the foreach. Regular expressions – simple and multiple search patterns. The match and substitute operators. Defining and using subroutines. Topics from chapter 9 and 19 of text book 1. Topics from chapter 11 of reference book 1 find : locating files It recursively examines a directory tree to look for files matching some criteria, and then takes some action on the selected files. Syntax: find path_list selecton_criteria action First, it Recursively examines all files in the directories specified in path_list It then matches each file for one or more selection-criteria Finally, It takes some action on those selected files The path_list comprises one or more directories separated by whitespace. There can also be a host of selection_criteria that you can use to match a file, and multiple actions to dispose of the file. Example: find command to locate all files named a.out $find / -name a.out -print Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 1

MODULE : 5 (Continuation) - WordPress.com · Structure of a perl script. ... the pattern should be quoted to prevent the shell from looking at it: ... Shell Programming #!/usr/bin/perl

Embed Size (px)

Citation preview

Module 5 Unix and Shell Programming

MODULE : 5 (Continuation)

(Cont….) The find command with illustrative example.

Structure of a perl script. Running a perl script. Variables and operators. String handling

functions. Default variables - $_ and $. – representing the current line and current line number.

The range operator. Chop() and chomp() functions. Lists and arrays. The @- variable. The splice

operator, push(), pop(), split() and join(). File handles and handling file – using open(), close()

and die () functions.. Associative arrays – keys and value functions. Overview of decision

making loop control structures – the foreach. Regular expressions – simple and multiple search

patterns. The match and substitute operators. Defining and using subroutines.

Topics from chapter 9 and 19 of text book 1. Topics from chapter 11 of reference book 1

find : locating files

It recursively examines a directory tree to look for files matching some criteria, and then

takes some action on the selected files.

Syntax: find path_list selecton_criteria action

First, it Recursively examines all files in the directories specified in path_list

It then matches each file for one or more selection-criteria

Finally, It takes some action on those selected files

The path_list comprises one or more directories separated by whitespace. There can also be a

host of selection_criteria that you can use to match a file, and multiple actions to dispose of the

file.

Example:

find command to locate all files named a.out

$find / -name a.out -print

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 1

Module 5 Unix and Shell Programming

/home/kumar/scripts/a.out

/home/user2/script/cprogram/a.out

/home/user/a.out

The path list / indicates that the search should starts from root directory. Each file in the

list is then matched against the selection criteria, which always consists of an expression

in the form –operator argument.

If the expression matches the file the file is selected.

The third section specifies the action to be taken on the files; a simple display on the

terminal.

Match the group of filenames with a wild-card pattern, the pattern should be quoted to

prevent the shell from looking at it:

find . -name “*.c” –print // All files with extension .c

find . -name '[A-Z]*' –print // All files begin with an upper case letter

Selection Criteria

Locating a File by Inode Number (-inum) : Based on the specified inode number files are searched.

find / -inum 13975 -print 2> /dev/null

find: cannot read dir /usr/lost+found: perm

/usr/bin/gzip

/usr/bin/gunzip

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 2

Module 5 Unix and Shell Programming

/usr/bin/gzcat

File type and Permission (-type and -perm) : The –type option indicates which type of file.

The letter f,d or l selects files of the ordinary, directory and symbolic link type.

find . -type d -print 2>/dev/null

. /shows current and hidden directories also

./.ds

./c_programs

./unix_lab/pgms

./shelscripts

./.ssh

The –perm option specifies the permissions to match. –perm 666 selects files having read and

write permissions for all categories of users.

find $HOME -perm 777 -type d -print

find $HOME/ds -perm 775 -type d -print 2> /dev/null

Finding Unused File (-mtime and -atime) : Files can be searched based on the modified and

accessed time.

Command to list all that have been modified since less last 2 days:

find . -mtime -2 -print

Command to select from the /home directory all files that have not been accessed for more than a

year, a positive value has to be used with –atime.

find /home -atime +365 -print // files that are not been accesses for more than a year

The find operators (!, -o , -a)

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 3

Module 5 Unix and Shell Programming

There are 3 operators that are commonly used with the find.

! operator is used before an option to negate its meaning.

find . ! -name “*.c” –print // Finds all files excluding .c file.

-o operator which represent an OR condition.

find /home \( -name “*.sh “ -o -name “*.pl” \) -print // here it searches files with .sh

or .pl extensions.

The –a operator represents the AND condition, and is implied by default whenever

two selection criteria are placed together.

Command to select all the directories that provide all access rights to everyone.

find $HOME –perm 777 –a –type –d -print

Table list the different Selection Criteria for find command.

Selection Criteria Selects File

-inum n Having inode number n

-type x If of type x, f ordinary file, d directory , l symbolic link

-type f If an ordinary file

-perm nnn If octal permissions match nnn completely

-links n If having n links

-user usname If owned by usname

-group gname If owned by group gname

-size +x If size is greater than x blocks

-mtime -x If modified in less than x days

-newer flname If modified after flname

-mmin -x If modified in less than x minutes

-atime +x If accesses in more than x days

-name flname , flname

-iname flname As above, but match is case insensitive

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 4

Module 5 Unix and Shell Programming

-prune Don't descend directory

-mount But don’t look in other file system

Action Significance

-print Prints selected file on standard output

-ls Executes ls –lids command on selected files

-exec cmd Executes UNIX command cmd followed by {} \;

-ok Same as –exec except that this option asks for the yes or no

confirmation of the user.

Options Available in the Action Component

Display ( -print) : Display pathnames of matching files on the standard output.

Displaying the listing (-ls) : To view the listing for the selected files

find . -type f -mtime +2 -mtime -5 -ls

4521 1 –rw-r—r-- 11user user 234 Mar 23 11:12 ./c_pgms/prime.c

Above command display a special listing of those regular files that are modified in more than

two days and less than five days.

Taking Action on Selected Files (-exec and -ok)

The –exec takes the command to execute as its argument, followed by {} and finally /;

find $HOME -type f -atime +365 -exec rm {} \; //Uses rm command to remove all ordinary

files unaccessed for more than a year.

find $HOME -type f -atime +365 -ok mv {} $HOME/ds \; // mv command turns interactive

with –i but only if the destination file exists. –ok seeks confirmation for every selected file to

be moved to the $HOME/ds directory irrespective of whether the file exist at the destination or

not. A y deletes a file.

Few more sample examples

find . -mtime 1 (- less than , + more than)

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 5

Module 5 Unix and Shell Programming

find . -type f -mmin 15 -prune

find . -mtime +10 -size +50000 -exec ls -l {} \;

find . -type f -name "*.php"

find / -type d -perm 777 -print -exec chmod 755 {} \;

find . -type f -name "*.mp3" -exec rm -f {} \;

find /tmp -type f -name ".*" // hidden file

find . -size 10M

find -newer "sum.java" // display files created after sum

find / \(-user krish –o –user ram \) –print –exec rm {}\; // Searches recursively and performs

remove action for the files owned by either krish or ram.

find . ! –user krish –print // all the files other than the user krish will be displayed

find/ ! -name "*.C"-Print

****************University Questions*************************

Use find command to locate the following:

i) all symbolic link files with the permission 666 in the parent directory.

ii) all files with number of links equal to 5 in the current working directory.

iii) display the content of the file name example.c in the home directory.

i) find -type l -perm 666 -print

ii) find -links 5 -print

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 6

Module 5 Unix and Shell Programming

iii) find $HOME -name “example.c” -exec cat{}\;

1. Explain   the   find  command with  different  selection  criteria  and

actions.

2. Illustrate the find command with example 

3. Write a short note on find command

4. Use find to locate all files named a.out and all C source files in

your home directory tree, and remove them interactively5. Use find command to locate from your home directory :

i) All files with the extension .htmlii) All flies having inode number 9076 iii) All directories having permissions 666

iv) All files not accessed for more than a year

v) All but the C program files

vi) All files named a.out and all C source files and remove

them interactively

6. Use find to locate from your home directory tree all i) files with

extension .html or .HTML ii) Files having the inode number 9076

iii) Directories having permission 666 iv) Files modified yesterday 

7. Use find to

i) Move all the files modified within the last 24 hours to the

posix directory under your parent directory 

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 7

Module 5 Unix and Shell Programming

ii) locate all files named a.out or core in your home directory

trr and remove them interactively 

iii) locate the file login .sql in the /oracle directory tree and

copy it to own directory 

iv) change   all   directory   permission   to   755   and   all   file

permission to 644 in your home directory tree 

(chapter 19) : perl-THE MASTER MANIPULATOR

Perl preliminaries

® Perl: Perl stands for Practical Extraction and Reporting Language. The language was

developed by Larry Wall. Perl is a popular programming language because of its

powerful pattern matching capabilities, rich library of functions for arrays, lists and

file handling. Perl is also a popular choice for developing scripts on the www (World

Wide Web).® Perl is a simple yet useful programming language that provides the convenience of shell

scripts and the power and flexibility of high-level programming languages. Perl programs

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 8

Module 5 Unix and Shell Programming

are interpreted and executed directly, just as shell scripts are; however, they also contain

control structures and operators similar to those found in the C programming language.® A perl program runs in a special interpretive model; the entire script is compiled

internally in memory before being executed. Script errors, if any, are generated before

execution.® NOTE: Perl is a freeware and can be obtained from http://www.perl.com or

http://www.activestate.com (Perl interpreter for Windows).

STRUCTURE OF A PERL SCRIPT

The very first line that starts with two special symbols (# and !) is a interpreter line.

This line gives the information about the path where the perl program is available. All comments in perl scripts are specified by # character. However the rule doesn’t

apply to the first line. The variables are prefixed with $ symbols are called scalars. $ symbol is prefixed with

definition as well as in evaluation. <STDIN> is a file handle representing standard input. Messages as well as computed results, if any are output using print(). Perl permits the use

of all logical operators, relational operators, the conditional operators and others. Each line of the code, except the comment lines, end with semicolon (;) character. Perl permits the use of escape sequence like “\n”. Moves the cursor to the beginning of

the next line. The variables are neither declared nor initialized.

Executing/ Running a script: way #1

● There are two ways of running a Perl script: at the command line and from a text file.

● For very short scripts, you can just type the whole program at the command line and watch

the computer run it.

C:\>perl -e "print 'Hello, World!'"

Hello, World!

C:\>

Executing a script: way #2

For longer scripts (and ones you would like to reuse), you create a plain-text document with the

program code and then tell the Perl interpreter to run this program, usually on the command line.

● Save as "myprogram.pl":

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 9

Module 5 Unix and Shell Programming

#!/usr/bin/perl

print 'Hello, World!';

Then, on the command line:

● Assign execute (x) permission on the script file and run it by specifying script filename (chmod+x filename).● Provide the execute permission, chmod +x myprogram.pl

C:\>./myprogram.pl

Hello, World!

The following is a sample perl script

#!/usr/bin/perl# Script: sample.pl – Shows the use of variables#print(“Enter your name: “);$name=<STDIN>;Print(“Enter a temperature in Centigrade: “);$centigrade=<STDIN>;$fahr=$centigrade*9/5 + 32;print “The temperature in Fahrenheit is $fahr\n”;

print “Thank you $name for using this program.”

o/p:

Enter your name : stallman

Enter a temp in centigrade: 40.6

The temp stallman

In fahrenheit is 104.9

VARIABLES AND OPERATORS

Perl variables have no type and need no initialization. However we need to precede the variable

name with a $ for both variable initialization as well as evaluation.

Example: $var=10;

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 10

Module 5 Unix and Shell Programming

print $var;

Some important points related to variables in perl are:

1. When a string is used for numeric computation or comparison, perl converts it into a

number.

2. If a variable is undefined, it is assumed to be a null string and a null string is numerically

zero. Incrementing an uninitialized variable returns 1.

perl –e ‘$x++; print (“$x\n”);’

1

3. .If the first character of a string is not numeric; the entire string becomes numerically

equivalent to zero.

4. Perl uses operators like ==,!=,!=,>,<,>= and <= for numeric comparison. For string

comparison, uses similar operators as shell : eq,ne,gt,lt,ge and le.

5. When perl compares string, it has to match the ASCII value of each character starting from

the left. Thus b is greater than aa1234 because the first character, b is greater than a in the

ASCII collating sequence. Similarly a is greater than z.

6. Perl supports unquoted, single and double quoted string.

7. Perl the conditional assignment using the ? and :. Ex: Determine whether February has 28 or

29 days. $feb_days = $year % 4 = =0 ? 29:28;

8. Every expression or comparison in perl returns a value, and can set this value to a variable.

Ex: $x = $y = = $z; Sets the $x value to 1 if $y and $z are equal; otherwise it has the value

0.

9. When Perl sees a string in the middle of an expression, it converts the string to an integer.

Example: "12O34" is converted to the integer 12, not 12034.

10. Some examples for different types of computations in perl

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 11

Module 5 Unix and Shell Programming

• $x =$y =$z =5; //Multiple assignment

• $name =“larry\t\twall\n”; //Two tabs and newline

• $y =‘A’ ;$y++; // This becomes B

• $z =“PO1”; $z++; // This becomes PO2!

• $todays date = ‘date’ //Uses Command substitution

• $name = ‘vcet puttur”

• $result =“\U$name\E”; // $result is VCET PUTTUR

• $result =“\u$name\E” // $result is Vcet Puttur

The Concatenation operators . and repetition operator x

Perl provides 2 operators that operate on strings:

The . operator, which joins two strings together;

The x operator, which repeats a string;

The . operator joins the second operand to the first operand:

Example:

$a = “Info" . “sys"; # $a is now “Infosys"

$x=”microsoft”; $y=”.com”; $x=$x . $y; # $x is now “microsoft.com”

This join operation is also known as string concatenation.

The x operator (the letter x) makes n copies of a string, where n is the value of the right

operand:

Example:

$a = “R" x 5;

$a is now “RRRRR"

$perl –e ‘print “@” x 5;’

@@@@@

STRING HANDLING FUNCTIONS

length determines the length of its argument.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 12

Module 5 Unix and Shell Programming

$x =“ abcdijklm”;

print length($x);

index(s1, s2) determines the position of a string s2 within string s1.

$x =“ abcdijklm”;

print index($x,j);

substr(str,m,n) extracts a substring from a string str, m represents the starting point of

extraction and n indicates the number of characters to be extracted. Substr can extract

characters from the right of the string, and insert or replace a string.

substr($x,4,0) =“efgh”; // stuffs the string$x with efgh without replacing any

string, 0 indicates non replacement

print “$x”; # abcdefghijklm

$y = substr($x,-3,2); // extracts 2 characters from the third position on the right

print “$y” # $y is kl

uc(str) converts all the letters of str into uppercase.

ucfirst(str) converts first letter of all leading words into uppercase.

$name =“shell programming”;

$result = uc($name); // $result is SHELL PROGRAMMING

$result = ucfirst($name); // $result is Shell Programming

reverse(str) reverses the characters contained in string str

$x =“hello”;

print reverse($x); # print olleh

Example perl script

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 13

Module 5 Unix and Shell Programming

#!/usr/bin/perl

$x="abcdijklm";

print length($x);

print("\n");

$l=length("hello");

print "$l\n";

print index($x,a);

print("\n");

$i=index("okay",a);

print("$i\n");

substr($x,4,0)="efgh";

print "$x\n";

#abcdefghijklm

$z=substr($x,0,2);

print "$z\n";

$y = substr($x,-3,2); # extracts from right

print "$y\n" ; # $y is kl

$x ="abcd";

print reverse($x);

print("\n");

$name ="larry wall";

$result = uc($name);

$result2 = ucfirst($name);

print "$result\n";

print "$result2\n";

DEFAULT VARIABLES - $_ AND $. – REPRESENTING THE CURRENT

LINE AND CURRENT LINE NUMBER AND THE RANGE OPERATOR.

Perl assigns the line read from input to a special variable, $_, often called the default variable.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 14

Module 5 Unix and Shell Programming

$_, represents the last line read or last pattern matched. It holds the current line. Many functions

use $_ as a default argument when no argument is mentioned explicitly.

Examples : Normally print function will expect variables, or a list or a string, the value of which

is expected to be printed. If no argument is specified for print function it will print the value of

default variable $_.

$_ = “Hello good morning\n”;

print;This code segment prints the message Hello good morning on the monitor. while (<>) // reading line from a file $_= <STDIN>{print;}<> is called NULL file handle, it reads input from the keyboard (std i/p). Every record read is

stored in the special variable $_.

Example calculating sum:

#!/usr/bin/perl

foreach(1..5)

{

$sum=$sum+$_;

print " the step number is $_ and the sum is $sum\n";

}

The following will display the each list element in foreach

foreach $branch ('cs','ec','cv'){print;print "\n";

}

$. (Current Line number) And .. (The range operator)$. is used to store the current line number. It is used to represent a line address and to select lines

from anywhere. Following code segment prints the message line number is 10 as soon as the

current line number ($.) becomes 10.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 15

Module 5 Unix and Shell Programming

Example 1: if ($. = 10)

{

print “line number is $.\n”

}

Example 2:’ perl –ne ‘print if ($. < 4)’ in.dat # is similar to head –n 3 in.dat

Range operator(..) : The range operator is represented by two double dots ..

Example: perl –ne ‘print if (1..3)’ foo # 1, 2 , 3

Example: foreach $num (1..10){

print $num\n”; # print from 1 to 10

}

perl –ne ‘print if (8..10)’ in.dat # Prints lines 8 to 10 from in.dat

chop() and chomp() FUNCTIONS

The chop() function is used to remove or eliminate the last character (new line

character).

The chop() function is used to remove or eliminate the last character

This function returns the character removed or discarded.

Write a Perl script to demonstrate the use of chop function

Example: #!/usr/bin/perl

print(“enter your name:”);

$name = <STDIN>;

chop($name); #removes newline character

if($name ne “ “) {

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 16

Module 5 Unix and Shell Programming

print (“$name ,have a nice day\n”);

} else {

print (“you have not entered your name\n”);

}

$./name.pl

Enter your name: larry wall

Larry wall, have a nice day

chomp()

The chomp() removes only a new line character, \n, if that appears as the last character. If

more than one new line character exists at the end then only one new line character is removed.

If no trailing new line character is present then nothing is removed.

The chomp() function returns the number of characters removed. This returned value has to

be 1. When no trailing new line character is present, the chomp() function returns a 0(zero) as no

character is removed.

Example: #!/usr/bin/perl

$string1 = "This is test";

$retval = chomp( $string1 );

print " Choped String is : $string1\n";

print " Number of characters removed : $retval\n";

$string1 = "This is test\n";

$retval = chomp( $string1 );

print " Choped String is : $string1\n";

print " Number of characters removed : $retval\n";

When above code is executed, it produces the following result:

Choped String is : This is test

Number of characters removed: 0

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 17

Module 5 Unix and Shell Programming

Choped String is : This is test

Number of characters removed: 1

ARGV[]: Command Line Arguments

Perl also uses the command line arguments which are stored in the system array

@ARGV[]; the first argument is $ARGV[0].

The special array variable @ARGV is automatically defined to contain the strings entered

on the command line when a Perl program is invoked.

Example

#!/usr/bin/perl

print("The first argument is $ARGV[0]\n");

Then, entering the command : $ test.pl 1 2 3

produces the following output:

The first argument is 1

perl script to determine whether the given year is leap year or not, which takes year as the command line argument string. #!/usr/bin/perl

#leap_year.pl

if (@ARGV == 0)

{ die("you have not entered the year \n");}

$year = $ARGV[0] ;

$lastdigit= substr($year, -2 , 2);

if ($lastdigit eq "00")

{

$yesorno = ($year % 400 == 0 ? "certainly" : "not");

}

else

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 18

Module 5 Unix and Shell Programming

{

$yesorno = ($year % 4 == 0 ? "certainly" : "not");

}

print("$year is ". $yesorno . " a leap year\n");

Output: $./leapyear.pl

You have not entered the year

$ ./leapyear.pl 2004

2004 is certainly a leap year

$ ./leapyear.pl 2004

2004 is not a leap year

LISTS AND ARRAYS

A list is a collection of scalar values enclosed in parentheses.

Elements in a list are comma separated and entire collection of scalars is enclosed within

parentheses. The following is a simple example of a list:

(1, 5.3, "hello", 2);

Assigning values to the elements of a list : Values to the elements of a list can be assigned

individually to every scalar variable element of the list one by one or at a single stretch by using

the syntax of the statement below:

($branch, $year, $sem) = (“cs”, “2015”, “6”);

When the above statement is executed $branch will get the value cs, $year will get the value

2015 and $sem will get the value 6 respectively.

A list element can also be an expression as shown below:

(17, $var1 + $var2, 26 <<2);

Arrays

The lists can be assigned to special variables known as array variables

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 19

Module 5 Unix and Shell Programming

An array is a variable that holds multiple values in series. Array names begin with @.

Arrays are the placeholders of lists, which is created by assigning a list to it.

Examples:

@subject = ("maths", "co", "ADE");

In the above example subject is the array name, and 3 values are assigned to it.

@month = (“jan”, “feb” , “nov”);

print @month; # will print the contents of the array month

@month = qw/jan feb mar/; # qw function provides double quotes and comma.

Accessing array elements

Individual elements are accessed using index. These indexes begin with a 0 (zero).

Elements are accessed using $var[n], where n is the index value. Individual elements are

accessed as scalar

print "$month[0] and $month[2]"; # print jan and nov

[Few more examples:

$x = 27; @y = @x; @x = (2, 3, 4);@y = (1, @x, 5); @x= (1...12);@month[1,3..5,12]=(“jan”,“feb”,”mar”,”may”,”jun”);Last index of the array is referred by $#. $# is also used to set the array to a specific size or

delete all its element.

$#month = 10; # array size is set to 11

$#month = -1; #no elements, elements are deleted

Length of the array is determined as : $length = @month;

Reading a file into an array @line = <>; // reads entire line from command linePrint @line; // contains lines of the fileExample : #!/usr/bin/perl@days_betwn = ("wed","thu");@days=(mon,tue,@days_betwn,fri);@days[5,6]=qw/sat sun/;

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 20

Module 5 Unix and Shell Programming

$length=@days;print ("the third day $days[2]\n");print("the days of week @days\n");print("number of elemets $length\n");print("last subscript of the array $#days\n");Output: ./ar.plThe third day wedThe days of the week mon,tue,wed,thu,fri,sat,sunThe number of elements in the array is 7The last subscript of the array is 6

MODIFYING ARRAY CONTENTSPerl supports operations both at the left and right of an array.

push() and pop() functions

Elements are added to an array using the push and unshift operator

Push takes first argument as an array variable name and second is the element to be pushed. push

adds the element to the end of the array.

@list = (1,2,3,4);

push(@list,5); # now the list contents are 1 2 3 4 5

unshift(@list,0); # now the list contents are 0 1 2 3 4 5

unshift function add the element to the beginning of the array.

pop and shift operator is used to remove elements from an array. It works with only one

argument- an array variable name.

pop(@list); # Removes the last element 0 1 2 3 4

shift(@list); # Removes the first element 1 2 3 4

Example perl script

@array = ( 1, 2 );

print "Before pushing elements @array \n";

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 21

Module 5 Unix and Shell Programming

push(@array, (3, 4, 5));

print "After pushing elements @array \n";

result:

Before pushing elements 1 2

After pushing elements 1 2 3 4 5

@array = ( 1, 2, 3, 4);

print "Value of array is @array\n" ;

unshift( @array, 20, 30, 40 );

print "Now value of array is @array\n" ;

result:

Value of array is 1 2 3 4

Now value of array is 20 30 40 1 2 3 4

Splice() operator Splice operator allows adding or removing of elements at any locations of the array.

splice(@array, $offset, [$length], [$list]);

@array : An array on which the splice works

Offset : from where the insertion or removal begins.

$length : number of elements to be removed. If it's not present all the items from the offset

onwards are removed.

$list: Items to be inserted are specified by $list

@list = (1, 2, 3, 4, 5,9);

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 22

Module 5 Unix and Shell Programming

splice(@list, 5, 0, 6..8); #adds at the 6th location – 1 2 3 4 5 6 7 8 9

splice(@list, 0, 2); # Removes from beginning – 3 4 5 6 7 8 9

insert an element in the middle of an array@val = qw(mat ade ds co dms web java);

splice (@val, 3, 0, 'UNIX');

print "@val"; # mat ade ds UNIX co dms web java

Insert a list of values in an array@val = qw(mat ade ds co dms web java);

splice @val, 3, 0, 'UNIX',c++,c;

print "@val"; # mat ade ds UNIX c++ c co dms web java

remove an element from the middle of an array@val = qw(mat ade ds co dms web java);

splice @val, 3, 2;

print "@val"; # mat ade ds web java

@fruits = ("orange", "guava", "grapes", "cherry", "berry");

splice @fruits,1,3;

print "@fruits"; # orange berry

foreach: Looping Through a List foreach construct is used to loop through a list. It is also used to execute the set of

statements repeatedly.

foreach $var (@arr) {

statements

}

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 23

Module 5 Unix and Shell Programming

Each element of the array @arr is picked up and assigned to the variable $var.

The iteration continued as many items as there are items in the list.

EXAMPLE #!/usr/bin/perl

@list = ("10", "20", "30", "40", "50", "60");

print("Here are the numbers in the list: \n");

foreach $temp (@list) {

print("$temp ");

}

The variable $temp takes the value listed in an array list, one by one, and executes the set

of statements in the following block once for each of these values.

Output: ./loop.pl

Here are the numbers in the list

10 20 30 40 50 60

Write a Perl script to find the square root of command line arguments

#!/usr/bin/perl

print (" The program is $0\n");

foreach $num (@ARGV)

{

print("The square root of $num is " . sqrt($num) . "\n");

}

$./square.pl

The program is square.pl

$./square.pl 25 123

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 24

Module 5 Unix and Shell Programming

The square root of 25 is 5

The square root of 123 is 11.0905365064094

split: Splitting into a List or Array

split breaks up a line or expression into fields. These fields are assigned either to

variables or an array.

Syntax: ($var1, $var2, $var3 ….… ) = split(/sep/, str);

@arr = split(/sep/, str);

The fields resulting from the split are assigned to a set of variables, or to an array.

It breaks the passed string into its constituent elements according to a separator. This

separator could be anything like a white space, tab, colon or anything.

So split drags the separator through the string and returns the list of terms or fields that

were separated by the separator.

Splitting into variables : the split result will be stored in a scalar variable as a separate

elements. So given string will be breaks up and stored in a variable.

#!/usr/bin/perl

print("enter 3 numbers:");

$numstring = <STDIN>;

die("nothing entered\n") if ($numstring eq "");

($f_num,$s_num,$l_num) = split(/ /,$numstring);

print("the last,second and first numbers are");

print("$l_num,$s_num and $f_num. \n");

output: num.pl

Enter 3 numbers : [Enter]

Nothing entered

num.pl

Enter 3 numbers: 100 200 300

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 25

Module 5 Unix and Shell Programming

The last,second and first numbers are 300, 200 and 100

OR#!/usr/bin/perl$color_list = (“red,yellow,blue”);

($var1,$var2,$var3)=split(/,/,$color_list);

print “$var1”;

print ”$var2”;

print “$var3”;

Splitting into an array :Split result will be stored in an array.

When split function is used and the returned values are not stored in an array explicitly, they will

be stored in the special array @_ by implication. Ex: split(/:/);

When no string is mentioned explicitly, the split function works on the default variable $_. Ex:

@COLOR = split(/:/);

When no field separator is mentioned explicitly white spaces are taken as the field separator by

default. Ex: split();

@fields = split(/:/, "1:2:3:4:5");

print "Field values are: @fields\n";

result:

Field values are: 1 2 3 4 5

Join: joining a list

Combines its arguments into a single string and uses the delimiter as the first argument.

The remaining arguments could be either an array name or a list of variables or string to

be joined.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 26

Module 5 Unix and Shell Programming

join (EXPR, LIST);

The first argument EXPR may be any string. This function puts the EXPR string between

the individual elements of the LIST and returns the resulting string.

$result = join(“ ”, “this”, “is”,”an”,”example”); //$result = this is an example

$week = join(“ “ , “mon”,”tue”,”wed”);

print $week # mon tue wed

In this example, the value of EXPR is a white space represented by “”. This could have

been any string. Expr appears only between the elements of the LIST never before or

after them.

Example which uses both split and join function

#!/usr/bin/perl

$x=join(“:”,10,20,30,40);

Print “$x\n”;

@y=split(/:/,$x);

Print “@y\n”;

$x=join(“*”,@y);

Print “$x\n”;

Output: $ ./spl.pl

10:20:30:40

10 20 30 40

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 27

Module 5 Unix and Shell Programming

10 -20-30-40

Write a perl program to convert a given decimal number to binary equivalent

#!/usr/bin/perl

foreach $num (@ARGV) {

$temp = $num;

until ($num == 0) { // the condition is false and keep executing that till the condition

becomes true. then terminates

$bit = $num % 2;

unshift(@bit_arr, $bit);

$num = int($num/2);

}

$binary_num = join(“ ”,@bit_arr);

print (“Binary form of $temp is $binary_num\n”);

$#bit_arr = -1;

}

Output: $./dec2bin.pl 2 7 6 5

Binary form of 2 is 010

Binary form of 7 is 111

Binary form of 65 is 1000001

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 28

Module 5 Unix and Shell Programming

FILE HANDLES AND HANDLING FILE

FILE HANDLE

File handle is a program variable that acts as a reference between perl program and the

operating system's file structure.

File handle names do not begin with the special characters and digits & which preferably

uses uppercase letters.

Streams are default file handles which are referred as STDIN, STDOUT and STDERR.

STDIN connects to keyboard and STDOUT,STDERR connect to display screen.

NULL filehandle : Allows scripts to get input from either STDIN or from each file listed

on the command line.

It is written as <>, and is called Angle operator, diamond operator or line-reading

operator.

Reads a line at a time from the file or from standard input as shown below :

perl -e 'while (<>){print ;}' #input is taken from keyboard

perl -e 'while (<>){print ;}' test.txt' #input is taken from file

open()

File is opened using the open() function

Syntax: open(FILEHANDLE, “>,<<,< filename”);

The >/<< these operators indicate the type in which the file need to open like read ,write, append

etc.

The table below shows the form in which the file can be opened

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 29

Module 5 Unix and Shell Programming

Character(s) Meaning < Input(default) i.e. Read Only Access > Output, starting at the beginning of the file (Creates, Writes, and

Overwrite)>> Output starting at the end of the existing data on the file(Writes, Appends)+> Input from and output to the file +< Read, processed and rewritten to the same file, replacing the data that was

red.Example: open(INFILE, “<emp.lst”);

Opens the file emp.lst in read only format.

File can be opened in a append mode as : open (OUTFILE, “>>pgm.lst”);

Perl script to open two files, one for read and another for write.

#!/usr/bin/perl

open(INFILE, "answer.txt") || die("Cannot open file");

open(OUTFILE, ">write.txt");

while(<INFILE>) {

print OUTFILE if(1..3);

}

close(INFILE);

close(OUTFILE);

close()

Closes the file which was already opened.

close(FILEHANDLE);

Closes the file referred to by the FILEHANDLE.

close(FILEIN);

FILE TESTs

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 30

Module 5 Unix and Shell Programming

Perform tests on filenames to see whether the file is a directory file or an ordinary file, whether

the file is readable, executable or writable etc

if -d filename True if file is a directory

if -e filename True if this file exists

if -l filename True if file is a symbolic link

if -s filename True if it is a non-empty file

if -w filename True if file writeable by the person running the program

if -z filename True if this file is empty

If -T filename True if this file is Text file

$x = “rdbnew.lst”;

print “File $x is readable\n” if –r $x;

print “File $x is executable\n” if –x $x;

print “File $x has non-zero size \n” if –s $x;

print “File $x exists\n” if –e $x;

print “File $x is a text file\n” if –T $x;

Write a perl script which will display the files which were modified less than 2.4 hours ago.

#!/usr/bin/perl

foreach $file (`ls`) {

chop($file);

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 31

Module 5 Unix and Shell Programming

if ( ($m_age = -M $file) < 0.1 )

{

print “file %s was last modified %0.3f days back\n”, $file, $m_age;

} }

The die() function

This function prints the value of LIST to STDERR and calls exit with the error value displaying

in monitor.

....

open(INFILE, “<pgm.txt”) or die (“can't open a file”);

........

ASSOCIATIVE ARRAYS or HASH

It is a list of paired elements. An associate array variable name begins with

character %.

These elements are ordered pairs where the first element is referred to as the key and

the second element is referred to as the value of the key element.

When declaring the array, these strings are delimited by commas or more friendly =>

notation.

Each element is paired with a key, which is used to find the data element

Hashes allow specification of any scalar values to access the elements of the array.

Uses string as index, and are no need to ordered by index.

Example: %fruits=("apple", 9, "banana", 23, "cherry", 11);

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 32

Module 5 Unix and Shell Programming

Initialization:

%age = ("a",“5”, "bob","zzz", “50”,"John");

If an array is assigned to a hash, the even index elements become values and the odd index

elements are the corresponding keys

Accessing: The hash variable name preceded by a "$" sign and followed by the "key" associated

with the value in curly brackets.

$values = $age{bob};

$fruits{“apple”} will retrieve 9.

$fruits{“banana”} will retrieve 23

adding : By Assigning the value of the new element to a reference to the key of the new element.

$age{555} = "z";

modifying : $age{bob} = "aaa";

delete : certain key-value pair (element) is deleted in a hash delete $age{50};

Keys and values function

keys: Returns all the keys in the form of a list, which is stored in a array

@list=keys(%region);

values: Returns all the values in the form of a list, which is stored in a array

@list=values(%region);

# sorts on keys in the associative array, region

foreach $key (sort(keys %region)) {

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 33

Module 5 Unix and Shell Programming

# reverse sorts on keys in assoc. array, region

@key_list = reverse sort keys %region;

Example: #!/usr/bin/perl

%region=("N","North","S","South","E","East","W","West");

foreach $letter (@ARGV)

{

print("The letter $letter stands for $region{$letter}". "\n");

}

@key_list=keys(%region);

print("The subscripts are @key_list\n");

@value_list=values%region;

print("The values are @value_list\n");

output : Write output

OR

#!/usr/bin/perl

%days=("mon","monday","tue","tuesday","wed","wednesday","thu","thursday","fri","friday");

foreach $name (@ARGV)

{

print ("The short name $name stands for $days{$name} \n");

}

$days{"sat"} = "saturday";

print("\n");

print("------------------------\n");

@short_list=keys(%days);

print("the short names are @short_list \n");

@long_list=values(%days);

print("the long names are @long_list \n");

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 34

Module 5 Unix and Shell Programming

Summary TABLE all the 3 types of variable.

Data type Format for variable namesof this type

Description

Scalar $scalarname Can be a string, an integer number, a floating-point number or a reference.

Array @arrayname An ordered list of scalar variables that can be accessed using integer indices.

Hash %hashname An unordered set of scalar variables whose values are accessed using uniquescalar values (i.e., strings) called keys.

OVERVIEW OF DECISION MAKING LOOP CONTROL STRUCTURES

1. if (expr) {

True block

}

2. if (expr) {

True block

}

else {

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 35

Module 5 Unix and Shell Programming

False block

}

3. if (expr) {

True block

}

elseif (expr)

{

True block

}

------

------

else

{

False block

}

Note: Write explanationExample : $i=1;

if ($i <= 10) {

print “$i\n”;

$i+=1;

}

short form ifShort form if's can only be used when a single statement is to be executed

This is an example of the shortform if statement: $flag = 1 if ($num1 > 10);

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 36

Module 5 Unix and Shell Programming

This is the same as writing if ($num1 > 10){$flag = 1;}

LOOP CONTROL

Syntax:

while (test) {

block of statement.

}

for (index; test; increment) {

statement;

statement;

}

Note: Write the explanation for describing all the syntax (same as of c program)

Example:

i=1;

while ($i<=10) {

print “$i\n”;

$i+=1;

}

for ($i=1; $i<=10; $i+=1) {

print “$i\n”;

}

Controlling loops

In perl to perform these functions stop looping, to stop execution or somehow to control the

execution of the loop itself following constructs are used.

The last construct: This keyword or construct stops the looping immediately. Execution

continues from the statement that appears immediately after the current loop.

The next construct: This keyword or construct stops the execution of the current iteration of the

loop, goes back to the top, and starts the next iteration with the test. It is like continue in C

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 37

Module 5 Unix and Shell Programming

The redo Construct: This keyword stops the execution of the current iteration, goes back to the

top, and starts the re-execution of the same iteration

REGULAR EXPRESSION

Extract required records by providing certain pattern. Pattern can be one or more field

and can be direct or indirect.

Direct pattern means that to look for record of “unix shell programming” the search

pattern will be exactly the same “classb”. Indirect pattern means the search pattern could

be something like “clas*.”, ie is made up of one or more metacharacters that have special

meanings associated with them and get expanded accordingly during the extraction

process- searching.

Regular expression is the pattern which contains metacharacters.

In perl, pattern matching is done by writing pattern, that is a Regular expression within a

pair of forward slashes. /PATTERN/

By default searching takes place on the contents of $_ special variable

$_ = “monday tuesday fridat sunday”;

if(/sun/)

{

print “pattern exist”;

}

If the pattern is found then a true value is returned. If the pattern is not found then false value is returned.

The table below shows the perl metacharacters along with their meaning

Metacharacter Meaning Example.(dot) Matches exactly any one character in /day./ matches with patterns

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 38

Module 5 Unix and Shell Programming

that position like day1, days*(asterisk) Matches zero or any number of times

the character in the preceding position

/ab*c/ matches with patterns

like ac, abc,abbc...+(plus) Matches one or any number of times

the character in the preceding position

/ab+c/ matches with abc,

abbc .. [[[ ac is not

matchable]]]? (Question mark) Matches zero or one time the

character in the preceding position

/ab?c/ matches with ac or abc

[[[[ only with these two]]]]The following examples demonstrate the use of special characters in a pattern.

1. The * character matches zero or more of the character it follows:

/jk*l/ # This matches jl, jkl, jkkl, jkkkl, and so on.

2. The + character matches one or more of the preceding character:

/jk+l/ # This matches jkl, jkkl, jkkkl, and so on.

3. The ? character matches zero or one copies of the preceding character:

/jk?l/ # This matches jl or jkl.

4. If a set of characters is enclosed in square brackets, any character in the set is an

acceptable match:

/j[kK]l/ # matches jkl or jKl

5. Consecutive alphanumeric characters in the set can be represented by a dash (-):

/j[k1-3K]l/ # matches jkl, j1l, j2l, j3l or jKl

CHARACTER CLASSES AND THEIR SHORTCUTSCharacter class is a list of possible characters taken inside a pair of square brackets ([ ])

Used to match any one character from within the class

To locate all lines that contain IP address. Need to escape the dots too.

if ( /\d+\.\d+\.\d+\.\d+/) {

Symbol Significance\w Match word character [a-zA-Z0-9]\d Matches a digit [0-9]\s Matches white space character [\t\n]\W Doesn’t match word character [^a-zA-Z0-9]\D Doesn’t match matches a digit [^0-9]\S Doesn’t match matches white space character [^\t\n]The match operator (m / /)

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 39

Module 5 Unix and Shell Programming

Check whether a variable contains a specified data or not. Two forward slashes are used

as delimiters of the search pattern.

Pattern matching may be made on any string by binding the search pattern to that string.

For this binding operator =~ is used.

$variable=~m/search_pattern/;

Matches the pattern on right against the string on left.

By default search is made on the current line or string. This search matches all the

occurrences of search _pattern in that line. It is possible to modify this default search

behavior, using single character modifiers such as ,g,i and o.

g – Used for global searching

i – Used for ignoring the case

o – used to search for the pattern only once.

The substitute Operator(s/ / /)Searches for the pattern and replaces or substitutes it with the replacement string

s/search_pattern/replacement_pattern/;

Search_pattern appears between the first and second forward slashes. Whereas the

replacement_pattern appears between the second and the third forward slashes.

Example: $line =~ s/:/-/g; #replaces all the occurrences of : with -

tr function: Translation (tr / / /)

tr command translate characters using the tr prefix. Syntax is similar to substitute operators.

To convert all appearance of uppercase to lowercase

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 40

Module 5 Unix and Shell Programming

$str =~ tr/A-Z/a-z/;

If the second list is empty, the characters from the first list are deleted from the string

$str =~ tr/\,\./ /;

Multiple search pattern (|)

Alternate search pattern is provided by using | operator in between the each search

pattern.

Example: /sachin|dravid|kohli|dhoni/ matches any string that contains subpatterns sachin or

dravid or kohli or dhon.

In this case pipe character (|) behaves like a logical or and hence helps selecting one of the many

alternate patterns mentioned inside the search pattern.

Ex2: /cs|mec|cv/

SUMMARYMatch Regular Expression - m//

Substitute Regular Expression - s///

Transliterate Regular Expression - tr//

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 41

Module 5 Unix and Shell Programming

DEFINING AND USING SUBROUTINES.

Subroutines are nothing but user defined functions.

The use of subroutines results in a modular program. We already know the advantages of

modular approach. (They are code reuse, ease of debugging and better readability).

Frequently used segments of code can be stored in separate sections, known as

subroutines.

Keyword sub along with the name for the sub-routine followed by a block of statements.

The general form of defining a subroutine in perl is:

sub procedure_name {

# Body of the subroutine

}

Example: sub triangle_area {

$base=...

$height=...

Return 1/2*base*height; }

Example: The following is a routine to read a line of input from a file and break it into

words.

sub get_words {

$inputline = <>;

@words = split(/\s+/, $inputline);

}

A subroutine is called by the & symbol followed by the subroutine name

$area=&triangle_area($b,$h);

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 42

Module 5 Unix and Shell Programming

The @_ variable

Inside the subroutine, Perl uses the argument variable @_ to hold all the arguments.

You can use @_ in your subroutines: sub set

{ print join(“ ”, @_);}

set “This”,“is”,“a”,“test” ;

************QUESTIONS APPEARED IN THE PREVIOUS YEARS***************

1. Explain chop function with an example.

2. Explain variables and operators in Perl3. Explain the following in PERL with examples. i) for each loping construct ii) join4. Explain the following string handling functions of PERL with examples:

i) length ii) index iii) substr iv) rever

5. Explain file handling in perl?

6. Write a perl script to convert a decimal number to binary?

7. Discuss how arrays are handled in perl with example?

8. List and explain the string handling functions in perl. Write a program to find

number Of characters ,words as well as to print reverse of a given number.#!/usr/bin/perlprint “enter the sentence”;chomp($in=<STDIN>);print “number of characters in the sentence”;print length $in;@sent=split(“ “ ,$in);$words=@sent;print “\n no. of words in sentence $words\n”;

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 43

Module 5 Unix and Shell Programming

@revers=reverse@sent;print “reversed version @reversed\n”;output: ./word.plEnter a sentence : So far learning was EasyNumber of characters in the sentence : 25Number of words in the sentence: 5Reverse version: Easy was learning far so

9. Explain 'chop()', 'split()' and join function with example10.Write a perl script to determine whether the given year is leap year or not.11.Explain the syntax and examples for perl conditional and looping statements. 12.How are split and join used in perl.

Module 4: Note: Differentiate between hard link and soft link in UNIX with examples. (Even you can list like this)

Hard Link Soft Link

Hard link is only an alias to the existing file. Soft link is the reference to the existing file.

Hardlink or hardlinks cannot be created for

directories (folders). Hard link can only be

created for a file.

Symbolic links or symlinks can link to a

directory (folder).

Removing the original file that your hard link

points to does not remove the hardlink itself;

the hardlink still provides the content of the

underlying file.

Removing the original file does not remove the

attached symbolic link or symlink, but without

the original file, the symlink is useless.

We cant have two linked file names in two

systems.

We can have two linked file names in two file

systems.

The size of the two linked files will be the

same.

The size of the symbolic link file is only the

length of path to the original file.

The inode number of two linked file names are

same.

The inode number of two linked file names are

different.Write Examples

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 44