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!
Saturday, March 14, 2009
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*
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.
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!
#####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!
External editing of the files in Unix
While debugging a build break, I came across some scripts which were strangely named with extension .ed. Just out of curiosity I opened one of them and I was taken aback by the contents. I just wondered to myself how the hell did I never knew this?
It was using ex (actually just a command line interface to vi), which I now realize is such a powerful tool to edit files without actually opening them in editor window one by one. Let me give you a small example of how to use it. That ease of use will demonstrate its awesome powers.
Lets say you have a file named, abc.txt. The contents of the file are
Solaris
Linux
Hpux
Aix
Windows
Now lets say you want to replace string Windows with Apple. You can write one script named replace.ed which will look something like this:
#####SCRIPT BEGINS#####
#!/bin/sh
TERM=vt100
export TERM
ex -s $1 "<<" MYEOF >/dev/null
set noignorecase
%s/Windows/Apple/g
.
w
q
MYEOF
#
exit 0
##### SCRIPT ENDS#####
Now, all you have to do is to run the script by providing the file to be edited as an input.
bash-2.05$./replace.ed abc.txt
The utility example that I have displayed here is pretty simple, but surely good enough to emphasize the power of this tool. Just imagine how quickly and automatically you can do search and replace text for n number of files by calling this script from some iterative loop.
Happy Editing!
It was using ex (actually just a command line interface to vi), which I now realize is such a powerful tool to edit files without actually opening them in editor window one by one. Let me give you a small example of how to use it. That ease of use will demonstrate its awesome powers.
Lets say you have a file named, abc.txt. The contents of the file are
Solaris
Linux
Hpux
Aix
Windows
Now lets say you want to replace string Windows with Apple. You can write one script named replace.ed which will look something like this:
#####SCRIPT BEGINS#####
#!/bin/sh
TERM=vt100
export TERM
ex -s $1 "<<" MYEOF >
set noignorecase
%s/Windows/Apple/g
.
w
q
MYEOF
#
exit 0
##### SCRIPT ENDS#####
Now, all you have to do is to run the script by providing the file to be edited as an input.
bash-2.05$./replace.ed abc.txt
The utility example that I have displayed here is pretty simple, but surely good enough to emphasize the power of this tool. Just imagine how quickly and automatically you can do search and replace text for n number of files by calling this script from some iterative loop.
Happy Editing!
Subscribe to:
Posts (Atom)