Sunday, October 19, 2008

Split

#!/usr/bin/perl

$str="one last challenge";

@words = split(/ +/, $str);


print($words[0] ."\n");


print($words[1] ."\n");

Saturday, October 18, 2008

Arrays

Arrays in the Perl starts with '@' symbol and it is not limited to only one data type.

#!/usr/bin/perl -w

@myArray=("Zero","one","two","three","four","five","six","seven","eight","nine");

foreach $val(@myArray)
{
print($val."\n");

}

Change the case to upper and lower

#!/usr/bin/perl -w

$str="Dasuntha";

$sub=substr $str, 2, 4;

print($sub."\n");

#convert the Uppercase

$Uppercase=uc $str;

print($Uppercase."\n");

#convert case of the first letter
$fcase=ucfirst $str;

print($fcase."\n");

$lcase=lcfirst $str;

print($lcase."\n");


#convert the Lowercase

$Lowercase=lc $str;

print($Lowercase."\n");

#chop -get the last letter
$ch=chop $str;

print($ch."\n");

Arguments are passed by reference

#!/usr/bin/perl -w
$v = 32;
print ("Value before calling the function: " . $v . "\n");
fun1($v);
print ("Value after calling the function: " . $v . "\n");
# end of program
sub fun1
{
print ("Value within the function: " . $_[0] . "\n");
$_[0] <<= 1; # shifts all bits left by 1 (like * 2)
print ("Value within the function, again: " . $_[0] . "\n");
}

Write methods in perl

Find the maximum value of the array

#!/usr/bin/perl -w

# find the maximum value of the array

$maxVal=maximum(2,10,1,9,3,12);
print("\n\nThe Maximum is : $maxVal \n");

sub maximum
{

$val=$_[0];

foreach $my(@_)
{
if($my > $val)
{
$val=$my;
print("\n Max value change to $val");
}
}
return $val;
}

last, next, redo and labels in control structures

#!/usr/bin/perl -w

for($i=0; $i<10; $i++)
{
print($i."\n");

#last will end up the loop
if($i == 7)
{
last;
}

#next
if($i == 5)
{
next;
}

}

print("\nSecond loop ...\n");

for($i=0; $i<10; $i++)
{
print($i."\n");

#next
if($i == 5)
{
next;
}

}
print("\nThird loop ...\n");

# put the label out
out:for($i=0; $i<3; $i++)
{
print($i."\n");

In:for($j=0; $j<3; $j++)
{
print($j."\n");

if($j==1)
goto out;
}
}

Friday, October 17, 2008

Find and Replace

Replace the word new with the word old. /g is used to search globally. /i is used to make case insensitive search.

#!/usr/bin/perl -w

while($_=STDIN)
{
chomp($_);

if (s/new/old/gi)
{
print($_ ."\n");
}
}


If we want to replace only the word new we can use following code.

#!/usr/bin/perl -w

while($_=STDIN)
{
chomp($_);

if (s/\bnew\b/old/gi)
{
print($_ ."\n");
}

}

Anchors

Allow to fix the pattern into the specific part.

Example: \b is limiting the pattern into the word boundary.

#!/usr/bin/perl -w

print("Please Enter a sentence: \n");

while($_=)
{
if(/sky\b/)
{
print("Sky is at the end");
}

if(/\bsky/)
{
print("sky must at the beginning\n");
}

if(/sky\B/)
{
print("sky is not in boundries \n");
}

}



/B can use to find the sentences with that word is not at the end

Pattern Matching

There are some special read only variables
$&: matched part of the string
$`: preceding part
$’: following part

Example:
#!/usr/bin/perl -w

print("please Enter word to find out\n");
$a=;

chomp($a);

print("Please ENter the sentence \n");

while ($_=)
{
chomp($_);


if(/$a/)
{
print("\n The word is find in the sentence $_ \n");

print("The matching part is ".$&. "\n");

print("Before the matching : $`\n");

print("After the matching : $'");

}
}

Thursday, October 16, 2008

Metacharacters

