copy file from host to the docker container:
Steps:
1: Get container name or short container id :
1a. docker ps
1b. Get full container id
docker inspect -f '{{.Id}}' SHORT_CONTAINER_ID-or-CONTAINER_NAME
1c. copy file :
sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE
EXAMPLE :
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
450b4b6a87e7 amit/ssh:latest /usr/sbin/sshd -D cranky_pare
$docker inspect -f '{{.Id}}' cranky_pare
or
$docker inspect -f '{{.Id}}' 450b4b6a87e7
450b4b6a87e734ba68d1c077de5d3d9ac617b1b9b6e829fe28c917abf1dce44f
From your docker server:
$ sudo cp file.txt /var/lib/docker/aufs/mnt/450b4b6a87e734ba68d1c077de5d3d9ac617b1b9b6e829fe28c917abf1dce44f/root/file.txt
Disclaimer: Its a collection from lots of other site(s) and few of my notes. I would also like to declare that I am not owning lots of its content. Please feel free to contact me directly if you want me to remove any of your content, that you don't want to share to other through this blog.
Wednesday, 19 November 2014
docker networking with hack part1
I found the easy way of having multiple container running on the same host, bind with the host IP, then assign multiple IP the same interface of the host.
1. How do I bind multiple IP to the same interface:
amund@prod-docker-app01:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 172.22.19.75
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
# bind-ed one assigned IP
auto eth0:0
iface eth0:0 inet static
address 172.22.19.76
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
# bind-ed second assigned IP
auto eth0:1
iface eth0:1 inet static
address 172.22.19.77
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
# bind-ed thired assigned IP
auto eth0:2
iface eth0:2 inet static
address 172.22.19.78
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
2. If you are using remote host, good to reboot once and check your network update.
1. How do I bind multiple IP to the same interface:
amund@prod-docker-app01:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 172.22.19.75
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
# bind-ed one assigned IP
auto eth0:0
iface eth0:0 inet static
address 172.22.19.76
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
# bind-ed second assigned IP
auto eth0:1
iface eth0:1 inet static
address 172.22.19.77
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
# bind-ed thired assigned IP
auto eth0:2
iface eth0:2 inet static
address 172.22.19.78
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in
2. If you are using remote host, good to reboot once and check your network update.
Thursday, 30 October 2014
Docker design doc1
A sample docker design doc, how we can implement docker in the production environment. Will update other docker command later in the blog.
The ports are just for example. Note: we can run the keepalived and haproxy on the same server where docker is running, just for simplicity I draw at different server.
Following image's original pdf link:
https://drive.google.com/file/d/0B02LZ59YLdSdaTBlVGdmX0RlWU0/view?usp=sharing
Following image's original pdf link:
https://drive.google.com/file/d/0B02LZ59YLdSdaTBlVGdmX0RlWU0/view?usp=sharing
Thursday, 23 October 2014
vimrc
my quick ~/.vimrc
set hlsearch
syntax enable
set tabstop=4
set softtabstop=4
set expandtab
set showcmd
set cursorline
filetype indent on
set showmatch
set incsearch
set hlsearch
syntax enable
set tabstop=4
set softtabstop=4
set expandtab
set showcmd
set cursorline
filetype indent on
set showmatch
set incsearch
Wednesday, 8 October 2014
nc - [ netcat ] - unix command
nc
0)
Copy a complete directory [ lets say a mysql data " but you should not take a running mysql data " ] Go to destination computer's destination EMPTY directory where you want to dump the data and run:[[[ nc -l 3333 | tar -zxvf - ]]] - Note you can use any free port at the place of 3333 now go to the source computer and type: [[[ tar cvz <directory> | nc destination_hostname 3333 ]]] 1) Destination host: nc -lp [3333] > [file] [ listen to port 3333 and what ever comes from that port put that to <<file>> Sources host: cat <<file>> | nc -w 1 IP <<3333>> [ to transfer a <<file>> of course host with netcat with having timeout of 1 sec " -w 1 " and on port of 33332) man nc: -l Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host. It is an error to use this option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored. -p source_port Specifies the source port nc should use, subject to privilege restrictions and availability. It is an error to use this option in conjunction with the -l option. -w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag. The default is no timeout. 3) CLIENT/SERVER MODEL It is quite simple to build a very basic client/server model using nc. On one console, start nc listening on a specific port for a connection. For example: $ nc -l 1234 nc is now listening on port 1234 for a connection. On a second console (or a second machine), connect to the machine and port being listened on: $ nc 127.0.0.1 1234 There should now be a connection between the ports. Anything typed at the second console will be concatenated to the first, and vice-versa. After the connection has been set up, nc does not really care which side is being used as a ‘server’ and which side is being used as a ‘client’. The connection may be terminated using an EOF (‘^D’). 4) DATA TRANSFER The example in the previous section can be expanded to build a basic data transfer model. Any information input into one end of the connection will be output to the other end, and input and output can be easily captured in order to emulate file transfer. Start by using nc to listen on a specific port, with output captured into a file: $ nc -l 1234 > filename.out Using a second machine, connect to the listening nc process, feeding it the file which is to be transferred: $ nc host.example.com 1234 < filename.in After the file has been transferred, the connection will close automatically. 5) TALKING TO SERVERS It is sometimes useful to talk to servers “by hand” rather than through a user interface. It can aid in troubleshooting, when it might be necessary to verify what data a server is sending in response to commands issued by the client. For example, to retrieve the home page of a web site: $ echo -n "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80 Note that this also displays the headers sent by the web server. They can be filtered, using a tool such as sed(1), if necessary. More complicated examples can be built up when the user knows the format of requests required by the server. As another example, an email may be submitted to an SMTP server using: $ nc localhost 25 << EOF HELO host.example.com MAIL FROM: <user@host.example.com> RCPT TO: <user2@host.example.com> DATA Body of email. . QUIT EOF 6) PORT SCANNING It may be useful to know which ports are open and running services on a target machine. The -z flag can be used to tell nc to report open ports, rather than initiate a con- nection. For example: $ nc -z host.example.com 20-30 Connection to host.example.com 22 port [tcp/ssh] succeeded! Connection to host.example.com 25 port [tcp/smtp] succeeded! The port range was specified to limit the search to ports 20 - 30. Alternatively, it might be useful to know which server software is running, and which versions. This information is often contained within the greeting banners. In order to retrieve these, it is necessary to first make a connection, and then break the connection when the banner has been retrieved. This can be accomplished by specifying a small timeout with the -w flag, or perhaps by issuing a "QUIT" command to the server: $ echo "QUIT" | nc host.example.com 20-30 SSH-1.99-OpenSSH_3.6.1p2 Protocol mismatch. 220 host.example.com IMS SMTP Receiver Version 0.84 Ready 7) EXAMPLES Open a TCP connection to port 42 of host.example.com, using port 31337 as the source port, with a timeout of 5 seconds: $ nc -p 31337 -w 5 host.example.com 42 Open a UDP connection to port 53 of host.example.com: $ nc -u host.example.com 53 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the IP for the local end of the connection: $ nc -s 10.1.2.3 host.example.com 42 Create and listen on a Unix Domain Socket: $ nc -lU /var/tmp/dsocket Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, port 8080.
This example could also be used by ssh(1); see the
ProxyCommand directive in ssh_config(5)
for more information. $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42 8) Minimal web-server example: You can set up netcat to act as a very basic webserver which can just serve one file: while `netcat -lp 8080 -c 'echo HTTP/1.0 200 OK';echo;cat file`;do;done 9) local port forwarding: This command would forward every request on port 8080 to port 80: while `netcat -lp 8080 -c 'netcat localhost 80'`;do;done 10) getting telnet command output using nc: [ example memcache ] printf 'stats\n' | nc 127.0.0.1 11211 | grep limit_maxbytes |
Tuesday, 26 August 2014
quick perl note.
1.
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
# This will print "Hello, World"
print "Hello, world\n";
print("Hello, world\n");
print "Hello, world to all\n";
2.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
$a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';
3.
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is case of single quote so variable value will not be
interpolated. For example value of a = $a
EOF
print "$var\n";
4.
#!/usr/bin/perl -w
$result = "This is \"number\"\n";
print "$result",' adding...';
print "\n\$result\n";
5.
#!/usr/bin/perl
use strict;
use warnings;
my $name = "A useful tool in natural language processing is concordance. This allows a specific string to be displayed in its immediate context whereever it appears in a text. For example, a concordance program identifying the target string the might produce some of the following output. Notice how the occurrences of the target string line up vertically. ";
my @person = split(/ /, $name);
#print "$person[0]\n"
foreach my $val (@person) {
print "$val\n"
}
exit 0;
6.
#!/usr/bin/perl -w
use strict;
# multi dimention array:
my @array1=(
# ["1","2", ["Amit"]],
["1","2"],
["3","4"],
["5","6"]
);
#print "$array1[0][2][0]\n";
print "$array1[0][0]\n";
# multi dimention array referance:
my $array1ref = [
["1","2"],
["3","4"],
["5","6"]
];
print "$array1ref->[0][0]\n";
7.
#!/usr/bin/perl
use strict;
use warnings;
open (fh2, "file1") || die "Error opening file1: $!";
#open (fh2, "/etc/passwd") || die "Error opening file1: $!";
my @array1;
while (<fh2>){
push @array1, [ split ]; # push function to push the data in array1 from $_
#push @array1, [ split/:/];
}
foreach (@array1){
print "@$_[0]\n";
print "@$_[1]\n";
#print "@$_\n";
}
8.
#!/usr/bin/perl
use strict;
use warnings;
open (fh2, "passwd") || die "Error opening file1: $!";
my @array1;
while (<fh2>){
push @array1, [ split/:/];
}
foreach (@array1){
print "@$_";
}
print "\nPrinting an extra line: $array1[0][0]\n";
9.
#!/usr/bin/perl
# Author: Amit Mund
# Date: 17.07.2014
# Purpose: Explain Lists, Slices, Ranges, etc.
use warnings;
use strict;
# qw : quote word
#Scalars(single values), Arrays(lists), Hashes(key/value pairs)
print (qw(Boston Charlotte newark miami)); # This is removing all the blank space.
print "\n";
print (qw(Boston Charlotte newark miami)[0,3]); # will print 0th and 3rd index value. [Bootonmiami]
print "\n";
print (qw(Boston Charlotte newark miami)[0..2]); # will print the index values from 0 to 3
print "\n\n";
##
my @uscities = qw(Boston Charlotte newark miami Austin Dallas Houston);
print "@uscities[1..$#uscities]\n";
my @eastcoastcities = @uscities[0..3,5];
print ("East Cost Cities: ","@eastcoastcities\n");
print ("Other Cities:", "@uscities[4..$#uscities]\n\n");
10.
#!/usr/bin/perl -wl
use strict;
# you can use the same name in a different type of variables
#scalars = single values ($)
#Arrays = lists of scalars (@)
# Hashes = key/values pairs
# NOTE: $fname != @fname and we can have both at the same time.
my $carmanufactures = "This is scalar values";
my @carmanufactures = ("Honda","Toyota","Nissan", "Lexus","BMW"); # begining with 0
print "$carmanufactures";
print "$carmanufactures[0]";
print "$carmanufactures[1]";
print "$carmanufactures[2]";
print "$carmanufactures[3]";
# perl keep the working data into memory.
print "This arrays contents: 0 to $#carmanufactures, mean total $#carmanufactures+1";
# Note: leangth of arrays using $#arrayname
print "Here is the full arrays: @carmanufactures[0,1,2,3,4]";
# following also works
print "Here is the full arrays: @carmanufactures[0..4]";
# I believe the best way is following in these 3.
print "Here is the full arrays: @carmanufactures[0..$#carmanufactures]";
11.
#!/usr/bin/perl
use warnings;
use strict;
# NOTE: For interger use ==, <, >, <=, >=
if (1 < 2 && (5 < 4 || 5 > 4)) {
print "I am at if block.\n";
}
## string test:
# NOTE: for string eq, ne
# if, elsif, else, unless [elsif, to use within a sub-if block.]
my $fname = "amit";
my $name = "amit";
if ( $name ne $fname) {
print "$fname is same as $name\n";
}
else {
print "I am at else block.\n";
}
# unless is for negetation:
my $value1 = "1";
my $value2 = "2";
unless ($value1 == $value2) {
print "$value1 is not equal to $value2\n";
}
# one line if test:
print "one liner if\n" if $value1 != $value2;
#END
12. fileio1.pl
#!/usr/bin/perl
use warnings;
use strict;
# open function for file handel.
# die function to cache any error.
# $! is the perl variable to keep the error.
my $min = 1;
my $max = 20;
my $fname = "Amit mund";
my $dob = "23/04/1982";
# defining OUTFILE as a fh.
open (OUTFILE, ">data1") || die "problems: $!"; # $! perl inbuilt variable for error return.
for ($min..$max) {
print OUTFILE "$fname and DOB = ";
print OUTFILE "$dob\n"
}
#END
13. fileio2.pl
#!/usr/bin/perl
use warnings;
use strict;
#my $OUT = "OUTFILE";
#my $filename = "/proc/cpuinfo";
my $filename = "data1";
open (INFILE, "$filename") || die "problems: $!";
#while (<INFILE>){
# print "$_";
#}
my @data1contains = <INFILE>; # assoicate a file to an array.
foreach (@data1contains){
print "$_";
}
#END
14. FileIO3.pl
#!/usr/bin/perl
use warnings;
$IN = "INFILE";
$OUT = "OUTFILE";
$filenamein = "/proc/cpuinfo";
$filenameout = "data2";
open ($IN, "$filenamein") || die "error while opening the file: $!";
open ($OUT, ">$filenameout") || die "error while writing the file: $!";
@data1contents = <$IN>;
foreach (@data1contents){
if (/^processor/ || /^core id/ || /model name/){ # Nesting an if statement with regex
#print $OUT "$_";
s/model name/cpu/; # Replace "model name with cpu"
@cur_data=$_;
print $OUT "@cur_data";
}
}
#END
15. File04.pl
#!/usr/bin/perl
use warnings;
my $PROCESS;
open ($PROCESS, "ps aux | grep -i apache |") || die "process error $!";
@PROCESS_DATA = <$PROCESS>;
#print "@PROCESS_DATA"
foreach (@PROCESS_DATA){
print "$_"
}
# END
16. fileIO04A.pl
#!/usr/bin/perl
use warnings;
my $PROCESS;
open ($PROCESS, "ps aux|") || die "process error $!"; # need to end with | pipe as per the input for $PROCESS.
@PROCESS_DATA = <$PROCESS>;
foreach (@PROCESS_DATA){
if (/apache/){ # <----- Regex.
print "$_"
}
}
17. FileIO4_B.pl
#!/usr/bin/perl
use warnings;
$FH = "FileReadWrite";
$FILENAME = "data1";
$APPEND_OF_FILE = "Appended string\n";
# +< for read and append mode, and it must be in quote.
open ($FH, "+<$FILENAME") || die "file error: $!";
@dataOfFile = <$FH>;
foreach (@dataOfFile){
#print $FH "$_";
}
print $FH "$APPEND_OF_FILE";
18. FileIO05.pl
#!/usr/bin/perl
use warnings;
#$DIR = "/home/amit/Documents/perl";
$DIR = "/etc";
$DH = "Handle";
opendir ($DH, "$DIR") || die "Error opening dir: $!";
@dirlist = readdir($DH); # list the first level of contents.
foreach (@dirlist){
#if (/^[x-z]/){ # regex of start with x-z letters.
# print "$_\n";
#}
print "$_\n";
}
#closedir ($DH); # Good to close the handler.
#@dirlist = `ls -A $DIR`;
#foreach (@dirlist){
# print "$_\n";
#}
#END
19.
#!/usr/bin/perl
use warnings;
#use strict;
#my $mem = perl -wln -e '@mem1 = split /:/, $_; if (/DirectMap2M/){print $mem1[1]} ;' /proc/meminfo
my $MEM_FH1 = "memory_FH";
my $mem_file = "/proc/meminfo";
open ($MEM_FH1, "$mem_file") || die "problem opening file: $!";
my @mem_data_contains = <$MEM_FH1>;
foreach(@mem_data_contains){
if (/^MemFree/){
#my @mem_value = split /:/, @mem_data_contains;
my @mem_free_value = split /\s+/,$_;
my @mem_free_value1 = $mem_free_value[1] * 1024; # in bite conversion
print "Free Memory = @mem_free_value1\n";
#print @mem_data_contains;
}
}
foreach(@mem_data_contains){
if (/^MemTotal/){
#my @mem_value = split /:/, @mem_data_contains;
my @mem_total_value = split /\s+/,$_;
my @mem_total_value1 = $mem_total_value[1] * 1024; # in bite conversion
print "Total Memory = @mem_total_value1\n";
#print @mem_data_contains;
}
}
#my $Free_percent =
20.
#!/bin/bash/perl
use strict;
use warnings;
#my $min = 1;
#my $max = 10;
#my $PROD_NAME = "Testing the perl foreach loop.";
my @array1 = ("Amit", "Kumar", "Mund", "Testing");
foreach (@array1) { # just need to pass the array value.
# body...
print "$_ "; # $_ is an internal variable that keep the complete value.
}
$#array1 +=1;
print "\nTotal arrays element = $#array1";
print "\n";
21.
#!/usr/bin/perl -wl
use strict;
# Note: in hash it need to even number of objects.
my %make_model = (
"Honda","city",
"Maruti","swift"
);
print $make_model{"Maruti"};
#### The nice way of writing the hash:
my $player = "Venus";
my %player_country = (
Venus => "USA",
Sharapova => "Russia",
);
printf "$player represents: ";
print $player_country{"$player"};
###
my @num_of_players_keys = keys %player_country;
my @num_of_players_values = values %player_country;
print "The keys are: @num_of_players_keys[0..$#num_of_players_keys]";
print "The values are: @num_of_players_values[0..$#num_of_players_values]";
22.
#!/usr/bin/perl
#
# The traditional first program.
# Strict and warnings are recommended.
use strict;
use warnings;
# Print a message.
print "Content-type: text/html\n\n";
print "Hello, World!\n";
=begin comment
This is all part of multiline comment.
You can use as many lines as you like
These comments will be ignored by the
compiler until the next =cut is encountered.
=cut
=begin comment
for multiline comment it start with: " =begin" and end with "cut" after
the =
=cut
print "Hello, world\n";
print 'Hello, world\n';
23.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
my $str = <<EOM;
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
EOM
print $str
24. perldb.pl
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
#Setp1 - creating connection object
my $dsn = 'DBI:mysql:thenewboston'; # module:db_engine:db_name
my $user = 'root';
my $pass = 'YourPassword';
my $host = 'localhost';
my $conn = DBI->connect($dsn,$user,$pass) || die "Error connecting" . DBI->errstr;
# step 2 - define query
my $query1 = $conn->prepare('SELECT id,name FROM user') || die "Error preparing query" . $conn->errstr;
# setp 3 - execute query
$query1->execute || die "Error executing query" . $conn->errstr;
# step 4 - return results
my @results;
while (@results = $query1->fetchrow_array()) {
my $id = $results[0];
my $name = $results[1];
#my $name = $results[2];
print "$id,$name\n"
}
# trying for another query too:
my $query2 = $conn->prepare('SHOW TABLES') || die "Error preparing query" . $conn->errstr;
$query2->execute || die "Error executing query" . $conn->errstr;
my @results2;
print "\nFollowing are the tables at thenewboston DB:\n";
while (@results2 = $query2->fetchrow_array()) {
my $tables = $results2[0];
print "$tables\n"
}
25.
#!/usr/bin/perl
use warnings;
use strict;
my @array1 = ("1","2","3","4");
print "@array1\n";
# pop function, to remove the last value.
my $ppop = pop @array1; # over here $ppop will be carring the removed value.
print "@array1\n";
print "$ppop\n"; # printing the remoed value.
# NOTE: if you don;t want you can just pop the @array1 and print the same again and it will remove the last one.
pop @array1;
print "@array1\n";
#my $ppop = pop @array1;
#print "@array1\n";
#print "$ppop\n";
#END
26.
#!/usr/bin/perl
use warnings;
use strict;
my @array1 = ("1","2","3","4");
print "Original array1 values: @array1\n";
# pop function, to remove the last value.
my $ppop = pop @array1; # over here $ppop will be carring the removed value.
print "Value after doing a pop: @array1\n";
print "What is ppop is carring now: $ppop\n"; # printing the remoed value.
# Now push the another value;
push @array1, $ppop; # adding back the removed one.
print "\nValue after doing a push of the ppop value: @array1\n";
# adding one more value.
push @array1, "5";
print "Pushing one more eliment: @array1\n";
# adding one more value.
push @array1, "Amit";
print "Pushing one more eliment: @array1\n\n";
# Adding the element(s) at the beginning of the array.
my $fname = "Amit";
my $mname = "Kumar";
my $lname = "Mund";
unshift (@array1, "$fname", "$mname", "$lname");
print "@array1\n";
# Removing the 1st element from the array
my $s_shift =shift @array1;
print "@array1\n";
print "$s_shift\n";
## "sort", For sorting the string.
my @array2 = ("Amit","kumar", "Mund", "Bitu");
my @sort_array2 = sort @array2;
print "@sort_array2\n";
# We can use the same array name after doing sort and assign the same.
@array2 = sort @array2;
print "@array2\n";
#END
27.
#!/usr/bin/perl
use warnings;
use strict;
# @ARGV variable [ array type spical variable ]
# print "$ARGV[0]\n";
# print "Trying to print all the input argument: @ARGV[0..$#ARGV]\n";
my $REQ = 3;
my $BADARGS = 165;
$#ARGV += 1; # NOTE: No need to use my or our for ARGV. If you give, it will show error.
unless ($#ARGV == 3){
print "$0 requires $REQ variables: ";
print "ERROR! Insufficent argument.\n";
exit "$BADARGS";
}
print "Correct number of argument $#ARGV has been passed.\n";
print "$ARGV[0] $ARGV[1] $ARGV[2]\n";
print "printing with array type : @ARGV[0..$#ARGV]\n"; # saw the same output at training too.
# perldoc -f exit; for more help.
#END
28.
#!/usr/bin/perl -wl
# w: option for the warning.
# l: option for the line group.
$fname = "Amit";
$lname = "Mund";
$fullname = "$fname $lname";
print "$fname $lname";
print "$fullname";
# NOTE: Must end with a semi-collon;
29.
#!/usr/bin/perl
# split, join and input files from shell.
use warnings;
use strict;
while (<>){ # reading by a stdin. [ file name as arg ]
my @username = split /:/, $_;
if(/false/){
print "$username[0]\n";
}
}
# perl -wln -e '@array1 = split /:/, $_; print $array1[0]' passwdFile
# perl -wln -e '@mem1 = split /:/, $_; if (/DirectMap2M/){print $mem1[1]} ;' /proc/meminfo
# 8226816 kB
#END
30.
#!/usr/bin/perl -w
use strict;
printf "Please enter your name: ";
my $fname = <STDIN>; # For standard input, you can declare the variable at the same time.
chomp($fname); # To remove the last newline character.
print "\n***************************************";
print "\nHello $fname, how are you doing today?\n";
print "***************************************\n\n";
31.
#!/usr/bin/perl -w
use strict;
testsub(); # This is how to call a sub-routines, Its the same as function
# and its not needed to be after declating this sub-routines.
sub testsub { # Need to start with "sub"
my $name = "Amit Mund";
print "Hello $name\n";
}
# Note: you can pass the argument too in the sub-routines
testsub1("Amit Mund", "Bitu");
sub testsub1 {
print "Following is an example of getting the value as argument from testsub2\n";
print "Hello: $_[0]\n"; # $_ is the default place holder.
}
# Two argument in the "foreach" loop and printing Hello Amit and Hello Bitu.
testsub2 ("Amit","Bitu","Tutu");
sub testsub2 {
foreach (@_){
print "Hello $_\n";
}
}
# NOTE: @_ is the for all the array value passed on the argument.
testsub3 ("Amit","Bitu","Tutu");
sub testsub3 {
print "Hello @_\n";
}
32.
#!/usr/bin/perl
use warnings;
use strict;
# file handling:
open (fh1, ">> sub2.log") || die "Error opening the file: $!";
my $etcdir = `ls -l /etc/passwd`;
chomp ($etcdir); # to delete extra newline.
my $message = "Launching sub2.pl";
log_message("$message");
log_message("$etcdir");
log_message(`uptime; who`);
sub log_message{
my $current_time = localtime;
print "$current_time - $_[0]","\n";
print fh1 "$current_time - $_[0]", "\n";
}
#perl -e '$decimal=10, print $decimal;'
#NOTE: perl -e always need to be in single quote, e.g:
# perl -l -e '$current_time = localtime, print $current_time;'
33.
#!/usr/bin/perl
use warnings;
use strict;
# Printing the arguments.
#print "$ARGV[0]\n";
#print "Trying to print all the input argument: @ARGV[0..$#ARGV]\n";
# printing value at hex.
#perl -wl -e '$hex = sprintf("0x%x",15), print $hex;'
#######################################
# NOTE: Try to always use the variable.
######################################
# Following is the "c" style for loop.
#my $min = 1;
#my $max = 100;
#my $i;
#
#for ($i=$min; $i<=$max; $i++){
#
# print "$i\n";
#}
my $min = 1;
my $max = 100;
my $i=0;
for ($min..$max){
print "For loop test\n";
}
# can put the same in a single line too:
for ($min..$max){$i++; print "$i\n";}
#END
34.
#!/usr/bin/perl
use warnings;
#use strict;
my $IN = INFILE;
my $infile = passwdFile;
open ($IN, "$infile") || die "problems: $!";
while (<$IN>){
print "$_";
}
#END
35.
#!/usr/bin/perl
use warnings;
@mem = split /:/, $_; if (/DirectMap2M/){print "$mem1[1]";} ;' /proc/meminfo
print @mem;
@myls = `ls -l`;
print @myls;
36.
#!/usr/bin/perl
use warnings;
use strict;
print "Content-type: text/html\n\n";
# syntax:
#while (condition){
#body;
#}
my $value1 = 1;
my $value2 = 20;
while ($value1 <= $value2){
print "$value1,";
$value1 +=2;
}
print "\n";
# syntax:
#until (condition){
#body;
#}
my $value11 = 1;
my $value22 = 20;
until ($value22 <= $value11){
print "$value22,";
$value22 -=2;
}
print "\n";
#END
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
# This will print "Hello, World"
print "Hello, world\n";
print("Hello, world\n");
print "Hello, world to all\n";
2.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
$a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';
3.
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is case of single quote so variable value will not be
interpolated. For example value of a = $a
EOF
print "$var\n";
4.
#!/usr/bin/perl -w
$result = "This is \"number\"\n";
print "$result",' adding...';
print "\n\$result\n";
5.
#!/usr/bin/perl
use strict;
use warnings;
my $name = "A useful tool in natural language processing is concordance. This allows a specific string to be displayed in its immediate context whereever it appears in a text. For example, a concordance program identifying the target string the might produce some of the following output. Notice how the occurrences of the target string line up vertically. ";
my @person = split(/ /, $name);
#print "$person[0]\n"
foreach my $val (@person) {
print "$val\n"
}
exit 0;
6.
#!/usr/bin/perl -w
use strict;
# multi dimention array:
my @array1=(
# ["1","2", ["Amit"]],
["1","2"],
["3","4"],
["5","6"]
);
#print "$array1[0][2][0]\n";
print "$array1[0][0]\n";
# multi dimention array referance:
my $array1ref = [
["1","2"],
["3","4"],
["5","6"]
];
print "$array1ref->[0][0]\n";
7.
#!/usr/bin/perl
use strict;
use warnings;
open (fh2, "file1") || die "Error opening file1: $!";
#open (fh2, "/etc/passwd") || die "Error opening file1: $!";
my @array1;
while (<fh2>){
push @array1, [ split ]; # push function to push the data in array1 from $_
#push @array1, [ split/:/];
}
foreach (@array1){
print "@$_[0]\n";
print "@$_[1]\n";
#print "@$_\n";
}
8.
#!/usr/bin/perl
use strict;
use warnings;
open (fh2, "passwd") || die "Error opening file1: $!";
my @array1;
while (<fh2>){
push @array1, [ split/:/];
}
foreach (@array1){
print "@$_";
}
print "\nPrinting an extra line: $array1[0][0]\n";
9.
#!/usr/bin/perl
# Author: Amit Mund
# Date: 17.07.2014
# Purpose: Explain Lists, Slices, Ranges, etc.
use warnings;
use strict;
# qw : quote word
#Scalars(single values), Arrays(lists), Hashes(key/value pairs)
print (qw(Boston Charlotte newark miami)); # This is removing all the blank space.
print "\n";
print (qw(Boston Charlotte newark miami)[0,3]); # will print 0th and 3rd index value. [Bootonmiami]
print "\n";
print (qw(Boston Charlotte newark miami)[0..2]); # will print the index values from 0 to 3
print "\n\n";
##
my @uscities = qw(Boston Charlotte newark miami Austin Dallas Houston);
print "@uscities[1..$#uscities]\n";
my @eastcoastcities = @uscities[0..3,5];
print ("East Cost Cities: ","@eastcoastcities\n");
print ("Other Cities:", "@uscities[4..$#uscities]\n\n");
10.
#!/usr/bin/perl -wl
use strict;
# you can use the same name in a different type of variables
#scalars = single values ($)
#Arrays = lists of scalars (@)
# Hashes = key/values pairs
# NOTE: $fname != @fname and we can have both at the same time.
my $carmanufactures = "This is scalar values";
my @carmanufactures = ("Honda","Toyota","Nissan", "Lexus","BMW"); # begining with 0
print "$carmanufactures";
print "$carmanufactures[0]";
print "$carmanufactures[1]";
print "$carmanufactures[2]";
print "$carmanufactures[3]";
# perl keep the working data into memory.
print "This arrays contents: 0 to $#carmanufactures, mean total $#carmanufactures+1";
# Note: leangth of arrays using $#arrayname
print "Here is the full arrays: @carmanufactures[0,1,2,3,4]";
# following also works
print "Here is the full arrays: @carmanufactures[0..4]";
# I believe the best way is following in these 3.
print "Here is the full arrays: @carmanufactures[0..$#carmanufactures]";
11.
#!/usr/bin/perl
use warnings;
use strict;
# NOTE: For interger use ==, <, >, <=, >=
if (1 < 2 && (5 < 4 || 5 > 4)) {
print "I am at if block.\n";
}
## string test:
# NOTE: for string eq, ne
# if, elsif, else, unless [elsif, to use within a sub-if block.]
my $fname = "amit";
my $name = "amit";
if ( $name ne $fname) {
print "$fname is same as $name\n";
}
else {
print "I am at else block.\n";
}
# unless is for negetation:
my $value1 = "1";
my $value2 = "2";
unless ($value1 == $value2) {
print "$value1 is not equal to $value2\n";
}
# one line if test:
print "one liner if\n" if $value1 != $value2;
#END
12. fileio1.pl
#!/usr/bin/perl
use warnings;
use strict;
# open function for file handel.
# die function to cache any error.
# $! is the perl variable to keep the error.
my $min = 1;
my $max = 20;
my $fname = "Amit mund";
my $dob = "23/04/1982";
# defining OUTFILE as a fh.
open (OUTFILE, ">data1") || die "problems: $!"; # $! perl inbuilt variable for error return.
for ($min..$max) {
print OUTFILE "$fname and DOB = ";
print OUTFILE "$dob\n"
}
#END
13. fileio2.pl
#!/usr/bin/perl
use warnings;
use strict;
#my $OUT = "OUTFILE";
#my $filename = "/proc/cpuinfo";
my $filename = "data1";
open (INFILE, "$filename") || die "problems: $!";
#while (<INFILE>){
# print "$_";
#}
my @data1contains = <INFILE>; # assoicate a file to an array.
foreach (@data1contains){
print "$_";
}
#END
14. FileIO3.pl
#!/usr/bin/perl
use warnings;
$IN = "INFILE";
$OUT = "OUTFILE";
$filenamein = "/proc/cpuinfo";
$filenameout = "data2";
open ($IN, "$filenamein") || die "error while opening the file: $!";
open ($OUT, ">$filenameout") || die "error while writing the file: $!";
@data1contents = <$IN>;
foreach (@data1contents){
if (/^processor/ || /^core id/ || /model name/){ # Nesting an if statement with regex
#print $OUT "$_";
s/model name/cpu/; # Replace "model name with cpu"
@cur_data=$_;
print $OUT "@cur_data";
}
}
#END
15. File04.pl
#!/usr/bin/perl
use warnings;
my $PROCESS;
open ($PROCESS, "ps aux | grep -i apache |") || die "process error $!";
@PROCESS_DATA = <$PROCESS>;
#print "@PROCESS_DATA"
foreach (@PROCESS_DATA){
print "$_"
}
# END
16. fileIO04A.pl
#!/usr/bin/perl
use warnings;
my $PROCESS;
open ($PROCESS, "ps aux|") || die "process error $!"; # need to end with | pipe as per the input for $PROCESS.
@PROCESS_DATA = <$PROCESS>;
foreach (@PROCESS_DATA){
if (/apache/){ # <----- Regex.
print "$_"
}
}
17. FileIO4_B.pl
#!/usr/bin/perl
use warnings;
$FH = "FileReadWrite";
$FILENAME = "data1";
$APPEND_OF_FILE = "Appended string\n";
# +< for read and append mode, and it must be in quote.
open ($FH, "+<$FILENAME") || die "file error: $!";
@dataOfFile = <$FH>;
foreach (@dataOfFile){
#print $FH "$_";
}
print $FH "$APPEND_OF_FILE";
18. FileIO05.pl
#!/usr/bin/perl
use warnings;
#$DIR = "/home/amit/Documents/perl";
$DIR = "/etc";
$DH = "Handle";
opendir ($DH, "$DIR") || die "Error opening dir: $!";
@dirlist = readdir($DH); # list the first level of contents.
foreach (@dirlist){
#if (/^[x-z]/){ # regex of start with x-z letters.
# print "$_\n";
#}
print "$_\n";
}
#closedir ($DH); # Good to close the handler.
#@dirlist = `ls -A $DIR`;
#foreach (@dirlist){
# print "$_\n";
#}
#END
19.
#!/usr/bin/perl
use warnings;
#use strict;
#my $mem = perl -wln -e '@mem1 = split /:/, $_; if (/DirectMap2M/){print $mem1[1]} ;' /proc/meminfo
my $MEM_FH1 = "memory_FH";
my $mem_file = "/proc/meminfo";
open ($MEM_FH1, "$mem_file") || die "problem opening file: $!";
my @mem_data_contains = <$MEM_FH1>;
foreach(@mem_data_contains){
if (/^MemFree/){
#my @mem_value = split /:/, @mem_data_contains;
my @mem_free_value = split /\s+/,$_;
my @mem_free_value1 = $mem_free_value[1] * 1024; # in bite conversion
print "Free Memory = @mem_free_value1\n";
#print @mem_data_contains;
}
}
foreach(@mem_data_contains){
if (/^MemTotal/){
#my @mem_value = split /:/, @mem_data_contains;
my @mem_total_value = split /\s+/,$_;
my @mem_total_value1 = $mem_total_value[1] * 1024; # in bite conversion
print "Total Memory = @mem_total_value1\n";
#print @mem_data_contains;
}
}
#my $Free_percent =
20.
#!/bin/bash/perl
use strict;
use warnings;
#my $min = 1;
#my $max = 10;
#my $PROD_NAME = "Testing the perl foreach loop.";
my @array1 = ("Amit", "Kumar", "Mund", "Testing");
foreach (@array1) { # just need to pass the array value.
# body...
print "$_ "; # $_ is an internal variable that keep the complete value.
}
$#array1 +=1;
print "\nTotal arrays element = $#array1";
print "\n";
21.
#!/usr/bin/perl -wl
use strict;
# Note: in hash it need to even number of objects.
my %make_model = (
"Honda","city",
"Maruti","swift"
);
print $make_model{"Maruti"};
#### The nice way of writing the hash:
my $player = "Venus";
my %player_country = (
Venus => "USA",
Sharapova => "Russia",
);
printf "$player represents: ";
print $player_country{"$player"};
###
my @num_of_players_keys = keys %player_country;
my @num_of_players_values = values %player_country;
print "The keys are: @num_of_players_keys[0..$#num_of_players_keys]";
print "The values are: @num_of_players_values[0..$#num_of_players_values]";
22.
#!/usr/bin/perl
#
# The traditional first program.
# Strict and warnings are recommended.
use strict;
use warnings;
# Print a message.
print "Content-type: text/html\n\n";
print "Hello, World!\n";
=begin comment
This is all part of multiline comment.
You can use as many lines as you like
These comments will be ignored by the
compiler until the next =cut is encountered.
=cut
=begin comment
for multiline comment it start with: " =begin" and end with "cut" after
the =
=cut
print "Hello, world\n";
print 'Hello, world\n';
23.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
my $str = <<EOM;
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
EOM
print $str
24. perldb.pl
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
#Setp1 - creating connection object
my $dsn = 'DBI:mysql:thenewboston'; # module:db_engine:db_name
my $user = 'root';
my $pass = 'YourPassword';
my $host = 'localhost';
my $conn = DBI->connect($dsn,$user,$pass) || die "Error connecting" . DBI->errstr;
# step 2 - define query
my $query1 = $conn->prepare('SELECT id,name FROM user') || die "Error preparing query" . $conn->errstr;
# setp 3 - execute query
$query1->execute || die "Error executing query" . $conn->errstr;
# step 4 - return results
my @results;
while (@results = $query1->fetchrow_array()) {
my $id = $results[0];
my $name = $results[1];
#my $name = $results[2];
print "$id,$name\n"
}
# trying for another query too:
my $query2 = $conn->prepare('SHOW TABLES') || die "Error preparing query" . $conn->errstr;
$query2->execute || die "Error executing query" . $conn->errstr;
my @results2;
print "\nFollowing are the tables at thenewboston DB:\n";
while (@results2 = $query2->fetchrow_array()) {
my $tables = $results2[0];
print "$tables\n"
}
25.
#!/usr/bin/perl
use warnings;
use strict;
my @array1 = ("1","2","3","4");
print "@array1\n";
# pop function, to remove the last value.
my $ppop = pop @array1; # over here $ppop will be carring the removed value.
print "@array1\n";
print "$ppop\n"; # printing the remoed value.
# NOTE: if you don;t want you can just pop the @array1 and print the same again and it will remove the last one.
pop @array1;
print "@array1\n";
#my $ppop = pop @array1;
#print "@array1\n";
#print "$ppop\n";
#END
26.
#!/usr/bin/perl
use warnings;
use strict;
my @array1 = ("1","2","3","4");
print "Original array1 values: @array1\n";
# pop function, to remove the last value.
my $ppop = pop @array1; # over here $ppop will be carring the removed value.
print "Value after doing a pop: @array1\n";
print "What is ppop is carring now: $ppop\n"; # printing the remoed value.
# Now push the another value;
push @array1, $ppop; # adding back the removed one.
print "\nValue after doing a push of the ppop value: @array1\n";
# adding one more value.
push @array1, "5";
print "Pushing one more eliment: @array1\n";
# adding one more value.
push @array1, "Amit";
print "Pushing one more eliment: @array1\n\n";
# Adding the element(s) at the beginning of the array.
my $fname = "Amit";
my $mname = "Kumar";
my $lname = "Mund";
unshift (@array1, "$fname", "$mname", "$lname");
print "@array1\n";
# Removing the 1st element from the array
my $s_shift =shift @array1;
print "@array1\n";
print "$s_shift\n";
## "sort", For sorting the string.
my @array2 = ("Amit","kumar", "Mund", "Bitu");
my @sort_array2 = sort @array2;
print "@sort_array2\n";
# We can use the same array name after doing sort and assign the same.
@array2 = sort @array2;
print "@array2\n";
#END
27.
#!/usr/bin/perl
use warnings;
use strict;
# @ARGV variable [ array type spical variable ]
# print "$ARGV[0]\n";
# print "Trying to print all the input argument: @ARGV[0..$#ARGV]\n";
my $REQ = 3;
my $BADARGS = 165;
$#ARGV += 1; # NOTE: No need to use my or our for ARGV. If you give, it will show error.
unless ($#ARGV == 3){
print "$0 requires $REQ variables: ";
print "ERROR! Insufficent argument.\n";
exit "$BADARGS";
}
print "Correct number of argument $#ARGV has been passed.\n";
print "$ARGV[0] $ARGV[1] $ARGV[2]\n";
print "printing with array type : @ARGV[0..$#ARGV]\n"; # saw the same output at training too.
# perldoc -f exit; for more help.
#END
28.
#!/usr/bin/perl -wl
# w: option for the warning.
# l: option for the line group.
$fname = "Amit";
$lname = "Mund";
$fullname = "$fname $lname";
print "$fname $lname";
print "$fullname";
# NOTE: Must end with a semi-collon;
29.
#!/usr/bin/perl
# split, join and input files from shell.
use warnings;
use strict;
while (<>){ # reading by a stdin. [ file name as arg ]
my @username = split /:/, $_;
if(/false/){
print "$username[0]\n";
}
}
# perl -wln -e '@array1 = split /:/, $_; print $array1[0]' passwdFile
# perl -wln -e '@mem1 = split /:/, $_; if (/DirectMap2M/){print $mem1[1]} ;' /proc/meminfo
# 8226816 kB
#END
30.
#!/usr/bin/perl -w
use strict;
printf "Please enter your name: ";
my $fname = <STDIN>; # For standard input, you can declare the variable at the same time.
chomp($fname); # To remove the last newline character.
print "\n***************************************";
print "\nHello $fname, how are you doing today?\n";
print "***************************************\n\n";
31.
#!/usr/bin/perl -w
use strict;
testsub(); # This is how to call a sub-routines, Its the same as function
# and its not needed to be after declating this sub-routines.
sub testsub { # Need to start with "sub"
my $name = "Amit Mund";
print "Hello $name\n";
}
# Note: you can pass the argument too in the sub-routines
testsub1("Amit Mund", "Bitu");
sub testsub1 {
print "Following is an example of getting the value as argument from testsub2\n";
print "Hello: $_[0]\n"; # $_ is the default place holder.
}
# Two argument in the "foreach" loop and printing Hello Amit and Hello Bitu.
testsub2 ("Amit","Bitu","Tutu");
sub testsub2 {
foreach (@_){
print "Hello $_\n";
}
}
# NOTE: @_ is the for all the array value passed on the argument.
testsub3 ("Amit","Bitu","Tutu");
sub testsub3 {
print "Hello @_\n";
}
32.
#!/usr/bin/perl
use warnings;
use strict;
# file handling:
open (fh1, ">> sub2.log") || die "Error opening the file: $!";
my $etcdir = `ls -l /etc/passwd`;
chomp ($etcdir); # to delete extra newline.
my $message = "Launching sub2.pl";
log_message("$message");
log_message("$etcdir");
log_message(`uptime; who`);
sub log_message{
my $current_time = localtime;
print "$current_time - $_[0]","\n";
print fh1 "$current_time - $_[0]", "\n";
}
#perl -e '$decimal=10, print $decimal;'
#NOTE: perl -e always need to be in single quote, e.g:
# perl -l -e '$current_time = localtime, print $current_time;'
33.
#!/usr/bin/perl
use warnings;
use strict;
# Printing the arguments.
#print "$ARGV[0]\n";
#print "Trying to print all the input argument: @ARGV[0..$#ARGV]\n";
# printing value at hex.
#perl -wl -e '$hex = sprintf("0x%x",15), print $hex;'
#######################################
# NOTE: Try to always use the variable.
######################################
# Following is the "c" style for loop.
#my $min = 1;
#my $max = 100;
#my $i;
#
#for ($i=$min; $i<=$max; $i++){
#
# print "$i\n";
#}
my $min = 1;
my $max = 100;
my $i=0;
for ($min..$max){
print "For loop test\n";
}
# can put the same in a single line too:
for ($min..$max){$i++; print "$i\n";}
#END
34.
#!/usr/bin/perl
use warnings;
#use strict;
my $IN = INFILE;
my $infile = passwdFile;
open ($IN, "$infile") || die "problems: $!";
while (<$IN>){
print "$_";
}
#END
35.
#!/usr/bin/perl
use warnings;
@mem = split /:/, $_; if (/DirectMap2M/){print "$mem1[1]";} ;' /proc/meminfo
print @mem;
@myls = `ls -l`;
print @myls;
36.
#!/usr/bin/perl
use warnings;
use strict;
print "Content-type: text/html\n\n";
# syntax:
#while (condition){
#body;
#}
my $value1 = 1;
my $value2 = 20;
while ($value1 <= $value2){
print "$value1,";
$value1 +=2;
}
print "\n";
# syntax:
#until (condition){
#body;
#}
my $value11 = 1;
my $value22 = 20;
until ($value22 <= $value11){
print "$value22,";
$value22 -=2;
}
print "\n";
#END
Saturday, 9 August 2014
html frame example
1. The following code will create a frame.
$vi frame.htm
<html>
<frameset cols="10%,*" border="7" bordercolor="red">
<frame name="leftFrame" src="navigation.htm" marginheight=30 marginwidth="5">
<frame name="mainFrame" src="mainHome.htm" marginheight="30" marginwidth="25">
</frameset>
</html>
NOTE: the frame name that you can do the target to for other pages.
2. navigation page:
$vi navigation.htm
<html>
<head>
</head>
<body>
<H5><u>Navigation</u></H5>
<a href="mainHome.htm" target="mainFrame"a>Home</a><br>
<a href="page1.htm" target="mainFrame"a> Page1<br>
<a href="page2.htm" target="mainFrame"a> Page2<br>
</body>
</html>
NOTE: e mainHome.htm, page1.htm and page2.htm all are target to the "mainFrame" that we have define on the frame.htm [above] code. So that when you click the above link, those pages will open on the mainFrame.
Different target:
http://www.w3schools.com/tags/att_a_target.asp
3. Now you create your pages as define on the Navigation.htm file:
example:
$ vi mainHome.htm:
<html>
<head>
</head>
<body>
This is the Home page.
</body>
</head>
example:
$ vi page1.htm
<html>
<head>
</head>
<body>
This is page1.
</body>
</head>
example:
$vi page2.htm
<html>
<head>
</head>
<body>
This is page2.
</body>
</head>
SnapShot:

