Monday, October 29, 2012


How to write perl shell.

[root@manas ~]cat /tmp/perl_shell
#!/usr/bin/perl

my($output);
do {
    $output = eval;
    if($output =~ /^\s*$/) {
        print "\n$@";
    }
    else {
        print "\n$output";
    }
    print "\nperl_shell>";
} while(<>);


Now you can use perl shell like following :
perl_shell>print "Mandar Pande is a rich man ! "
Mandar Pande is a rich man !

perl_shell>print rand(100)
27.8423844211833

perl_shell>print sqrt(16);
4

simple tab settings in vi/vim


Pretty simple setting ! But I used to forget every time :). 
Python coding would be hell, if this setting is not defined in .vimrc.

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

Guys, Please continue to update .vimrc settings on this post. 

Wednesday, October 24, 2012

lftp file transfer in shell script


script uses conf file as an input:

[mandy@mandy mpix]$ cat lftp.sh 
#!/bin/sh
lftp -f lftp.conf

Input file uses open command on port 2121:

[mandy@mandy mpix]$ cat lftp.conf 
open -p 2121 server_name
mput file1
mput file2
mput file3
bye

Saturday, October 20, 2012

Remote copy using tar over ssh.

$tar -cvf - <source_content> | ssh <user@hostname> "tar -xvf - -C <where_to_copy>"

where,
-  : tells tar to compress and send output of tar to terminal ( at source )
- : tells tar to accept input from terminal and extract into directory specified by -C.

Observations :
It takes at least 40% of less time than actual scp/rcp takes !!

Wednesday, October 17, 2012

tar quick ref

List the files from tar:
------------------------

[mandy@mandy mpande]$ tar -tvf mp.tar
-rw-r----- mandy/mandy 12 2012-08-30 05:13:06 20120830000001.txt
-rw-r----- mandy/mandy 12 2012-08-30 05:13:15 20120830000002
-rw-r----- mandy/mandy 12 2012-08-30 05:13:25 20120830000003.txt
-rw-r----- mandy/mandy 12 2012-08-30 05:11:32 3
-rw-r----- mandy/mandy 12 2012-08-30 05:11:34 4
-rw-r----- mandy/mandy 12 2012-08-30 05:11:37 5

List only *2012* files from tar:
--------------------------------

[mandy@mandy mpande]$ tar -tvf mp.tar --wildcards --no-anchored '*2012*'
-rw-r----- mandy/mandy 12 2012-08-30 05:13:06 20120830000001.txt
-rw-r----- mandy/mandy 12 2012-08-30 05:13:15 20120830000002
-rw-r----- mandy/mandy 12 2012-08-30 05:13:25 20120830000003.txt

Extract files with *2012* from tar:
-----------------------------------

[mandy@mandy mpande]$ tar -xvf mp.tar --wildcards --no-anchored '*2012*'
20120830000001.txt
20120830000002
20120830000003.txt

List files excluding certain pattern:
-------------------------------------

[mandy@mandy mpande]$ tar -tvf mp.tar --exclude *2012*
-rw-r----- mandy/mandy 12 2012-08-30 05:11:32 3
-rw-r----- mandy/mandy 12 2012-08-30 05:11:34 4
-rw-r----- mandy/mandy 12 2012-08-30 05:11:37 5

Extract files excluding certain pattern:
----------------------------------------

[mandy@mandy mpande]$ tar -xvf mp.tar --exclude *2012*
3
4
5

Monday, October 8, 2012

Reset MySQL root Password

Hello,

Many times it happens that we do forget the root password for our mysql instance(whether local or on server if you have rights). It also happens, if we start using a system in which mysql was already there and we don't know whats the root login.

Following steps can help in resetting the root password
Note: You must have root privileges or sudo access to execute the commands :)

1) Stop the mysql daemon:
             like: /etc/init.d/mysql stop
2) Start to MySQL server w/o password:
             use skip-grant-tables option while restarting.
3) Connect to mysql server as the root user:
             mysql -u root
4) Setup new mysql root account password i.e. reset mysql password:
             update user set password=PASSWORD("new password") where User='root';
             (we need to enter password in encrypted form, so using password())
5) Exit and restart the MySQL server:
               like: /etc/init.d/mysql restart





Hope this is useful.

Tuesday, September 25, 2012