^ matches at start of line
$ matches at end of line
. matches any single character except newline (\n)
* matches zero or more of previous character or group
? matches zero or more of previous character or group
+ matches one or more of previous character or group
.* as many characters as you like


Program to illustrate above regular expressions

#!/usr/bin/perl

#should be
while(STDIN)
{

if(/^hello/g)
{
print("hello is at the beginning \n")
}

if(/Bye$/g)
{
print("Bye is at the end \n")
}


if(/A?/g)
{
print("This has zero and one A with regular Expression'?'\n")
}

if(/A*/g)
{
print("This has zero and one A with regular Expression '*' \n")
}

if(/A+/g)
{
print("This has one or more A \n")
}

}

Tuesday, October 14, 2008

More about Arrays

Get Array length
#!/usr/bin/perl -w

@myarray=(1,2,3,4,5,6,7,8,9);

$len=@myarray;

print("length of the Array : $len");


@ARGV is the array that contains the command line arguments.
example:
#!/usr/bin/perl -w

print("First Argument is : $ARGV[0]\n");

print("First Argument is : $ARGV[1]\n");

print("First Argument is : $ARGV[2]\n");

print("First Argument is : $ARGV[3]\n");

File Handling

#!/usr/bin/perl -w

open (INFILE, "first.txt") or die "Input file: $!";
open (OUTF, ">second.txt") || die "Output file: $!";

while ( INFILE ) {
#Replace the "hi" with "Hello"
s/hi/Hello/g;
#print the input stream into the file "second.txt"
print OUTF $_;
}

Arrays and foreach loop

#!/usr/bin/perl -w

#Define array
@myArray=(1,2,3,4,5,6,7);

#foreach loop
foreach $myVal(@myArray)
{
print($myVal);
print("\n");
}

#To reverse the array
print("Reverse Array \n");
foreach $revVal(reverse @myArray)
{
print($revVal);
print("\n");
}

#Directly print Array

print("@myArray \n");

Welcome to Perl!!

Write Hello World program using perl.

#!/usr/bin/perl -w

print("Hello World \n");


#! -> Gives specifies which perl to use
# -> Comments
-w -> This flag gives more warnings about possible errors.

Next Program


#!/usr/bin/perl -w

$ln=0;
# Should be while()
while(STDIN)
{

$ln++;

$len=length($_);

if($len > 15)
{
print("Line $ln has $len Characters\n");

}
}


Above program will print the lines which have more the 15 characters. Variables in perl start with $, @ or % sign. $ is for that normal variables (scalar), @ is for the arrays and % is for the associative arrays.

$_ is defaultly allocated for the input stream.

Now we extend above program little bit.

#!/usr/bin/perl -w

$ln=0;

#should be while()
while(STDIN)
{

$ln++;

$len=length($_);

if($len > 15)
{
print("Line $ln has $len Characters\n");

}

if(/hello/)
{
print("Line $ln has the word hello");

}
}


Put if statement to identify the sentences with the word "hello"

Saturday, August 30, 2008

sed

sed command is used to search and replace the word.
eg:
sed "s/old/new/g" filename

This command find and replace the word old with new globally.

Search and Replace specific lines only.

sed '1,3s/a/A/g' filename
Above command replace the 'a' with 'A' in first three lines in the file.

Input a square brackets.
sed 's/[A-Z]/[&]/g' filename

To print '*' at the beginning of files those contain capital letters
sed '/[A-Z]/s/^/*/' filename

Search and Replace in vi editor

Replace the word at the beginning of the sentence. Press ecs in vi editor and type
:%s/^step/more/g
This command replace the sentence starting with 'step' with the word 'more'.
'g' option is used to replace everything (globle search). Other wise it replaces the first sentence.

:%s/end$/first/g
This command replaces the word of end at the end of sentence with first.

Serch by word
:%s/\/new/g