$vi frame.htm
<html>
<frameset cols="10%,*" border="7" bordercolor="red">
<frame name="leftFrame" src="navigation.htm" marginheight=30 marginwidth="5">
<frame name="mainFrame" src="mainHome.htm" marginheight="30" marginwidth="25">
</frameset>
</html>
NOTE: the frame name that you can do the target to for other pages.
2. navigation page:
$vi navigation.htm
<html>
<head>
</head>
<body>
<H5><u>Navigation</u></H5>
<a href="mainHome.htm" target="mainFrame"a>Home</a><br>
<a href="page1.htm" target="mainFrame"a> Page1<br>
<a href="page2.htm" target="mainFrame"a> Page2<br>
</body>
</html>
NOTE: e mainHome.htm, page1.htm and page2.htm all are target to the "mainFrame" that we have define on the frame.htm [above] code. So that when you click the above link, those pages will open on the mainFrame.
Different target:
http://www.w3schools.com/tags/att_a_target.asp
3. Now you create your pages as define on the Navigation.htm file:
example:
$ vi mainHome.htm:
<html>
<head>
</head>
<body>
This is the Home page.
</body>
</head>
example:
$ vi page1.htm
<html>
<head>
</head>
<body>
This is page1.
</body>
</head>
example:
$vi page2.htm
<html>
<head>
</head>
<body>
This is page2.
</body>
</head>
SnapShot:
Subscribe to:
Posts (Atom)
