Friday 14 June 2013

Perl examples

1) A perl program to read a line  from standard input and  output each words entered using foreach.

#!/usr/bin/perl

$str1 = <STDIN>;

chomp $str1;

$count =1;

foreach $chr1 (split( //,$str1)){

print "\n word $count = $chr1";

$count++;

}

2)  A perl Program to reverse an entered  input of line.

a) Reverse the line of string as a whole.

b) reverse each words in the line

#!/usr/bin/perl

print "\n enter string";

$var1 = <STDIN>;

print "\n enter string";

$var2 = <STDIN>;

$rev1 = reverse($var1);

$rev2 = join ( " ",reverse split(" ",$var2));

print "\n reverse of $var1 = $rev1";

print "\n reverse of $var2 = $rev2";


3)   Perl program containing   substitution of expressions containing variables within patterns.

#!/usr/bin/perl

$no = 25;

$str1 = 'MY NUM IS $no';

$str2 = 'MY NUM INTO 2 IS $no';

$str1  =~ s/\$([a-z]+)/${$1}/g;

$str2  =~ s/\$([a-z]+)/${$1} * 2/eg;

print "\n $str1";

print "\n $str2";


4)Perl program to check whether input number is a natural number or integer.

#!/usr/bin/perl

$str = <STDIN>;

if ( $str =~ /^\d+$/ || $str =~ /^[0-9]+$/)

{

print "\nnatural number";

}

elsif ( $str =~ /^[+-]?\d+$/ || $str =~ /^-?\d+$/)

{

print "\nInteger";

}

else

{

print "\nnot an integer"

}



5)Perl program to add or subtract days from an entered date in YYYY-MM-DD format.It requires the Date::calc module.

#!/usr/bin/perl

use Date::Calc qw(Add_Delta_Days);

print "\nenter date in YYYY-MM-DD  ";

$dt = <STDIN>;

print "\nEnter days to add or -days to subtract  ";

$of = <STDIN>;

@darr = split("-",$dt);

$yr  = $darr[0];

$mon = $darr[1];

$ddi = $darr[2];

printf( "\nnew date  = %04d-%02d-%02d",Add_Delta_Days($yr,$mon,$ddi,$of) );


6) A perl program to print no of days, hours, minutes & seconds between two given  days

#!/usr/bin/perl

use Date::Calc qw(Delta_Days Delta_DHMS);

@date1 = (2004,03,13);

@date2 = (2003,12,15);

$ddiff = Delta_Days(@date2,@date1);

print "\n The days between the two dates $ddiff";

@dtim1 = (2005,06,17,14,36,17);

@dtim2 = (2005,06,18,11,19,35);

@dtimdiff = Delta_DHMS(@dtim1,@dtim2);

print "\n $dtimdiff[0] days $dtimdiff[1] Hrs $dtimdiff[2] Mins $dtimdiff[3] Secs";

.


7) A Perl program to add  a specified days,hours,minutes,secinds to a given date and time.

#!/usr/bin/perl

use Date::Calc qw(Add_Delta_DHMS);

print "\nenter Date & Time in YYYY-MM-DD-HH-MI-SS  ";

$dt = <STDIN>;

print "\nEnter duration to add in DAYS,HRS,MIN,SS   ";

$of = <STDIN>;

@dtmarr = split("-",$dt);

$yr  = $dtmarr[0];

$mon = $dtmarr[1];

$ddi = $dtmarr[2];

$hh = $dtmarr[3];

$mi = $dtmarr[4];

$ss = $dtmarr[5];

@durarr = split(",",$of);

$days = $durarr[0];

$hrs = $durarr[1];

$mins = $durarr[2];

$secs = $durarr[3];

printf( "\nnew date  = %04d-%02d-%02d %02d:%02d:%02d",Add_Delta_DHMS($yr,$mon,$ddi,$hh,$mi,$ss,$days,$hrs,$mins,$secs) );