if you want to replace " character with someother word or charecter use
:%s/\"/word/g
or :%s/["]/word/g

Thursday, August 28, 2008

xargs

xargs command take the output from one command and send certain number of lines to another command.
Lets look at the following example
ls > list.txt
cat list.txt | xargs -i file {}


file command use to get the type of the file.
xargs -i command takes one whole line as a one input and ingore the space infront of the lines.

xargs -t command writes each generated command to the standard error.

xargs -l command execute for each nonempty line. Line is considered to be end with the first new line character.

Wednesday, August 27, 2008

grep

grep command is used to search patterns in files.
eg: when we want to find lines with the word 'apple' we can use
grep apple filename
This can also do using
cat filename | grep apple
grep -l gives the file name that has the required pattern. As example if we want to find the name of text file that contains apple,
grep -l apple *.txt

If we use
cat filename | grep -l apple
command gives "stdin", because pipe doesn't know anything about where input is coming from.

grep -i
makes grep command case insensitive.

grep -w command only give the matching of whole word.

grep -c
gives the number of lines those matches with required pattern.

If we want to do invert of particular command, we can use
grep -v apple filename

Two grep commands can make logical and operator.
eg:
grep apple filename | grep banana filename
this command matches both apple and banana

If we want to find the sentences start with "My"
grep ^My filename

The command
grep me$ filename gives the sentences end with me in the file.

Make Logical OR operator
egrep operator is used to expand the capabilities of grep operator. egrep operator can work with logical OR using '|'.
eg:
egrep 'apple | banana' filename

echo $?

echo $? have the return value of the command. Therefore this is used to check that the command properly worked or not. If command properly worked this returns 0.

#!/bin/bash
cat $1 >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo "Cat worked"
else
echo "File Not Found"
fi


The above program use the command line argument as a file name.

* In unix /dev/null folder is always empty. If we want to delete something we can move that in to /dev/null folder

eg:
ls -l /etc >/dev/null 2> /dev/null
This command doesn't output anything to the screen.
The purpose of this is to make sure that the command is properly working. For that we have to type echo $? immediately after executed that command.

Monday, August 25, 2008

Redirect Vs Pipe

Redirect use to put the output of a command into to a file or take input from a file.

But in pipe output of first command further process in second command.

Tuesday, August 19, 2008

Another way to do Arithmetic expansion

This is the third way to do arithmetic operations. First we looked at 'let' and 'expr' commands.
Now we use $ sign with two brackets.
example1:
echo $(( 5 * 6))

example2:
echo $(($((4 * 5)) + 300 ))

Monday, August 18, 2008

Redirect the standard error

There are three I/O streams available in Unix.
1. STDIN (0)
2. STDOUT (1)
3. STDERR (2)

To redirect the error to a file

ls -l doesnotexist 2>err.txt

Example:
ls -l exp 1>result.txt 2>error.txt
This command outputs the standard output into the file called result.txt and the standard error to the file called error.txt.

Redirection

To redirect the output to a file use '>' command
example :
ls -l > example.txt
'>' command replace the existing content of the file. If you want to append the output to with existing content, use '>>'
example:
ls -l >> example.txt
'<' command use to get the input from a file.
example:
cat < example.txt
This redirection command doesn't know where is the input come from.
For example if you use
wc -w < example.txt
It only outputs the number of words in the file.

If you use wc -w example.txt instead of that you will get the number of words and the name of the file.

Friday, August 15, 2008

Pipe

Pipe

Pipe command can use to combine two commands or more. That means output of one command will input for the next command.
command 1 | command 2


As an example
ls -l | less
This command displays ls -l results page by page. We can move up and down using arrow keys.

'more' is smiler to less command. But more doesn't support for arrow keys.

Example with more than two commands
ls -l| head -4| tail -1



important : command connected using pipe are execute separately and simultaneously.
Normally in piping standard output is redirected to the next command. That means not the standard error.
Let's see
ls -l myFolder | wc -l
where myFolder directory doesn't exist.
This command makes the output as 0.

Thursday, August 14, 2008

i-node

i-node store everything about files other than the content.
i-node stores the addresses where the content is stored.


Create a link to a file
First create a file with one or two sentences.
eg: example1.txt
Then create a link to that file using following command
ln example1.txt example2.txt
This is different from cp example1.txt and example2.txt, because in ln command both files share the same i-node.


Therefore when type ls –i we can see the i-node and number of links for the same i-node.
Then edit the content of one file (as an example : example1.txt). The content of other file is automatically changed. We can see that by typing cat example2.txt.


If change the permission of one file,
eg: chmod 751 example2.txt
Then permission of other file which linked to that one is automatically changed.

Tuesday, August 12, 2008

Wildcards

Wildcards are *, ?, [].
To list files and directories starting from a,
ls a*


To find files and directories with a in their name,
ls *a*


Consider the following example
ls ?a*
In here ‘?’ represents only one character. That means this command lists all files and directories with a as second character in their name.


ls M[ia]*
This command lists files and directories starts with M and second character is either i or a


ls ?[va]*
This command lists files those have v or a as a second character


We can use wildcards when coping, moving and deleting files.
Example
cp f* demo
This command copies all the files starting from f to directory called demo


Important:
Wildcards are very powerful, but be careful, because lots of damage can be happen very easily specially when deleting.

Monday, August 11, 2008

Identify the permission

When we are working in UNIX how can we get to know about the permission of current working directory?
We can use
ls –ld for that.

If not we can use ls –l ..
In this case output can have lots of directories.
Therefore first one is more convenient.

Permission is divided in to three categories. Those are write permission, read permission and execute permission. And permission is granted to three groups. First one is owner of the file, next is the group of owners and the other one is rest of others.

Permission is represented in binary format with eight combinations.

R W E
22 21 20
0 0 1 Value 1 means execute only
0 1 0 Value 2 means read only
1 0 0 Value 4 means write only
1 1 1 Value 7 means read, write and execute

Likewise there are eight combinations.

Another way to change the permission
chmod g+w example1.sh
This command gives write permission to group

Thursday, August 7, 2008

expr

expr is another command that is used to evaluate arithmetic expressions similar as ‘let’ command.
But expr can’t define variables.
expr command is working as following example.

num1=1
num2=2
expr $num1 + $num2


Important :
In expr command multiplication works different way. That means this is not working.
2 * 3

Have to type '\*' to get the multiplication
2 \* 3

echo -e

echo –e command allows interpreting backslash escapes

eg:- echo “Date and time is : \c”
Date

‘\c’ is used to print the output in the same line

eg:- echo “Have a nice day \n”
‘\n’ is used to make a new line after that.

Wednesday, August 6, 2008

Get the arguments from UNIX shell

This is a simple script program to calculate average of three numbers input as command line arguments.

Important --> UNIX is case sensitive

In scripting
# --> use for insert comments

This is the script avg.sh
#following command indicate the place to interpret the script
#!bin/bash

#$1 $2 and $3 get the command line arguments
echo “First three arguments are $1 $2 $3”
echo “This program is $0”

# $* can use to display all the command line arguments
echo “All arguments : $*”

# $# is used to get the number of arguments
echo “Number of arguments: $#”

# let command is used to execute arithmetic expression
# when declaring a variable that should not put $ sign at the beginning
let sum=$1+$2+$3
#whenever that variable is referring $ sign must put in front of the variable name
echo “sum is $sum”
let avg=$sum/$#
echo “Average is : $avg”

Start to do scripting

To start the UNIX scripting users have to familiar with one of UNIX editors. As a example vi, emacs or PICO.
Open the ‘vi’ editor just type vi and then press ‘i’ to go to the insert mode.
Then type following simple script.
Echo “Hello ”
whoami
Echo “Date and time is :”
Date


echo command dispays the output to the screen.

After that press esc button and type :w followed by the file name
:w example1.sh
Press shift and double z to exit from the vi editor.

After that permission must be given to this file to execute.
This can do using
chmod +x example1.sh
or chmod 755 example1.sh
After executing this command
owner has permission to write, read, and execute,
owner's group have permission to read and execute,
rest of the other people have permission to read and execute.

./example1.sh is used to run the script before set the class path to the current working directory.

To set the path into current working directory
PATH=. : $PATH
After that exmple1.sh can run by typing just example1.sh

man for help

man is the most important command in Unix. It gives complete description about every command.

UNIX

UNIX is developed by programmers for the programmers.