Sunday, March 18, 2012

Some interesting Unix commands - PART I


  1.  newname=$(ls *.log 2> /dev/null | sed 's/log/bad/' | sort -n )   --> this will list the files ending with .log and send errors to /dev/null. Then substitute the filename from log to bad and sort the files and store the filenames in the CRON_LOG variable. the actual file names will be untouched. 
    • sed options include: i-> edit files in place. Also, n-> silent output if not using edit file in place.
  2. Another way of doing the same as 1 is : perl -p -i -e 's///g'  *. You can also create a backup using  perl -p -i.bak -e 's///g'  *. This replaces within the existing file.
  3. echo ls *|tr [:lower] [:upper]  --> translates lower filenames to upper
  4. ls *.log|tr -d log ---> Deletes log from the filename
  5. ls file{2,3}*log ---> Only lists files: file2.log and file3.log
  6. ls *.log|cut -d "." -f1 --> prints out all the file names without the ".log"
  7. find /home/ -type f -name "*.log"
  8. newname=$(ls *.bad|cut -d '.' -f1,2,3) and then for z in $newname; do echo $z".log";done ---> the first command truncates .bad from filename abc.2323.gz.bad. The second one traverses through all the newnames and appends .log to them. So, the new filename is now abc.2323.gz.log

How to create keys on ssh

- On your local machine: ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
be:fd:e4:c6:bb:b5:20:4d:ea:9c:76:39:db:7a:70:f3 user@centosvm03


- Server A is the server that you would like to ssh into from your local machine:
cat .ssh/id_rsa.pub | ssh user @A 'cat >> .ssh/authorized_keys'

Thats it!!! Now, you can log into Server A from your local host without a password or with the passphrase if you didn't leave it blank.

How to install new modules from CPAN

There are various ways you can do this. Keep in mind that you need to be logged in as 'root' to be able to install a package from CPAN:
 - Use the cpan shell and install all the modules you would like:
- If you don't want to log into the shell. You can use Perl to download a module from CPAN directly from the command line:

Tuesday, February 21, 2012

How to use the new Modern PERL features

I have PERL 5.8.8 installed on my system and I was trying to use some of the modern PERL features like "say". Of course, I wasn't able to do so. In order to access the features, the first step was to update PERL to a 5.10 or greater version. I installed the latest release available at the time - 5.14.2. I installed this version manually because I read on various sites that it is not advisable to remove the older version of PERL. Mainly because there are a lot of  packages that rely on PERL and you will need to delete all these packages to remove PERL using yum remove perl or any other tool. So, it is just easier to have both the versions. To prevent YUM from modifying your current version, you can type the following in /etc/yum.conf: exclude=perl

Here are the upgrade steps that I took manually:

- wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/perl-5.14.2.tar.gz
- tar -xzvf  perl-5.14.2.tar.gz
- cd /home/user/perl-5.14.2/
     - sh Configure -de Dusethreads : The option "Dusethreads" is to compile the interpreter to support threads.  
     - make
     - make test -> All of them didn't pass but >80% tests passed.
     - make install -> this needs to be run as root.
- Now, /usr/local/bin/perl was on 5.14.2 whereas /usr/bin/perl was still on 5.8.8. I didn't care about it because I used "use feature ':5.10' " in my perl scripts to enable the newer version features but if you care then do the following:
      - mv /usr/bin/perl /usr/bin/perl_5_8_8
      - ln -s /usr/local/bin/perl /usr/bin/perl

Wednesday, February 8, 2012

How to find what PERL modules are installed on my system



1. The easiest way of doing so is by running the command intstmodsh. It provides an interactive interface to query details of installed PERL modules. When you run instmodsh, this is what you get:

At the prompt you can enter 'l' to list all the installed modules.

2. Another way to do this is:
perldoc perllocal


This command will list out all the installed modules along with their path and version information.

3. Yet another way of getting all the modules installed on your system along with the path information, type:
find `perl -e 'print "@INC"' ` -name '*.pm' -print



4. perl -le 'eval "require DBI" and print ${"DBI::VERSION"}'


5. find `perl -e '{print join $/, grep {/[^.]/} @INC}'` -name '*pm'|grep DBI|more


6. perl -MDBI -e'print $_ . " => " . $INC{$_} . "\n" for keys %INC'