8) A perl program to read a paragraph into an array and then print a particular line.

#!/usr/bin/perl

@para = (<<"INPUT" =~ m/^\s*(.+)/gm);

It was a day, long and hectic,

the day which I slogged more than ever

but got no reward and no accolades.

INPUT

print "\n $para[1]";


9) A perl program showing different ways of  storing variables in array.

#!/usr/bin/perl

@arr1 = "what are you doing";            #strores entire string as one element;

@arr2 = ("what","are","You","doing");   # stores quoted strings as individual elements

@arr3 = qw (what are you doing ?);         # stores individual words as elements

print "\n $arr1[0]";

print "\n $arr1[2]";

print "\n $arr2[2]";

print "\n $arr3[2]";

10)A perl progrm to store output of Unix ls command into an array.
#!/usr/bin/perl

@arr1 = qx(ls);

print "$arr1[0],$arr1[1],$arr1[2]";

11) A perl program to reduce the  length of an array using $#ARRAY.

#!/usr/bin/perl
@vararr = qw(This is an array);

$x = $#vararr;
print "\n|@vararr| length = " . ($x + 1);
$#vararr -= 1;
$x = $#vararr;
print "\n |@vararr| - length = " . ($x + 1);

PL/SQL examples

PL/SQL examples


1) A PL/SQL  block to check whether value entered by user is present in a list

set serveroutput on;

declare

x char(10);

y number(5);

z date;

BEGIN

dbms_output.put_line('ENTER A VALUE');

x := '&x';

dbms_output.put_line('ENTER another value');

y := &y;

IF x in ('2','4','hi')  then

dbms_output.put_line('FOUND');

END IF;

END;

/





2)PL/SL block  that formats numbers(floating point).

set serveroutput on;

declare

n1 number(6,2);

n2 number(6,2);

n3 number(6);

n4 number(4,-1);  --rounded of to 10

n5 number(4,-2);  -- rounded of to 100

n6 number(1,4);  -- rounded of to 1/10000

BEGIN

n1 := 2014.5;

n2 := 233.465;

n3 := 234.6;

n4 := 1657;

n5 := 1457;

n6 := 1/3845;

dbms_output.put_line('N1 = '||n1);

dbms_output.put_line('N2 = '||n2);

dbms_output.put_line('N3 = '||n3);

dbms_output.put_line('N4 = '||n4);

dbms_output.put_line('N5 = '||n5);

dbms_output.put_line('N6 = '||n6);

END;

N1 = 2014.5

N2 = 233.47

N3 = 235

N4 = 1660

N5 = 1500

N6 = .0003

PL/SQL procedure successfully completed.



3) A PL/SQL program that compares a CHAR and a VARCHAR2 variable with leading spaces.

declare

d1 varchar2(12); 

d2 char(12); -- ADDS SPACE CHARACTERs AT THE END

BEGIN

d1 := 'definitions';

d2 := 'definitions';

IF d1 = d2 then

dbms_output.put_line(' equal');

else

dbms_output.put_line(' not equal');

END IF;

END;

/

4) PL/SQl block to concatenate two variables values.
set serveroutput on;

DECLARE

x number(5);
y char(5);
z char(15);

BEGIN
x :=&x;
y :='&y';

z := x|| ' and '||y;
DBMS_OUTPUT.PUT_LINE(z);
END;
/

5)PL SQL Raise Application error example

SET SERVEROUTPUT ON;
DROP TABLE BILL_PAID;
CREATE TABLE BILL_PAID (
CUST_ID VARCHAR2(6),
BILL_DATE VARCHAR2(10),
BILL_AMOUNT NUMBER(10,4)
);


DECLARE

CUT_OF_DATE VARCHAR2(10) := '2014-01-05';
cid BILL_PAID.CUST_ID%TYPE;
DUE_DATE_EXCEEDED EXCEPTION;
PRAGMA EXCEPTION_INIT(DUE_DATE_EXCEEDED, -20006);

