Jan 23, 2015

Unix Shell scripting conditional expressions


In this post, lets discuss the use of conditionals in bash scripts.

What are conditionals?


At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.

The most compact syntax of the if command is:

if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi

The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.

The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails.

Syntax:


if [ expression 1 ];
then
  Statement(s) to be executed if expression 1 is true
elif [ expression 2 ];
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ];
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

Example:



#!/bin/sh
a=1
b=2

if [ $a == $b ];
then
   echo "a is equal to b"
elif [ $a -gt $b ];
then
   echo "a is greater than b"
elif [ $a -lt $b ];
then
   echo "a is less than b"
else
   echo "None of the conditions matched"
fi

Now let us see the different ways to use expressions with numeric and strings.

       ( EXPRESSION )
              EXPRESSION is true

       ! EXPRESSION
              EXPRESSION is false

       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true

       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING

       -z STRING
              the length of STRING is zero

       STRING1 = STRING2
              the strings are equal

       STRING1 != STRING2
              the strings are not equal

       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2


Expressions for file operations can be found in this link: File Operations


All these details could be obtained from 'man test' command on linux/unix terminal.

What are VIEWS in Oracle/PLSQL?



Oracle/PLSQL: VIEWS


Let's learn to create, update, and drop Oracle VIEWS with syntax and examples.

What is a VIEW in Oracle?


An Oracle VIEW, in essence, is a virtual table that does not physically exist. Rather, it is created by a query joining one or more tables.

Create VIEW
Syntax
The syntax for the Oracle CREATE VIEW Statement is:

CREATE VIEW view_name AS 
SELECT columns FROM tables WHERE conditions;

view_name is the name of the Oracle VIEW that you wish to create.

Example
Here is an example of how to use the Oracle CREATE VIEW:

CREATE VIEW sup_orders AS 
SELECT suppliers.supplier_id, orders.quantity, orders.price 
FROM suppliers INNER JOIN orders 
ON suppliers.supplier_id = orders.supplier_id WHERE suppliers.supplier_name = 'Microsoft';

This Oracle CREATE VIEW example would create a virtual table based on the result set of the SELECT statement. You can now query the Oracle VIEW as follows:

SELECT * FROM sup_orders;

Update VIEW

You can modify the definition of an Oracle VIEW without dropping it by using the Oracle CREATE OR REPLACE VIEW Statement.

Syntax
The syntax for the Oracle CREATE OR REPLACE VIEW Statement is:

CREATE OR REPLACE VIEW view_name AS 
SELECT columns FROM table WHERE conditions;

Example
Here is an example of how you would use the Oracle CREATE OR REPLACE VIEW Statement:

CREATE or REPLACE VIEW sup_orders AS 
SELECT suppliers.supplier_id,
orders.quantity, orders.price
FROM suppliers INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id
WHERE suppliers.supplier_name = 'Apple';

This Oracle CREATE OR REPLACE VIEW example would update the definition of the Oracle VIEW called sup_orders without dropping it. If the Oracle VIEW did not yet exist, the VIEW would merely be created for the first time.

Drop VIEW

Once an Oracle VIEW has been created, you can drop it with the Oracle DROP VIEW Statement.

Syntax
The syntax for the Oracle DROP VIEW Statement is:

DROP VIEW view_name;

view_name is the name of the view that you wish to drop.

Example

Here is an example of how to use the Oracle DROP VIEW Statement:

DROP VIEW sup_orders;

This Oracle DROP VIEW example would drop/delete the Oracle VIEW called sup_orders.

Frequently Asked Questions

Question: Can you update the data in an Oracle VIEW?

Answer: A VIEW in Oracle is created by joining one or more tables. When you update record(s) in a VIEW, it updates the records in the underlying tables that make up the View.
So, yes, you can update the data in an Oracle VIEW providing you have the proper privileges to the underlying Oracle tables.


Question:
Does the Oracle View exist if the table is dropped from the database?

Answer: Yes, in Oracle, the VIEW continues to exist even after one of the tables (that the Oracle VIEW is based on) is dropped from the database. However, if you try to query the Oracle VIEW after the table has been dropped, you will receive a message indicating that the Oracle VIEW has errors.
If you recreate the table (the table that you had dropped), the Oracle VIEW will again be fine.

Jan 14, 2015

Eclipse + Maven + Tomcat 7 : java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet




Sometimes when we setup a new web application project in Eclipse with Maven and using Tomcat 7 server, we might face following exception trace in server startup.



java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)



This exception usually occur due to missing "Maven Dependency" in Deployment Assembly.

Following the below steps will help resolving the issue:

- Open your eclipse-> right click on your maven project & choose "Properties"

- Click on "Deployment Assembly"

- Click "Add"

- Click on "Java Build Path Entries"

- Select "Maven Dependencies"

- Click Finish.

Rebuild and deploy it again.




Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete



Problem usually arises when we use Eclipse Kepler and Tomcat 7.

Add Tomcat 7 to eclipse and when you start the tomcat or when you try to add a project to tomcat, you will get following error 


Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 
Server at localhost-config. The configuration may be corrupt or incomplete


Follow the below steps to resolve the issue:


- Delete tomcat from eclipse-> servers tab.

