[Ilugc] [TIP] perl tutorial IX ( perl and shell scripts)

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sun, 11 Dec 2011 07:24:26 +0530

Perl is a good substitute for shell scripts but not really good in the
 sense that shell scripts have not lost their relevance and status.

Due to its rich integration with the UNIX environment and culture perl
 makes an excellent alternative for automating/scripting and install
 scripts.

In fact in the bad old Indian IT industry perl is almost only used for test
 suites and test automation. We will look at that one of these days. Perl
 has excellent support for test automation. Really, easy cool and intuitive.

Today we are looking at how perl knowledge can make you better
 network admin.

In fact I wrote my first perl scripts when I created an install script for a
 crypto product in 2003. The admin web interface was entirely perl.

So I had plenty of exposure. Moreover I think some parts of the
install script was in shell.
 I don't remember well. You can mix and match anything in a shell script.

Let us first look at perl's find and perl's grep.

Here is:

$ perldoc -f grep
grep BLOCK LIST


       grep EXPR,LIST
               This is similar in spirit to, but not the same as, grep(1) and
               its relatives.  In particular, it is not limited to using
               regular expressions.

               Evaluates the BLOCK or EXPR for each element of LIST (locally
               setting $_ to each element) and returns the list value
               consisting of those elements for which the expression evaluated
               to true.  In scalar context, returns the number of times the
               expression was true.

                   @foo = grep(!/^#/, @bar);    # weed out comments

               or equivalently,

                   @foo = grep {!/^#/} @bar;    # weed out comments

As usual perl man pages are written for geniuses and are not very clear. But
 once you use it a few times you will get the hang of it.

There is a script called find2perl and you can :

$ man File::Find

Let us look at an example for perl's grep.

$ cat i.pl
@a = (1,2,3,4,"what", 'bat', 23);

@b = grep(/\d/, @a);

print "@b is only numbers\n";

As with every other thing in perl to use it to your advantage you should
 know regular expressions. Without a grasp of regular expressions you don't do
 much in perl.

What has this got to do with shell scripts?

Where are shell scripts used? And when should we go for perl scripts?

Shell scripts are used for installing software. Shell scripts are used
in cron jobs for
 automation and for network testing and even for avoiding repetition for some
quick task you have in mind.

You have to use perl instead of shell when you wish to do sockets programming.

However this is not strictly true. There are plenty of command line tools like
 curl, wget and netcat which can help you even with sockets, but the full power
 of sockets will be available only in perl.

I will give a sample of how something is written in shell and how the
same thing is
 done in perl.

$ cat i.sh
for i in /etc/*.conf
do
        echo "---- New file $i ----" >> /tmp/configsinetc
        echo  >> /tmp/configsinetc
        cat -n $i >> /tmp/configsinetc
        echo  >> /tmp/configsinetc
done

$ cat i.pl
#!/usr/bin/perl

use File::Glob ':glob';

@f = bsd_glob('/etc/*.conf');

open FH, ">/tmp/configs";
for $fl (@f) {
        open RD, $fl;
        print FH "--- New file $fl ---\n\n";
        while(<RD>) {
                print FH "$. $_";
        }
        close RD;

}
close FH;

Both are expected to do the same thing:

Write files with .conf suffix under /etc/ all into one file with line numbers.

The perl version seems substantially difficult as you have to do
 manual reading of each file.

The shell script however is much simpler.

But there are counter examples. Let us look at one:

$ cat f.pl
#!/usr/bin/perl

open FH, "/etc/passwd";

while(<FH>) {
        ($uname) = split /:/;
        if(length($uname) eq 5) {
                print;
        }
}
close FH;

Here we find out usernames that are 5 chars in length.

How will you do this with shell script?

Perl is a higher level programming language than the shell script. So
 it comes with a convenience that you can make very good use of.

-Girish

-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts:

  • » [Ilugc] [TIP] perl tutorial IX ( perl and shell scripts) - Girish Venkatachalam