Tuesday, May 12, 2015

Write a program to accept username and password as input arguments without displaying the password.

#!/bin/bash

unset PASSWORD
unset CHARCOUNT

echo -n "Login:";
read login_name;
echo -n "Enter password: "

stty -echo

CHARCOUNT=0
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
do
    # Enter - accept password
     if [[ $CHAR == $'\0' ]] ; then
         break
     fi
     # Backspace
     if [[ $CHAR == $'\177' ]] ; then
         if [ $CHARCOUNT -gt 0 ] ; then
            CHARCOUNT=$((CHARCOUNT-1))
            PROMPT=$'\b \b'
            PASSWORD="${PASSWORD%?}"
         else
            PROMPT=''
         fi
     else
         CHARCOUNT=$((CHARCOUNT+1))
         PROMPT='*'
         PASSWORD+="$CHAR"
     fi
done
stty echo

if [[ $login_name == "pearl"  && $PASSWORD == "pearlyperl" ]] ; then
    echo -e "\n Login Successful  :-)  \n";
else
    echo -e "Login Failed \n";
fi


OUPUT:

./login.sh
Login:pearl
Enter password: **********
Login Successful :-)

Write a perl program to read username and password from user without displaying the password string.

#!/usr/local/bin/perl

use strict;
use warnings;

print "Please enter you credentials \n";
print "login:";
my $usr_nm = <STDIN>;
print "password:";
system('stty', '-echo');
my $pswd = <>;
system('stty', 'echo');
print "\n";




Output:
./login.pl
Please enter you credentials
login:user_name
password:

File the frequency of each word and print the output in the decreasing order

#!/usr/local/bin/perl

use strict;
use warnings;
my %count;
my $file = shift or die "Usage: $0 FILE\n";
open my $fh, '<', $file or die "Could not open '$file' $!";
while (my $line = <$fh>) {
        chomp $line;
        foreach my $str (split /\s+/, $line) {
             $count{$str}++;
        }
}

foreach my $str (reverse sort {$count{$a} <=> $count{$b}} keys %count) {
        printf "%-31s %s\n", $str, $count{$str};
}



USAGE: ./count.pl  txt1

OUTPUT:
are                               3
you                              3
Hii                               2
how                             2
hii                                2
i                                   1
Hoe                             1
am                               1
doing                           1
well                             1


Aptitude Question: Find the shortest distance travelled by a ant on the side of the cube to reach from A to H?












































The minimum distance travelled by ant to reach from A to H is = square root of 5 = 2.326

Sunday, May 10, 2015

What is @INC in perl?

The @INC array is a list of directories Perl searches when attempting to load modules. To display the current contents of the @INC array:

perl -e 'print @INC '

The following two methods may be used to append to Perl's @INC array:

1. Add the directory to the PERL5LIB environment variable.
2. Add use lib 'directory'; in your Perl script.

Installing latest version of python on CentOS 5

http://www.hosting.com/support/linux/installing-python-3-on-centosredhat-5x-from-source

The latest release of the python scripting language is Python 3.2. However due to backwards incompatibilities with Python 2, it has not been adopted for CentOS / Redhat Linux 5. T he primary reason for this, is because release of 'yum' (package management) used in EL5 requires Python 2. Because of this, Python cannot be upgraded in place to version 3 without breaking the package manager.
Therefore to use Python 3, it will need to be installed outside of /usr.

Installing

Below is the list of command (with inline comments) on what is required to compile and install Python 3 from source. The installation will be done into the prefix /opt/python3. This will ensure the installation does not conflict with system software installed into /usr.
# Install required build dependencies
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

# Fetch and extract source. Please refer to http://www.python.org/download/releases
# to ensure the latest source is used.
wget http://www.python.org/ftp/python/3.2/Python-3.2.tar.bz2
tar -xjf Python-3.2.tar.bz2
cd Python-3.2

# Configure the build with a prefix (install dir) of /opt/python3, compile, and install.
./configure --prefix=/opt/python3
make
sudo make install
Python 3 will now be installed to /opt/python3.
$ /opt/python3/bin/python3 -V
Python 3.2
Ensure your python 3 scripts and applications query the correct interpreter.
#!/opt/python3/bin/python3