- Copy all the contents inside your "TOMCAT_7_HOME/conf" directory.

- Paste the above copied content into "WORKSPACE_DIR/Servers/Tomcat v7.0 Server at localhost-config" directory.

- Open Eclipse and add the tomcat server to Servers tab.

- Add the project and start the server.


Jan 12, 2015

Shell Scripting: File Operations



In Shell scripting, we can perform lots of file based operations such as checking if file exists, checking if it's a directory, checking if it's a writable file and so on. 

For Example, following is a shell script that checks if the file "config_file.txt" exists.
 
#!/bin/bash
FILE="config_file.txt" if [ -f $FILE ]; then echo "File $FILE exists" else echo "File $FILE does not exists" fi

Following are the list of options available for file operations.

-a file
True if file exists.

-b file
True if file exists and is a block special file.

-c file
True if file exists and is a character special file.

-d file
True if file exists and is a directory.

-e file
True if file exists.

-f file
True if file exists and is a regular file.

-g file
True if file exists and is set-group-id.

-h file
True if file exists and is a symbolic link.

-k file
True if file exists and its ‘‘sticky’’ bit is set.

-p file
True if file exists and is a named pipe (FIFO).

-r file
True if file exists and is readable.

-s file
True if file exists and has a size greater than zero.

-t fd 
True if file descriptor fd is open and refers to a terminal.

-u file
True if file exists and its set-user-id bit is set.

-w file
True if file exists and is writable.

-x file
True if file exists and is executable.

-O file
True if file exists and is owned by the effective user id.

-G file
True if file exists and is owned by the effective group id.

-L file
True if file exists and is a symbolic link.

-S file
True if file exists and is a socket.

-N file
True if file exists and has been modified since it was last read.

file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and
file2 does not.

file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.

file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.

How to Remote Desktop (RDP) Linux Machine from windows 7


Overview
Our requirement is to enable remote desktop function in centos Linux machine which will allow us to access Linux box from windows machine using mstsc.exe (Remote Desktop).

For that we will use XRDP which is wonderful remote desktop protocol application. It is free open source software for Linux.

Installation:
We need some additional work to install XRDP on centos as centos repositories do not contain the XRDP package. You need to download and add EPEL (Extra Packages Enterprise Linux).

Let’s verify our correct OS architecture first.
Using below command we can verify OS architecture.

Step 1:
[/root]# uname -r
2.6.32-431.el6.x86_64

If the output shows x86_64 at end then mean, you have a 64-bit install or if it shows i386 then it is a 32-bit install.

Our output is showing (2.6.32-431.el6.x86_64) it’s mean we have 64-bit operating system.
After determining your architecture, install the correct EPEL repository with below command.

Step 3:
For RedHat or Centos 6.x 32-bit
[/root]# wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
[/root]# rpm -ivh epel-release-6-8.noarch.rpm

For Redhat Enterprise Linux or Centos 5.x 64-bit
[/root]# wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
[/root]# sudo rpm -Uvh epel-release-5*.rpm

For RedHat Enterprise Linux or Centos 6.x 64-bit
[/root]# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[/root]# sudo rpm -Uvh epel-release-6*.rpm

For Redhat Enterprise Linux or Centos 7.x 64-bit
[/root]# wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-1.noarch.rpm
[/root]# sudo rpm -Uvh epel-release-7*.rpm

Step 4:
Lets verify EPEL repository is installed correctly
[/root]# yum repolist -v | grep epel
We can see EPEL repository is installed correctly.

While doing this step, if any following error occurs,

"Loaded plugins: fastestmirror, refresh-packagekit, security Loading
mirror speeds from cached hostfile
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again"

Solve this issue editing both /etc/yum.repos.d/epel.repo and /etc/yum.repos.d/epel-testing.repo files, commenting all entries starting with mirrorlist=... and uncomenting all the entries starting with baseurl=...

If it didn’t solve the issue, then run yum update on the os.

Step 5:
Now we need to install XRDP package including some dependencies.
[/root]# yum install xrdp tigervnc-server

Step 6:
After instllation we need to configure display.

Edit display file located at /etc/sysconfig/vncservers
[/root]# vim /etc/sysconfig/vncservers

Find at last these lines
# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"

Uncomment and change them into.
VNCSERVERS="2:root"
VNCSERVERARGS[2]="-geometry 800x600"

I am using root user as my remote desktop user.
Now save this file and exit.

Step 7:
Start VNC and XRDP services.
[/root]# service vncserver start
[/root]# service xrdp start

If any following error occurs on vncserver start,

"You will require a password to access your desktops.
getpassword error: Inappropriate ioctl for device"

Run the command "vncserver /.vnc/passwd" and then enter the password (i.e. Setup new password)


Step 8:
Make them available on next startup.
[/root]# chkconfig xrdp on
[/root]# chkconfig vncserver on

That’s it all the packages are installed, now you should be able to access your linux box from any RDP Client.

Stop iptables from centos machine.
[/root]# sudo /etc/init.d/iptables stop 


Then goto your windows machine where you need to connect the above configured Linux machine. Click on "Start" and type "mstsc.exe".

In the open dialog, enter the Linux PC's ip address & Enter. Give Linux machine's user name ('root' in our case) and password.

Now you are connected to Linux via Remote Desktop.