Monday, November 24, 2008

How to group UNIX Solaris, Linux commands

Simply group them in a small bracket and separate them by semi colon (;)

123-bash: (ls -1;pwd;cd /tmp/curl)

It will list the files in CWD first, print the directory you are in, cd to /tmp/curl

Tuesday, November 18, 2008

How to get user input in PERL

It's simple -

- The angle bracket operator to read from STDIN (see I/O operators in perlop)
- If entering 'Y' or 'N' - make sure to use 'chomp' to cut the new line character.
- @ARGV - the array that holds parameters from the command line (see perlvar)
- Getopt::Long or Getopt::Std - for more powerful handling of command line arguments
- uc - to uppercase an expression
- ucfirst - to uppercase the first letter of an expression

Later in your code, you can compare by using 'eq' or 'ne' or '==' or '!=' or
pattern matching =~ m///, thingy to move on to do what you want.....

Friday, November 14, 2008

Solaris Kernel Modules operations

Kernel Modules:
---------------

modinfo : prints out information on all currently loaded kernel modules.
modload : loads kernel modules.
modunload : unloads kernel modules.
forceload : in the /etc/system file loads a module at boot time.

Monday, November 10, 2008

Passing a DB handler to an user defined function

It is pretty simply. Pass it like an ordinary variable.

Example here:

#!/opt/local/bin/perl

use db_connect;
use warnings;
use strict;


# More efficient than ./test_connection.pl because $0 makes only one DB connection

my $obj = db_connect->new();
my $db = $obj->connect_qora007_mir("qora007_mir");

print " \n";
&checkConnectionMIR_qora007($db);
&checkConnectionCIR_qora007($db);
print " \n";

$db->disconnect();



sub checkConnectionMIR_qora007($)
{
my $dbh = shift;

my $sql;
my $sth;
my $str;
my @row;

$sql = "select sysdate from dual";

$sth = $dbh->prepare($sql);
$sth->execute;

while((@row) = $sth->fetchrow_array())
{
$str = join(" " , map {defined $_ ? $_ : "(null)"} @row);
print "INSTANCE: $str\n";
}

Thursday, November 6, 2008

PERL - IF ELSE statement shortcuts

I will give an example of how to write IF ELSE statement in short. This will be simply useful to read others code if you know that this can also be an option. This kind of code normally comes from seasoned PERL programers.

#!/opt/local/bin/perl

use warnings;
use strict;

print &max(5, 3);
print &max(7, 10);

sub max (@)
{
my ($x, $y) = @_;
return $x > $y ? $x : $y;
}

Explanation: In function max, 2nd lines means that

if $x is greater than $y, return $x or return $y. That's it.....

Wednesday, November 5, 2008

How to open a write into an already zipped file in perl:

How to open a write into an already zipped file in perl:
--------------------------------------------------------

my $temp_file = "file_name.gz";
open(HWRITE, "|gzip -c > $temp_file") or die "cannot open file $temp_file\n";
print HWRITE "Something_1|Something_1|Something_1|Something_1\n"
or die "File write failed : $!\n";