PROCEDURE pay_bill ( cust VARCHAR2 , dt  VARCHAR2, amount NUMBER) IS
 BEGIN
 
    COMMIT;
    IF  TO_DATE(dt,'YYYY-MM-DD')  > TO_DATE( CUT_OF_DATE,'YYYY-MM-DD' ) THEN

     RAISE_APPLICATION_ERROR (-20006,'BILL PAID AFTER DUE DATE');
  
   END IF;
   INSERT INTO BILL_PAID VALUES (cust,dt,amount);
END  pay_bill;

BEGIN
cid := 'J54677';
pay_bill(cid,'2013-12-30',1450.50);

cid := 'J54678';
pay_bill(cid,'2014-01-12',1320.00);  --will cause exception.

cid := 'J54642';
pay_bill(cid,'2014-01-05',750.50);

EXCEPTION
WHEN DUE_DATE_EXCEEDED THEN
DBMS_OUTPUT.PUT_LINE ('PENALTY CHARGED FOR CUSTOMER ' || cid);
DBMS_OUTPUT.PUT_LINE ('ERRNO = '|| SQLCODE || ' ERROR MSG IS ' || SQLERRM);
END;
/


Monday 3 June 2013

mail/mailx



mail/mailx/ 

unix mail utilities mail or mailx can be used to send mails to  users within a machine, a remote machine or web users using a relay server.

Example1)
To end mail to user hradmin on the machine v_serv1
/home/gadm$ mail x –v  -s “intimation”  hradmin@vserv_1  <mail_content_file
Here  the text “intimation”after –s  is the subject and mail_content_file contains mail body.

Example2)
Configurations for sending external mails through a relay server(eg IBM LOTUS) .
1)make an entry of mail relay server in /etc/mail/sendmail.cf file(can be done from root login)
vi /etc/mail/sendmail.cf
DS<mail relay server name>  ->add your mail server hostname in domain form(eg: mailserver.cbbs.net)

2)  edit /etc/hosts file and make an entry  of mail relay server.
10.5.6.34     <mail_svr_hostname>    <domain_name>
Also If your hostname contains only IP and plain hostname like
10.234.45.56  <my_hostname>,
Add another column  to make your hostname have a valid domain name(eg .com) otherwise you will get “hostname not qualified” error.
10.234.45.56  <my_hostname>   <my_hostname@<my_domain>

Ask your mail admin to add this hostname as a qualified  hostname  for receiving mails from.

3) stop and start the sendmail daemon.
stopsrc -s sendmail
startsrc -s sendmail -a “-bd -q30m”

Now you can send external mails using mailx command as follows.(AIX)
mailx  -v -s “<subject>” -r “< sender_name>”  -c  ”<cc_address(es)>”  <to_address>  < content_attach
here
“<subject> “         Is a quoted text   received  under subject(quoting is must when there is space in the text.
<sender_name>     Is the  name of the sender as seen by the recipient. If it is not used the recipient will see the sender as your <default user_name@domain.com>.
”<cc_address(es)>  Refer to those addresses to whom you need to send the copies of this mail to.
“<to_address>     Actual recipient address.
content_attach    A file containing body of the mail and encoded attachments.

A file like conetent_attach can be formed by using the uuencode command. For example if you hava a content file named  c_mail_body.txt  and an attachment  /home/v_admin/pdf_archive/trans_list.pdf  file   to send .
Uuencode  /home/v_admin/pdf_archive/trans_list.pdf   trans_list.pdf    >> c_mail_body.txt 
Now the file c_mail_body.txt   will contain both mail contents and attachment. You can use this file  to input  as content_attach  file  in the mailx  syntax.
mailx  -v -s “<subject>” -r “< sender_name>”  -c  ”<cc_address(es)>”  <to_address>  < c_mail_body.txt