Sep 16, 2012 ... chomp function: http://www.tizag.com/perlT/perlchomp.php. 6. #!/usr/bin/perl. $
var1="morning"; #this is a variable print "Enter your name: ";.
9/16/2012
Perl Scripting Basics IT 4423 Unix/Linux Administration
J.G. Zheng Fall 2012
Overview Perl scripting language basics
Script files Language elements Variable Operators Control flow Function
Scripting apps
String processing File processing 2
1
9/16/2012
Perl Practical extraction and report language (unofficial) Perl is a general purpose programming language
Used widely for system administration, network programming, web CGI, etc.
Perl is installed on most Linux distros
Check perl's version and installed directory
3
Perl Script File Script file
File extension .pl Add “#!/usr/bin/perl” as a directive in the first line Case sensitive
Statement delimiter
Use ; to separate (or end) statements (required)
Use # for comments (same as Bash) #!/usr/bin/perl $var1="morning"; #this is a variable print "Good $var1\n";
#!/bin/bash var1="morning" #this is a variable echo "Good $var1" 4
2
9/16/2012
Script Execution Similar to bash scripts
Use the perl process to execute scripts perl [script file name] Or allow the execution permission
Source error
The program will not execute if there are syntax errors (compilation error). – different from Bash Initially, the file does not have execution permission. Use "perl" to execute it or add execution permission
!! For security reasons, the current directory is not in the search path. Add “./” to force search the current directory 5
Input and Output Output
Use "print" command the control characters like \n, \t, etc.
Advanced output
http://www.tizag.com/perlT/perlstrings.php
http://en.wikibooks.org/wiki/Perl_Programming/Advanced_Output
User input
Use or just http://www.tizag.com/perlT/perluserinput.php chomp function: http://www.tizag.com/perlT/perlchomp.php
#!/usr/bin/perl
#!/bin/bash
$var1="morning"; #this is a variable print "Enter your name: "; $var2=; different print "good $var1, $var2\n"
var1="morning"; #this is a variable read -p "Enter your name: " var2 echo "good $var1, $var2" 6
3
9/16/2012
Variables Variable naming
Variable names are case sensitive Dynamically typed: string, number, etc. – different from bash
Assignment
Variables are always indicated by $, both in assignment and reference – different from bash No space around the
#!/usr/bin/perl
different
$var1 = "morning"; #this is a variable print "Enter your name: "; $var2=; print "good $var1, $var2\n"
#!/bin/bash
= symbol in bash; no such restriction in perl
var1="morning"; #this is a variable read -p "Enter your name: " var2 echo "good $var1, $var2" 7
Script Arguments Arguments are stored in an array @ARGV and can be accessed through $ARGV[0], $ARGV[1], etc. $#ARGV is the index number of the last array element
So, $#ARGV+1 is the number of arguments
Script arguments can be supplied at the command line separated by space #!/usr/bin/perl
Use parentheses to force a calculation
print "Number of arguments: ".($#ARGV+1)."\n"; print $ARGV[0]."\n"; print $ARGV[1]."\n"; . is the string print $ARGV[2]."\n";
concatenation operator
#!/bin/bash echo "Number of arguments: $#" echo "$0" echo "$1" echo "$2"
8
4
9/16/2012
Arithmetic Operations Basic operators and operator precedence are similar to C style languages
+, -, *, /, %, ** ++, --, +=, etc.
#!/usr/bin/perl $var1=5; $var2=10; $var3=$var1+$var2; print "$var1+$var2=$var3\n"; $var4=$var1/$var2; print "$var1/$var2=$var4\n";
#!/bin/bash !!Different: • In Bash, arithmetic operations need to be specifically instructed (using "let" or other methods). • In perl, there is no need to.
var1=5 var2=10 var3=var1+var2 echo "$var1+$var2=$var3" let var3=var1+var2 echo "$var1+$var2=$var3" var3=$[var1+var2] echo "$var1+$var2=$var3" 9
Relational and Logical Operations Get more
http://www.tizag.com/perlT/perloperators.php !!Different: • In Perl, operators are the same for strings or numbers. • In bash, they are different
10
5
9/16/2012
Flow Control – If #!/usr/bin/perl
$var1=6; $var2=5;
In Perl, the if structure is much more like C style languages and much simpler
Note the keyword for else if is "elsif"
var1=5 var2=5
Bash syntax for if is much more strict
if [ $var1 = $var2 ]; then echo "Same" else echo "Different" fi
if ($var1==$var2) { print "Same"; } elsif ($var1>$var2) { print "$var1 is greater"; } else { print $var1." is smaller"; } print "\n";
#!/bin/bash
{ } are required
11
Compound Conditions Logical operator
&&: logical AND ||: logical OR
#!/usr/bin/perl
In Perl, the logical comparison is much more like C style languages and much simpler
#!/bin/bash
Bash syntax for if is much more strict
$var1=3;
var1=5
if ($var14) { print "$var1 is Within range"; } else { print "$var1 is out of range"; } print "\n";
if [[ $var1 -lt 10 && $var1 -gt 4 ]]; then echo "Within range" else echo "Out of range" fi
12
6
9/16/2012
Flow Control – For Loop
#!/usr/bin/perl
In Perl, the loop structure is much more like C style languages and much simpler
#!/bin/bash for (( i=0 ; i < 3 ; i++ )); do echo "$i" done
for ($i=0 ; $i < 3 ; $i++ ) { print "$i\n"; }
13
Flow Control – While Loop More
http://www.tizag.com/perlT/perlwhile.php
#!/usr/bin/perl
In Perl, the loop structure is much more like C style languages and much simpler
$counter=0; while ($counter