Saturday, May 31, 2014

Reverse print a string the hard way

Script

#!/usr/bin/perl

print ("Enter a string: ");
my $i;
my $string = <STDIN>;
@string=split//,$string;

print ("The reverse of this string is:\n");
for ($i = $#string; $i >= 0; $i--)
{
print $string[$i];
}
print ("\n\n");


Execution

Enter a string: The Internet is a network of networks.
The reverse of this string is:

.skrowten fo krowten a si tenretnI ehT



Enter a string: RATS LIVE ON NO EVIL STAR
The reverse of this string is:

RATS LIVE ON NO EVIL STAR



Enter a string: University of California, Berkeley is one of the top Engineering institutions in the United States.
The reverse of this string is:

.setatS detinU eht ni snoitutitsni gnireenignE pot eht fo eno si yelekreB ,ainrofilaC fo ytisrevinU


How to run a program in Perl

pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / > cat binary-to-decimal
#!/usr/bin/perl

print ("Enter a Binary number: ");
$bina_number = <STDIN>;
chop ($bina_number);

$decimal = oct("0b".$bina_number);

print ("The decimal equivalent of the Binary number $bina_number is:  ", $decimal);
print ("\n");

pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / > ./binary-to-decimal
Enter a Binary number: 1010110101110001101010
The decimal equivalent of the Binary number 1010110101110001101010 is:  2841706
pqr47.us.company.com: / >
pqr47.us.company.com: / >

Parsing the /etc/passwd file and printing the output field by field

Script

#!/usr/bin/perl
open (FILE, '/etc/passwd');

while (<FILE>)
{
chomp;
($username, $password, $userid, $groupid, $realname, $homedirectory, $defaultshell) = split(":");
print "Username: $username\n";
print "Password: $password\n";
print "UserID: $userid\n";
print "GroupID: $groupid\n";
print "Real name: $realname\n";
print "Home directory: $homedirectory\n";
print "Default shell: $defaultshell\n";
print "======================\n";
}

close (FILE);


Execution

xyz78.us.company.com: / >
xyz78.us.company.com: / >
xyz78.us.company.com: / > cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
ssamba:x:499:76:"Ssambad user":/var/empty/ssamba:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
nikki:x:140:140:nikki daemon user:/var/lib/nikki:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
luci:x:141:141:luci high availability management application:/var/lib/luci:/sbin/nologin
escalate:x:60:60::/etc/sysconfig/ha:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
bobby1:x:500:500:BMS:/scratch/bobby1:/bin/bash
rtkit:x:498:496:ReadChKit:/proc:/sbin/nologin
pklmn:x:497:495:PklmnAudio System Daemon:/var/run/pklmn:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
atopi-autoipd:x:170:170:Atopi IPv4LL Stack:/var/lib/atopi-autoipd:/sbin/nologin
aristro-dev:x:501:501::/export/aristro-dev:/bin/bash
xyz78.us.company.com: / >
xyz78.us.company.com: / > ./parse-the-etc-passwd-file
Username: root
Password: x
UserID: 0
GroupID: 0
Real name: root
Home directory: /root
Default shell: /bin/bash
======================
Username: bin
Password: x
UserID: 1
GroupID: 1
Real name: bin
Home directory: /bin
Default shell: /sbin/nologin
======================
Username: daemon
Password: x
UserID: 2
GroupID: 2
Real name: daemon
Home directory: /sbin
Default shell: /sbin/nologin
======================
Username: adm
Password: x
UserID: 3
GroupID: 4
Real name: adm
Home directory: /var/adm
Default shell: /sbin/nologin
======================
Username: lp
Password: x
UserID: 4
GroupID: 7
Real name: lp
Home directory: /var/spool/lpd
Default shell: /sbin/nologin
======================
Username: sync
Password: x
UserID: 5
GroupID: 0
Real name: sync
Home directory: /sbin
Default shell: /bin/sync
======================
Username: shutdown
Password: x
UserID: 6
GroupID: 0
Real name: shutdown
Home directory: /sbin
Default shell: /sbin/shutdown
======================
Username: halt
Password: x
UserID: 7
GroupID: 0
Real name: halt
Home directory: /sbin
Default shell: /sbin/halt
======================
Username: mail
Password: x
UserID: 8
GroupID: 12
Real name: mail
Home directory: /var/spool/mail
Default shell: /sbin/nologin
======================
Username: uucp
Password: x
UserID: 10
GroupID: 14
Real name: uucp
Home directory: /var/spool/uucp
Default shell: /sbin/nologin
======================
Username: operator
Password: x
UserID: 11
GroupID: 0
Real name: operator
Home directory: /root
Default shell: /sbin/nologin
======================
Username: games
Password: x
UserID: 12
GroupID: 100
Real name: games
Home directory: /usr/games
Default shell: /sbin/nologin
======================
Username: gopher
Password: x
UserID: 13
GroupID: 30
Real name: gopher
Home directory: /var/gopher
Default shell: /sbin/nologin
======================
Username: ftp
Password: x
UserID: 14
GroupID: 50
Real name: FTP User
Home directory: /var/ftp
Default shell: /sbin/nologin
======================
Username: nobody
Password: x
UserID: 99
GroupID: 99
Real name: Nobody
Home directory: /
Default shell: /sbin/nologin
======================
Username: dbus
Password: x
UserID: 81
GroupID: 81
Real name: System message bus
Home directory: /
Default shell: /sbin/nologin
======================
Username: rpc
Password: x
UserID: 32
GroupID: 32
Real name: Rpcbind Daemon
Home directory: /var/cache/rpcbind
Default shell: /sbin/nologin
======================
Username: vcsa
Password: x
UserID: 69
GroupID: 69
Real name: virtual console memory owner
Home directory: /dev
Default shell: /sbin/nologin
======================
Username: ssamba
Password: x
UserID: 499
GroupID: 76
Real name: "Ssambad user"
Home directory: /var/empty/ssamba
Default shell: /sbin/nologin
======================
Username: postfix
Password: x
UserID: 89
GroupID: 89
Real name:
Home directory: /var/spool/postfix
Default shell: /sbin/nologin
======================
Username: apache
Password: x
UserID: 48
GroupID: 48
Real name: Apache
Home directory: /var/www
Default shell: /sbin/nologin
======================
Username: ntp
Password: x
UserID: 38
GroupID: 38
Real name:
Home directory: /etc/ntp
Default shell: /sbin/nologin
======================
Username: rpcuser
Password: x
UserID: 29
GroupID: 29
Real name: RPC Service User
Home directory: /var/lib/nfs
Default shell: /sbin/nologin
======================
Username: nfsnobody
Password: x
UserID: 65534
GroupID: 65534
Real name: Anonymous NFS User
Home directory: /var/lib/nfs
Default shell: /sbin/nologin
======================
Username: abrt
Password: x
UserID: 173
GroupID: 173
Real name:
Home directory: /etc/abrt
Default shell: /sbin/nologin
======================
Username: nikki
Password: x
UserID: 140
GroupID: 140
Real name: nikki daemon user
Home directory: /var/lib/nikki
Default shell: /sbin/nologin
======================
Username: haldaemon
Password: x
UserID: 68
GroupID: 68
Real name: HAL daemon
Home directory: /
Default shell: /sbin/nologin
======================
Username: luci
Password: x
UserID: 141
GroupID: 141
Real name: luci high availability management application
Home directory: /var/lib/luci
Default shell: /sbin/nologin
======================
Username: escalate
Password: x
UserID: 60
GroupID: 60
Real name:
Home directory: /etc/sysconfig/ha
Default shell: /sbin/nologin
======================
Username: sshd
Password: x
UserID: 74
GroupID: 74
Real name: Privilege-separated SSH
Home directory: /var/empty/sshd
Default shell: /sbin/nologin
======================
Username: tcpdump
Password: x
UserID: 72
GroupID: 72
Real name:
Home directory: /
Default shell: /sbin/nologin
======================
Username: oprofile
Password: x
UserID: 16
GroupID: 16
Real name: Special user account to be used by OProfile
Home directory: /home/oprofile
Default shell: /sbin/nologin
======================
Username: bobby1
Password: x
UserID: 500
GroupID: 500
Real name: BMS
Home directory: /scratch/bobby1
Default shell: /bin/bash
======================
Username: rtkit
Password: x
UserID: 498
GroupID: 496
Real name: ReadChKit
Home directory: /proc
Default shell: /sbin/nologin
======================
Username: pklmn
Password: x
UserID: 497
GroupID: 495
Real name: PklmnAudio System Daemon
Home directory: /var/run/pklmn
Default shell: /sbin/nologin
======================
Username: gdm
Password: x
UserID: 42
GroupID: 42
Real name:
Home directory: /var/lib/gdm
Default shell: /sbin/nologin
======================
Username: atopi-autoipd
Password: x
UserID: 170
GroupID: 170
Real name: Atopi IPv4LL Stack
Home directory: /var/lib/atopi-autoipd
Default shell: /sbin/nologin
======================
Username: aristro-dev
Password: x
UserID: 501
GroupID: 501
Real name:
Home directory: /export/aristro-dev
Default shell: /bin/bash
======================
xyz78.us.company.com: / >
xyz78.us.company.com: / >
xyz78.us.company.com: / >

Parse network configuration data from network config file

Script

#!/usr/bin/perl
open (FILE, '/etc/sysconfig/network-scripts/ifcfg-eth0');


print "Parameter  $parameter";
print "                        Details  $details\n";
print "==========                         ========\n";

while (<FILE>)
{
chomp;
($parameter, $details) = split("=");
print "$parameter";
print "                         $details\n";
}

close (FILE);


Execution

xyz41.us.company.com: / >
xyz41.us.company.com: / >
xyz41.us.company.com: / > cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO=static
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
UUID="ca8416d3-92ae-4052-a087-823b71798d58"
HWADDR=00:10:E0:42:19:33
IPADDR=10.149.117.241
PREFIX=23
GATEWAY=10.149.117.1
NETMASK=255.255.254.0
DNS1=129.77.231.27
DNS2=142.37.92.121
DNS3=197.116.81.157
DOMAIN=us.company.com
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
xyz41.us.company.com: / >
xyz41.us.company.com: / >
xyz41.us.company.com: / > ./parse-the-etc-sysconfig-network-scripts-ifcfg-eth0-file
Parameter                          Details
==========                         ========
DEVICE                         "eth0"
BOOTPROTO                         static
NM_CONTROLLED                         "no"
ONBOOT                         "yes"
TYPE                         "Ethernet"
UUID                         "ca8416d3-92ae-4052-a087-823b71798d58"
HWADDR                         00:10:E0:42:19:33
IPADDR                         10.149.117.241
PREFIX                         23
GATEWAY                         10.149.117.1
NETMASK                         255.255.254.0
DNS1                         129.77.231.27
DNS2                         142.37.92.121
DNS3                         197.116.81.157
DOMAIN                         us.company.com
DEFROUTE                         yes
IPV4_FAILURE_FATAL                         yes
IPV6INIT                         no
NAME                         "System eth0"
xyz41.us.company.com: / >
xyz41.us.company.com: / >
xyz41.us.company.com: / >

Reverse a string

Script

#!/usr/bin/perl

print ("Enter a statement of your choice:\n");
print ("Press Ctrl D when you are done:\n");


while (<STDIN>)
{
        chop;
        $new = reverse($_);
        print ("The statement printed in reverse is:  ");
        print($new, "\n");
}


Execution

Enter a statement of your choice:
Press Ctrl D when you are done:
abc def ghi jkl mno pqr stu vwx yz
The statement printed in reverse is:  zy xwv uts rqp onm lkj ihg fed cba


Enter a statement of your choice:
Press Ctrl D when you are done:
RATS LIVE ON NO EVIL STAR
The statement printed in reverse is:  RATS LIVE ON NO EVIL STAR


Enter a statement of your choice:
Press Ctrl D when you are done:
Anybody here who is not confused does not know what is going on.
The statement printed in reverse is:  .no gniog si tahw wonk ton seod desufnoc ton si ohw ereh ydobynA

Count the number of lines and words in a file

Script

#!/usr/bin/perl

open(INFILE,$ARGV[0]);

$line_count = 0;
$word_count = 0;

while ($line = <INFILE>)
{

$line_count++;
@words_on_this_line = split(" ",$line);
$word_count += scalar(@words_on_this_line);
}

print "The file $ARGV[0] contains ",$line_count," lines and ",
$word_count, " words\n";


Execution

./file-details /etc/resolv.conf
The file /etc/resolv.conf contains 5 lines and 12 words



./file-details /etc/inittab
The file /etc/inittab contains 26 lines and 149 words


Illustration of push, pop, unshift and shift in an array

Script

#!/usr/bin/perl

@planets = ("Venus","Earth","Mars","Jupiter","Saturn","Uranus");

print "\n";
print "@planets \n\n";
print "Use PUSH to include one entry at the end \n";
push(@planets, "Neptune");
print "@planets \n\n";
print "Use POP to get rid of the last entry at the end \n";
pop(@planets);
print "@planets \n\n";
print "Use UNSHIFT to include one entry at the beginning of the array \n";
unshift(@planets, "Mercury");
print "@planets \n\n";
print "Use SHIFT to get rid of the first entry at the beginning of the array \n";
shift(@planets);
print "@planets \n";


Execution


Venus Earth Mars Jupiter Saturn Uranus

Use PUSH to include one entry at the end
Venus Earth Mars Jupiter Saturn Uranus Neptune

Use POP to get rid of the last entry at the end
Venus Earth Mars Jupiter Saturn Uranus

Use UNSHIFT to include one entry at the beginning of the array
Mercury Venus Earth Mars Jupiter Saturn Uranus

Use SHIFT to get rid of the first entry at the beginning of the array
Venus Earth Mars Jupiter Saturn Uranus

List numbers in a specific range

Script

#!/usr/bin/perl

print ("Enter the starting number: ");
$start = <STDIN>;
chop ($start);
print ("Enter the last number: ");
$last = <STDIN>;
chop ($last);

@list = ($start..$last);
$count = 0;

print ("Here is the list: \n");

while ($list [$count] != 0 || $list [$count] == -1 || $list [$count + 1] ==1 )
{
        print ("$list[$count] \n");
        $count++;
}

Execution



Enter the starting number: -20
Enter the last number: 17
Here is the list:
-20
-19
-18
-17
-16
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Getting familiar with arrays

Script

#!/usr/bin/perl

@numbers = (1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80);

print "\n";
print "All numbers in a horizontal line: \n";
print "@numbers \n\n";
print "Print from numbers[0] to numbers[4]\n";
print "$numbers[0] \n";
print "$numbers[1] \n";
print "$numbers[2] \n";
print "$numbers[3] \n";
print "$numbers[4] \n";
print "Print from numbers[5] to numbers[10]\n";
print "$numbers[5] \n";
print "$numbers[6] \n";
print "$numbers[7] \n";
print "$numbers[8] \n";
print "$numbers[8] \n";
print "$numbers[8] \n";
print "$numbers[9] \n";
print "$numbers[10] \n";
print "Print from numbers[11] to numbers[15]\n";
print "$numbers[11] \n";
print "$numbers[12] \n";
print "$numbers[13] \n";
print "$numbers[14] \n";
print "$numbers[15] \n";
print "Number of entries in the array: ";
$numberofentries = $#numbers + 1;
print "$numberofentries \n";


Execution

All numbers in a horizontal line:
1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80

Print from numbers[0] to numbers[4]
1
2
3
4
5
Print from numbers[5] to numbers[10]
6
7
8
9
9
9
10
20
Print from numbers[11] to numbers[15]
30
40
50
60
70
Number of entries in the array: 17

Password checker

Script

#!/usr/bin/perl

print ("Enter the secret password: ");
$password = "aristocrat123";
$intake = <STDIN>;
chop ($intake);
$outcome = $intake eq $password ?
        "Yes, the password is correct !!! \n" :
        "No, the password is not correct \n";
print ($outcome);

Execution

Enter the secret password: bdtsydo93
No, the password is not correct

Enter the secret password: rvrevre9
No, the password is not correct

Enter the secret password: aristocrat123
Yes, the password is correct !!!

Count up to

Script

#!/usr/bin/perl

$count = 0;
print ("$count \n")
while ($count++ <25);

Execution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Angle between the hands of a clock

Script

#!/usr/bin/perl

print ("Enter the hour: ");
$hour = <STDIN>;
print ("Enter the minute: ");
$minute = <STDIN>;
$angle = $hour * 30 - $minute * 5.5;
print ("The angle between the hour  hand and the minute hand is $angle degrees \n");

Execution

Enter the hour: 3
Enter the minute: 0
The angle between the hour  hand and the minute hand is 90 degrees

Enter the hour: 6
Enter the minute: 0
The angle between the hour  hand and the minute hand is 180 degrees


Enter the hour: 6
Enter the minute: 15
The angle between the hour  hand and the minute hand is 97.5 degrees

Enter the hour: 6
Enter the minute: 30
The angle between the hour  hand and the minute hand is 15 degrees

Enter the hour: 4
Enter the minute: 15
The angle between the hour  hand and the minute hand is 37.5 degrees

Enter the hour: 4
Enter the minute: 20
The angle between the hour  hand and the minute hand is 10 degrees