Perl smart match operator

Many times, it is very handy to use this operator for comparing arrays, hashes:
use 5.010;

my(@arr_a)=qw (123 abc def ghi);
my(@arr_b)=qw (123 abc def ghi);

print "same2same\n" if (@arr_a ~~ @arr_b);

Tuesday, September 18, 2012

How to setup yum in RHEL 6.0

[root@localhost ~]# cat /etc/yum.repos.d/iso1.repo <-- this name can be anything but in the format *.repo
[iso1.repo]
name=RHEL6
baseurl=ftp://<servername>/<path where repodata directory is available> < ---- [ it means 'repodata' dir should be available in this path ]
enabled=1
gpgcheck=0





==============================================================
Above procedure should be same for other OS.

Thursday, August 30, 2012

I haven't ever heard about open source Search Engine kinda thingy...... :P

Came around this:
The Apache Lucene project develops open-source search software
Apache Lucene
NOOP operator in Bash:

A standard NOOP Operator in bash is colon ":".

Try out:
: echo test or anything :)
[m@root]$ a=1
[m@root]$ b=2
[m@root]$ c=3
[m@root]$ echo $a; : echo $b ; echo $c
1
3


Thanks,
Kailash Chandak

Wednesday, August 29, 2012

Reference Counting Garbage Collection and Circular Reference in PERL

Found very good link about Memory Management in PERL:

http://www.perl.com/pub/2002/08/07/proxyobject.html

Tuesday, August 28, 2012

Wednesday, August 22, 2012

How to identify driver for a give network interface card.


$/sbin/ethtool -i eth0
 
where,
eth0 : Name of Network interface card. 

Tuesday, July 24, 2012

Remove duplicate entries in a perl scalar array

How to remove duplicate entries from perl scalar array.

Ans ==>
my @duplicate_entries = (1,2,3,4,3,22,45,1,22,76,456,12,45,22,876,456,847,14,6,365,7,4,33,5);
my (%hash,@result);
foreach my $x(@duplicate_entries){
    unless($hash{$x}){
        push(@result,$x);
        $hash{$x} = 1;
    }
}

>> @result will be an array of unique elements.

Write a program which will identify public IP address of the current host.

sub get_tm_ip {
    my($host_to_contact) = shift; < ---- it can be any 
                                      pingable hostname
                                      (e.g.  google.com )
    my($port) = 22;
    my $sock = IO::Socket::INET->new(
            PeerAddr=> $host_to_contact,
            PeerPort=> $port,
            Proto   => "tcp");
    my $localip = $sock->sockhost;
    return($localip);
}



Get unique id for a Linux host.

Sometimes we found hostid for two hosts are same. In this case, following command should help us to get unique id for a linux host.

[root@host ~]dmidecode|grep UUID|cut -d: -f2|tr -d ' '
420DE5AC-31FE-5A1A-DDA3-C971902A228D

Inserting column [with value] at a given position in a file using AWK [1 liner]

very useful:
awk -v FS='|' -v OFS='|' '{$3=$3"|"4} 1' 1.txt 

Input:
1|2|3|5 
1|2|3|5 
1|2|3|5 
1|2|3|5 

Output:
1|2|3|4|5 
1|2|3|4|5 
1|2|3|4|5 
1|2|3|4|5 

Wednesday, July 18, 2012

variables inside awk print and sed

Simple & useful: [Many times i forfot this trick]


[mandy ~]$ echo $v1
75

[mandy ~]$ awk -F "|" ' { print $'"$v1"' } ' file_name
mandar
mpande
mp

[mandy ~]$ sed 's/'"$old_val"'/'"$new_val"'/g' file_name

Monday, July 9, 2012

XML Pull Parser

Found a very good article/link in XML Parsing:

Need to check this PullXML Parser thing is available with Perl/Python ? 
This kind of parsing seems to be really faster, efficient than SAX [Off course faster/better than DOM :)].

 XML Pull Parser .
http://www.bearcave.com/software/java/xml/xmlpull.html

Tuesday, May 22, 2012

How to assign a file handle to a subroutine.
==============================

my $FH;
open($FH,"< /tmp/text");
my_print(\*$FH);
exit(0);
sub my_print {
    my $FH = shift;
    my @arr = <$FH>;
    print @arr;
}