Sunday 25 December 2016

Array in Linux/Unix Shell Scripting

In Linux shell an Array is the collection of same or different kind of elements which are accessible using zero-based index. In shells array there is no specific data type that means while declaring a shells array no need to define the data type for array. You can store integer, float, string etc in a same array.

This post is part of the shell scripts posts. You can read more basic shell script examples on this page ( http://coolstuffsite.blogspot.in/p/shells-script.html )

In this post i will make shells array easy for you with proper examples.

Declaring a Shell Arrays
To declare a shell array use the following syntax.
declare -a array_name

where array_name is the name of array
Note: In bash, declaring array is not necessary. We can insert element individually with array index.

Declaring and Assigning values to an Array:
Method 01:
Use the following syntax to declare and assigning or initializing the values.
array_name[index]=value

where array_name is the name of array.
      index is position of element which start from zero.
      value is the array value at particular index.
     
Method 02:
You can use another method to declare bash array as following syntax.
array_name=(value1 value2 ... valuen)

This method is useful when you need to take dynamic elements . In this method you can declare space separated static array elements as well as you can place any Linux command, wild cards or SQL query .
For example if we use '*' then it will create a array of all directories and files available in current directory. I will make you understand these concept in following examples .

Accessing Bash Array Elements:
To read the array element use the following syntax.
echo ${array_name[index]}

To access all the array elements use the following syntax.
echo ${array_name[@]}
        #or
echo ${array_name[*]}

To print the number of elements of an array use the following syntax.
echo ${#array_name[@]}

Now lets start some practice with examples.

Example01 : Declaring array with method 01 and accessing array elements.
#!/bin/bash


# Declaring an array demoArray.
declare -a demoArray # It is optional


# Assigning diffrent data type elements to array demoArray
demoArray[0]='Unix'
demoArray[1]='Linux'
demoArray[2]=100
demoArray[4]=50.0
demoArray[5]='"This is demoArray script"'


# Printing third element which is an integer
echo "3rd elemets of array demoArray is : ${demoArray[2]}"
# Printing last element which is a string
echo "6th or last element of array is : ${demoArray[5]}"
# To print all the elements of the array
echo "List of all elemets : ${demoArray[@]}"


# To print the number of elements in the array
echo "Number of elements in array is : ${#demoArray[*]}"


Output:
./bashArray01.sh

3rd elemets of array demoArray is : 100
6th or last element of array is : "This is demoArray script"
List of all elemets : Unix Linux 100 50.0 "This is demoArray script"
Number of elements in array is : 5


Example02: Declaring array with method 02 and accessing array elements.
In the below example 'declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script")' and 'demoArray=(Unix Linux 100 50.0 "This is demoArray script")' represents the same array.

#!/bin/bash


# Declaring array and assigning static elements
declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script")


# Printing third element which is an integer
echo "3rd elements of array demoArray is : ${demoArray[2]}"
# Printing last element which is a string
echo "6th or last element of array is : ${demoArray[5]}"
# To print all the elements of the array
echo "List of all elements : ${demoArray[@]}"


# Declaring and initializing array current directory contents.
#declare -a currDirContent=(*)
currDirContent=(*)


# Printing all elements of current directory.
echo "-------------------------------"
echo "Current dir files are : ${currDirContent[@]}"


# Declaring lower case alphabets array
lowercase=({a..z})
# Printing a to z
echo "-------------------------------"
echo "${lowercase[@]}"
Output:
./bashArray02.sh

3rd elements of array demoArray is : 100
6th or last element of array is :
List of all elements : Unix Linux 100 50.0 This is demoArray script
-------------------------------
Current dir files are : bashArray01.sh bashArray02.sh
-------------------------------
a b c d e f g h i j k l m n o p q r s t u v w x y z



Example02: In this example we are creating an array of all emailIds which are in the customer table of a mysql database. To understand this example consider a table "customer" which having the following details.
mysql> select * from customer;
+------------+--------------+----------------------+-----------+
| customerid | customername | emailid              | accountno |
+------------+--------------+----------------------+-----------+
| 1001       | Neeraj singh | neeraj@gmail.com     |     30004 |
| 1002       | Tanu         | tanu@gmail.com       |     30005 |
| 1003       | Shivam Kumar | shivam@yahoo.com     |     30006 |
| 1004       | vinay        | vinay.s@gmail.com    |     30007 |
| 1005       | Ajay singh   | ajay.singh@gmail.com |     30008 |
+------------+--------------+----------------------+-----------+


Once we get array of above emailIds we will access the emails using a for loop.

#!/bin/bash


# Creating array of emails from database. 
declare -a EMAILIDS=(`mysql -umukesh -p*** -hlocalhost -e "SELECT emailid FROM mydb.customer"`)
# The above command will be equivalent to :
# EMAILIDS=(neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com )


# Accessing emails using for loop
for email in "${EMAILIDS[@]}"
do
    echo "$email"
done


# Print all array of emails
echo "-----------------------------"
echo ${EMAILIDS[@]}


# Printing number of emails
echo "-----------------------------"
echo "Total emails are : ${#EMAILIDS[@]}"

Output:
./bashArray03.sh

neeraj@gmail.com
tanu@gmail.com
shivam@yahoo.com
vinay.s@gmail.com
ajay.singh@gmail.com
-----------------------------
neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com
-----------------------------
Total emails are : 5

Saturday 10 December 2016

Apache-Tomcat Interview Questions And Answers

Q. Difference between apache and apache-tomcat server ?
Apache:
  •     Apache mostly serves static content by itself, but there are many add-on modules (some of     which come with Apache itself) that let it modify the content and also serve dynamic content written in Perl, PHP, Python, Ruby, or other languages.
  •     Basically Apache is an HTTP Server, serving HTTP.

Apache-Tomcat:
  •     Tomcat is primarily a servlet/JSP container. Its written in Java. It can serve static content too, but its main purpose is to host servlets and JSPs.
  •     JSP files (which are similar to PHP, and older ASP files) are generated into Java code (HttpServlet), which is then compiled to .class files by the server and executed by the Java virtual machine.
  •     Apache Tomcat is used to deploy your Java Servlets and JSPs. So in your Java project you can build your WAR (short for Web ARchive) file, and just drop it in the deploy directory in Tomcat.
  •     Although it is possible to get Tomcat to run Perl scripts and the like, you wouldn't use Tomcat unless most of your content was Java.
  •     Tomcat is a Servlet and JSP Server serving Java technologies

Note:
    The two technologies can be used together through a connector module called mod_jk. This will allow you to use the Apache HTTP server to serve regular static webpages, and the Tomcat Servlet engine to execute servlets.
   
Q. Difference between web server and application server ?
    1. Application Server supports distributed transaction and EJB.
        While Web Server only supports Servlets and JSP.
    2. Application Server can contain web server in them. most of App server e.g. JBoss or WAS has
        Servlet and JSP container.
    3. Though its not limited to Application Server but they used to provide services like Connection
        pooling, Transaction management, messaging, clustering, load balancing and persistence. Now
        Apache tomcat also provides connection pooling.
    4. In terms of logical difference between web server and application server. web server is supposed
        to provide http protocol level service
        while application server provides support to web service and expose business level service
        e.g. EJB.
    5. Application server are more heavy than web server in terms of resource utilization.

Q. How to start and shutdown to tomcat server
   There are two .sh file in cd $CATALINA_HOME/bin  dir or in /usr/share/tomcat7/bin/
    ./startup.sh
    ./shutdown.sh

Q. After startup what is the url for default web-application?
      http://localhost:8080/

Q. What are the directories under the apache-tomcat installation dir ?
  •     conf - Server configuration files (including server.xml)
  •     logs - Log and output files
  •     shared - For classes and resources that must be shared across all web applications
  •     webapps - Automatically loaded web applications
  •     work - Temporary working directories for web applications
  •     temp - Directory used by the JVM for temporary files (java.io.tmpdir)

Q. How to change the default(8080) port number?
  •     Go to tomcat>conf folder.
  •     Edit server.xml. (/usr/share/tomcat/conf/server.xml)
  •     Search "Connector port"
  •     Replace "8080" by your port number.
  •     Restart tomcat server.
  •    
    For example, if you change the port to 1977, you would request the URL http://localhost:1977/
   
    Note: While changing the port number make sure that port is not already in use and port no should be greater than 1024, as ports less than or equal to 1024 require superuser access to bind to.
  
Q. How to know your Apache tomcat version ?
    root@ubuntu:~# /usr/share/tomcat7/bin/version.sh
    Using CATALINA_BASE:   /usr/share/tomcat7
    Using CATALINA_HOME:   /usr/share/tomcat7
    Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
    Using JRE_HOME:        /usr/lib/jvm/java-1.7.0-openjdk-i386
    Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
    Server version: Apache Tomcat/7.0.26
    Server built:   Apr 1 2013 08:32:04
    Server number:  7.0.26.0
    OS Name:        Linux
    OS Version:     3.2.0-105-generic-pae
    Architecture:   i386
    JVM Version:    1.7.0_101-b00
    JVM Vendor:     Oracle Corporation

Q. what are the configuratiopn file in tomcat server?
    Tomacat XML Configuration Files:
  1.     server.xml(TOMCAT-HOME/conf/server.xml)
  2.     web.xml   (TOMCAT-HOME/conf/web.xml)    
  3.     tomcat-users.xml (TOMCAT-HOME/conf/tomcat-users.xml)
Q. How to use tomcat server as a HTTP server ?
    Tomcat also contains a HTTP connector which can be used to serve static HTML pages. The standard directory which will be served is below the Tomcat webapps/ROOT installation directory. Place static content into this directory.

    To allow directory browsing via Apache Tomcat change the listings parameter in the file conf/web.xml from false to true.
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
   
Q. Explain what is Jasper?
    Jasper is a Tomcat’s JSP engine
    It parses JSP files to compile them into JAVA code as servlets
    At run-time, Jasper allows to automatically detect JSP file changes and recompile them
   
Q. Where can be set roles,username and password ?
   /conf/tomcat-users.xml
  
   <tomcat-users>
        <role rolename="manager-gui" />
        <user username="tomcat" password="s3cret" roles="manager-gui" />
   </tomcat-users>

Saturday 3 December 2016

Python Mysql Database Access

There is a python module called MySQLdb through which we can access many databases like Mysql,Oracle,PostgreSQl,Sybase,GadFly etc.
Here i am going with Mysql on Linux operating system.
Other than MySQLdb module there is one more module called mysql-connector which also provide database access from python script.

Diffrence between MySQLdb and mysql-connector.
MySQLdb : MySQLdb is a C module that links against the MySQL protocol implementation in the libmysqlclient library. It is faster, but requires the library in order to work.

mysql-connector : mysql-connector is a Python module that reimplements the MySQL protocol in Python. It is slower, but does not require the C library and so is more portable.

MySQLdb:
MySQLdb is an interface or python module for connecting to a MySQL database server from Python.

Verify if MySQLdb  have installed in your machine. Try to run the following script.
    #!/usr/bin/python

    import MySQLdb


if it is throwing following error that mean MySQLdb module need to install in your machine.
    Traceback (most recent call last):
      File "test.py", line 3, in <module>
        import MySQLdb
    ImportError: No module named MySQLdb


To install MySQLdb module in your Unix/Linux OS, download zip file from MySQLdb Download page(http://pkgs.fedoraproject.org/repo/pkgs/MySQL-python/MySQL-python-1.2.2.tar.gz/532268f02870bea18c1d465e88afff30/MySQL-python-1.2.2.tar.gz) and run the following command.

You can also try wget command to download zip frile in your unix machine.

    $ wget http://pkgs.fedoraproject.org/repo/pkgs/MySQL-python/MySQL-python-1.2.2.tar.gz/532268f02870bea18c1d465e88afff30/MySQL-python-1.2.2.tar.gz
    $ gunzip MySQL-python-1.2.2.tar.gz
    $ tar -xvf MySQL-python-1.2.2.tar
    $ cd MySQL-python-1.2.2
    $ python setup.py build
    $ python setup.py install



Before starting create a test databases called mydb and table as customer with following table structure and values.
    mysql> select * from mydb.customer;

    +------------+--------------+----------------------+-----------+
    | customerid | customername | emailid              | accountno |
    +------------+--------------+----------------------+-----------+
    | 1001       | Neeraj singh | neeraj@gmail.com     |     30004 |
    | 1002       | Tanu         | tanu@gmail.com       |     30005 |
    | 1003       | Shivam Kumar | shivam@yahoo.com     |     30006 |
    | 1004       | vinay        | vinay.s@gmail.com    |     30007 |
    | 1005       | Ajay singh   | ajay.singh@gmail.com |     30008 |
    +------------+--------------+----------------------+-----------+


Example 01 : Read/Select operation

In read operation we need to use the following useful method to fetch or read the information from database.
fetchone() : It fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.
fetchall(): It fetches all the rows in a result set. If some rows have already been extracted from the result set, then it retrieves the remaining rows from the result set.
rowcount: This is a read-only attribute and returns the number of rows that were affected by an execute() method.

#!/usr/bin/python

import MySQLdb

# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to INSERT a record into the database.
sql = "SELECT * FROM customer \
       WHERE customerid > '%d'" % (1003)
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Fetch all the rows in a list of lists.
   print "Total count :"+str(cursor.rowcount)
   results = cursor.fetchall()
   for row in results:
      customerid = row[0]
      customername = row[1]
      emailid = row[2]
      accountno = row[3]
      # Now print fetched result
      print "customerid=%s,customername=%s,emailid=%s,accountno=%d" % \
             (customerid, customername, emailid, accountno )
except :
   print "Error: unable to fecth data"


# Close the cursor and disconnect from server
finally:
        cursor.close()
        conn.close()

Read operation with fetchone() method
#!/usr/bin/python

import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")


# Fetch a single row using fetchone() method.
data = cursor.fetchone()


print "Database version : %s " % data


# Close the cursor and disconnect from server
finally:
        cursor.close()
        conn.close()

        

Ouptput:
Database version : 5.5.49-0ubuntu0.12.04.1

Example 02 : Insert Operation
#!/usr/bin/python


import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to INSERT a record into the database.
#sql = """INSERT INTO customer(customerid,
#         customername, emailid, accountno)
 #        VALUES ('1006', 'Mohan', 'mohan@reddif.com', '30009')"""


# Prepare dynamic SQL query to INSERT a record into the database.
sql = "INSERT INTO customer(customerid,\
         customername, emailid, accountno) \
                 VALUES('%s','%s','%s','%d')" \
         %('1006', 'Mohan', 'mohan@reddif.com', 30009)
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()


# Close cursor and disconnect from server
conn.close()
cursor.close()


Example 03 : Update Operation
Following procedure will update all records which having customername 'Mohan'.
#!/usr/bin/python


import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to UPDATE required records
sql = "UPDATE customer SET emailid = 'mohan.kumar@reddif.com'\

                          WHERE customername = '%s'" % ('Mohan')
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()


# disconnect from server
conn.close()


Example 04 : Delete Operation
#!/usr/bin/python


import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to DELETE required records
sql = "DELETE FROM customer WHERE customerid = '%s'" % ('1006')
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()


# disconnect from server
conn.close()


After performing this delete operation through python script. Connect mysql db and verify the operation successfully proceeded or not.



************************END*******************************


Related Posts Plugin for WordPress, Blogger...