Saturday, March 14, 2009

Read command line arguments in perl script

Somehow, I never got it right in first attempt and hence keeping a note here.

use strict;
use Getopt::Std;
use vars qw ($opt_d $opt_p $opt_r $opt_m $opt_t $opt_a);
getopts('d:p:r:m:t:a:') ;
$Depot = $opt_d;
$Product = $opt_p;
$Release = $opt_r;
$Branch = $opt_m;
$BuildTimeStamp = $opt_t;
$Action = $opt_a;
print ("Depot == $Depot");

The most important code to note is "use vars qw" declaration for opt_? kind of variables.

Returning multiple arrays from a perl function

I wrote a perl function where I was trying to return 2 arrays from a perl function with statement,
return (@array1, @array2);
Return values were being collected as
my (@a1, @a2) = array_function ();
Even though there were no errors, my script was not working as expected. Only when I digged deeper I realized that returned arrays were not getting collected properly in @a1 & @2. Perl, was returning both of those arrays in concatenated manner and all the contents were getting assigned to single array @a1.

The fix:
To fix this issue, I changed the function to return references to array instead of arrays themselves. References are scalars and hence can be returned from functions just like any other scalar variables. Now my code inside a function changed to

return (\@array1, \@array2);

and calling statement changed to

my ($ref_a1, $ref_a2)= array_function ();
The way these references are used to retrieve the contents of array is to use a formation,
@{ref_a1} and @{ref_a2}

This one was a good learning!

Thursday, December 11, 2008

Parallel Program Execution using Windows Batch File

The requirement is to initiate 4 program executions parallely. The main calling program should not exit until all 4 executions are complete. This is how I did my POC:

a.bat : (Main Calling Script)

start cmd /T:A0 /k "call D:\scratch\sanbox\a1.bat"
start cmd /T:B0 /k "call D:\scratch\sanbox\a2.bat"
start cmd /T:C0 /k "call D:\scratch\sanbox\a3.bat"
start cmd /T:D0 /k "call D:\scratch\sanbox\a4.bat"

:TimePlease

ping 1.1.1.1 -n 3 -w 120000
echo Checking if everything is done
findstr /N ^$ a.out | findstr 4

if NOT %ERRORLEVEL% == 0 goto TimePlease



The 4 programs that were to be executed parallely:

a1.bat :

@echo "In A1 Execution"
ping 1.1.1.1 -n 10 -w 7000
@echo "A Done" >> a.out

a2.bat :

@echo "In A2 Execution"
ping 1.1.1.1 -n 30 -w 1000
@echo "AA Done" >> a.out

a3.bat :

@echo "In A3 Execution"
ping 1.1.1.1 -n 70 -w 3000
@echo "AAA Done" >> a.out

a4.bat :

@echo "In A4 Execution"
ping 1.1.1.1 -n 50 -w 5000
@echo "AAAA Done" >> a.out


The way it works is, when I invoke master script a.bat, it invokes 4 programs, named a1,a2,a3,a4. All these programs at the end of their execution, create an entry into common file a.out. Master script keeps polling a.out until it finds 4 lines in it.


Monday, September 29, 2008

Rcp issue

TERM: Undefined Variable error

This was because user was getting C shell by default. When one does an rsh/rcp the shell that gets assigned in that case is also C shell. While logging into Csh, it was trying to source .tcsh file (which had some errors) which was actually checking for TERM value and since it was not defined, the execution could not complete. Hence the error.

Remedy : Just removed .csh file and it started working.

Thursday, June 19, 2008

Windows Autologon

You want your windows machine to be in logged on state after every reboot instead of it waiting for you to enter login credentials? Here is how you do that:

Make sure that you have these 4 entries in the registy at path:

HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon

autoadminlogon REG_SZ 1
DefaultDomainName REG_SZ *Domain Name*
DefaultPassword REG_SZ *password*
DefaultUserName REG_SZ *username*

Thursday, May 29, 2008

AIX & LIBPATH

The AIX operating system assumes /usr/lib:/lib if LIBPATH is notset, if LIBPATH is set then AIX operating system only uses the pathin theLIBPATH var.

By using changing the assignment statement to
LIBPATH=${LIBPATH:-"/usr/lib:/lib"}:/home/abc/lib
we can make sure the /usr/lib:/lib is always included on theLIBPATH.

Thursday, April 17, 2008

External editing of files in Unix - II

While playing around with ex, I found one another script which is using "ed" to edit the files without actually opening them. Lets say in a text file, you want to insert particular string "Sunday is a Holiday" before every line on which the word Monday is found. Here is how you can write .ed script.

#####SCRIPT BEGINS#####
#!/bin/sh
TERM=vt100
export TERM
ed -s $1 "<<"MYEOF >/dev/null
1
/Monday/
i
Sunday is a holiday
.
w
q
MYEOF
#
exit 0
#####SCRIPT ENDS#####

Run the script as
bash-2.05$./test.ed abc.txt

So many hidden gems there!