--- In Perl_Official@yahoogroups.com, "arun5167" <arun5167@...> wrote:
>
> please someone post perl turorial with examples expecially for
> administrators
> Thanks in advance
>
Here is a small exercise that I have prepared. This is meant for
someone who wants to learn Perl.
Exercise 1. In a Perl script, accept command line arguements.
Display all the arguements and also the number of arguements.
Expected output:
$ ex1.pl this that
Arguements: this that
Number of arguements: 2
$
===========================
Exercise 2. Accept a filename as command line arguement. Display the
contents of that file.
Expected output:
$ cat > newfile.text
How much is three times humpty-steen?
Elaine, Have you forgotten?
Why does a chicken cross the road?
Who carries home a toper's load?
You are so very stupid, dear!
Elaine, have you forgotten?
(Ctrl+C)$
$
$ ex2.pl newfile.text
How much is three times humpty-steen?
Elaine, Have you forgotten?
Why does a chicken cross the road?
Who carries home a toper's load?
You are so very stupid, dear!
Elaine, have you forgotten?
$
This is similar to displaying the contents of a file using cat
command.
===========================
Exercise 3. Accept a filename as command line arguement. Display all
lines of that file which contain letter d
Expected output:
$ ex3.pl newfile.text
Why does a chicken cross the road?
Who carries home a toper's load?
You are so very stupid, dear!
$
===========================
Exercise 4. Accept a filename as command line arguement. Display a
count of how many lines of that file contain letter d
Expected output:
$ ex4.pl newfile.text
newfile.text contains w 3 times
$
===========================
Exercise 5. Accept a filename as command line arguement. Display all
lines of that file which contain the character ?
Expected output:
$ ex5.pl newfile.text
How much is three times humpty-steen?
Elaine, Have you forgotten?
Why does a chicken cross the road?
Who carries home a toper's load?
Elaine, have you forgotten?
$
===========================
Exercise 6. Accept a filename as command line arguement. Display all
lines of that file which do not contain the letters w and W
Expected output:
$ ex6.pl newfile.text
Elaine, Have you forgotten?
You are so very stupid, dear!
Elaine, have you forgotten?
$
===========================
Exercise 7. Accept a string as first arguement and a filename as
second arguement. Display all lines of that file which contain the
given string.
Expected output:
$ ex7.pl es newfile.text
How much is three times humpty-steen?
Why does a chicken cross the road?
Who carries home a toper's load?
$
The first steps of making our own grep command!
===========================
Exercise 8. Accept a username as command line arguement. Display if
that user is currently logged in or not. Run who or w command and
use its output to determine if the given user is logged in or not.
Expected output:
$ ex8.pl yogesh
yogesh is not logged in
$
$ ex8.pl root
root is logged in
$
===========================
Exercise 9. Accept a username as command line arguement. Display if
that user is currently logged in or not; and if logged in, then how
many times.
Expected output:
$ ex9.pl root
root is logged in 3 times
$
Additional exercise: show "1 time" if user is logged in only once
and "x times" if user is logged in multiple times (use of s in times
only in case of multiple logins)
===========================
Exercise 10. Accept command line arguements. Consider all command
line arguements as user names and for all the given user names,
display if they are logged in or not.
Expected output:
$ ex9.pl yogesh sandesh root
yogesh is not logged in
sandesh is not logged in
root is logged in
$
===========================