Saturday, June 20, 2015

Introduction To Shell Scripting

What is a Shell

A shell is an interpreter in UNIX like Operating system. It takes commands typed by the user and calls the operating system to run those commands. In simple terms a shell acts as form of wrapper around the OS. For example , you may use the shell to enter a command to list the files in a directory , such as ls , or a command to copy ,such as cp.
[linuxforfreshers@localhost ~]$ ls
config  data  pam  test

In this example , when you simply type ls and press enter . The $ is the shell prompt , which tells you the the shell awaits your commands.The remaining lines are the names of the files in the current directory.

What is Shell Prompt
The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command. The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

What are different types of Shells :
since there is no monopoly of shells , you are free to run any shell as you wish. That's all well and good , but choosing a shell without knowing the alternative is not very helpful. Below are lists of shells available in UNIX/Linux.

1. The Bourne Shell
The Original Unix Shell is known as sh , short for shell or the Bourne shell , named for steven Bourne , the creator of sh. This is available on almost all the UNIX like operating system. The Basic bourne shell supports only the most limited command line editing, You can type the Characters,remove characters one at a time with the Backspace key and Press enter to execute the command. If command line gets messed up , you can press Ctrl-C to cancel the whole command.

2. The C Shell
It is desgined by Bill Joy at the university of california at Berkeley , the C shell was so named because much of its syntax parallels that of C programming language. This shell adds some neat features to the Bourne shell,especially the ability to recall previous commands to help create future commands.Because it is very likely you will need to execute more than one command to perform a particular task,this C shell capability is very useful.

3. The Korn Shell
It is created by David Korn at AT&T Bell laboratories , the korn shell or ksh offers the same kind of enhancements offers by the C Shell , with one important difference: The korn shell is backward compatible with the older Bourne shell Synatx. In UNIX likeAIX & HP-UX korn shell is the default shell.

4. Bash ( The Bourne Again Shell)
Bash offers command-line editing like the korn shell,file name completion like the C shell and a lot of other advance features. Many Users view bash as having the best of the Korn and C shells in one shell. In Linux and Mac OS X system , bash is the default shell.

5. tcsh ( The T C Shell)
Linux systems popularized the T C shell ot Tcsh. Tcsh extends the traditional csh to add command line editing,file name completion and more. For example , tcsh will complete the file and directory names when you press Tab key(the same key used in bash). The older C shell did not support this feature.

What is Shell Script :
A Shell Script is a text file that contains one or more commands. In a shell script, the shell assumes each line of text file holds a separate command. These Commands appear for most parts as if you have typed them in at a shell terminal.

Why To Use Shell Script :
Shell scripts are used to automate administrative tasks,encapsulate complex configuration details and get at the full power of the operating system.The ability to combine commands allows you to create new commands ,thereby adding value to your operating system.Futhermore ,combining a shell with graphical desktop environment allows you to get the best of both worlds.

Creating a First Script :
Create a text file in your current directory with a name myscript.sh , all the shell scripts have an “.sh” extension. First line of a shell script is either #!/bin/sh or #!/bin/bash , it is knows as shebang because # symbol is called hash and ! Symbol is called a bang. Where as /bin/sh & /bin/bash shows that commands to be executed either sh or bash shell. Bleow are the contents of amyscript.sh
#!/bin/bash
#A sample shell script
pwd # shows present working directory
date
# displays systems date


Assgin the Executable Permissions using below Command :
# chmod a+x myscript.sh

Now Execute the Script.
# sh myscript.sh
OR
# ./myscript.sh
Above shell script will display the current working directory along with date & time of the linux box.
Note: To execute your any shell script available in current directory you would execute using ./<Script-Name>

Taking Input From a User in the Shell Script.
Read command is used to take inputs from user via keyboard and assgin the vaule to a variable. Echo command is used to display the contents.
Example : Modify the myscript.sh file as shown below
#!bin/bash
echo 'What is Your Name?'
read name
echo "My Name is, $name"
Now Execute the script and will get the below output.
# sh myscript.sh
or 
./myscript.sh
What is Your Name?
linuxforfreshers
My Name is, linuxforfreshers


Monday, June 8, 2015

How to install Apache OpenOffice 4.1.1 on Ubuntu

How to install Apache OpenOffice 4.1.1 on Ubuntu 14.04 Trusty Tahr (LTS). You can also install Apache OpenOffice 4.1.1 on Ubuntu 12.04 Precise Pangolin, Linux Mint 17 Qiana, Elementary OS 0.3 and other Ubuntu and Debian (Linux) derivative systems.

Apache OpenOffice is the leading open-source office software suite for word processing, spreadsheets, presentations, graphics, databases and more. It is available in many languages and works on all common computers. It stores all your data in an international open standard format and can also read and write files from other common office software packages. It can be downloaded and used completely free of charge for any purpose.

To install Apache OpenOffice 4.1.1 on Ubuntu 14.04, we will download the deb packages (installer packages) from sourceforge and install it via dpkg command.

Before you begin the installation, you will have to remove the already installed versions of either OpenOffice and LibreOffice software on your Ubuntu 14.04 system.  You have to uninstall/remove the already installed OpenOffice or LibreOffice, irrespective of the Ubuntu 14.04 system architecture (32 bit or 64 bit).

Open the Terminal and run the following commands to remove the already installed versions of either openoffice and libreoffice software:


 sudo apt-get remove libreoffice* openoffice*
 sudo apt-get autoremove
Please restart your system before you begin the fresh installation.

How to install Apache OpenOffice 4.1.1 on 32 bit Ubuntu

Open the Terminal and run the following commands to download OpenOffice, extract the archive and install the deb packages (OpenOffice 4.1.1):


wget sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/en-GB/Apache_OpenOffice_4.1.1_Linux_x86_install-deb_en-GB.tar.gz
 tar -xzvf Apache_OpenOffice_4.1.1_Linux_x86_install-deb_en-GB.tar.gz

 cd en-GB/DEBS
 sudo dpkg -i *.deb
 cd desktop-integration
 sudo dpkg -i *.deb


How to install Apache OpenOffice 4.1.1 on 64 bit Ubuntu

Open the Terminal and run the following commands to download OpenOffice, extract the archive and install the deb packages (OpenOffice 4.1.1):


 wget sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/en-GB/Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz
 tar -xzvf Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz

 cd en-GB/DEBS
 sudo dpkg -i *.deb
 cd desktop-integration
 sudo dpkg -i *.deb


How to remove/un-install Apache OpenOffice 4.1.1

Open the Terminal and run the fowlloing commands to remove Apache OpenOffice 4.1.1 from Ubuntu 14.04 Trusty Tahr (LTS) system (either 32 bit or 64 bit Ubuntu 14.04 system).


$ sudo apt-get remove openoffice*

Saturday, June 6, 2015

How To Install Java on Ubuntu with Apt-Get



Introduction

As a lot of  programs require to have Java installed, this article will guide you through the process of installing and managing different versions of Java.

Installing default JRE/JDK
This is the recommended and easiest option. This will install OpenJDK 6 on Ubuntu 12.04 and earlier and on 12.10+ it will install OpenJDK 7.

Installing Java with apt-get is easy. First, update the package index:

sudo apt-get update
Then, check if Java is not already installed:

java -version
If it returns "The program java can be found in the following packages", Java hasn't been installed yet, so execute the following command:

sudo apt-get install default-jre

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example Apache Ant, Apache Maven, Eclipse and IntelliJ IDEA execute the following command:

sudo apt-get install default-jdk
That is everything that is needed to install Java.

All other steps are optional and must only be executed when needed.

Installing OpenJDK 7 (optional)
To install OpenJDK 7, execute the following command:

sudo apt-get install openjdk-7-jre

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), execute the following command:

sudo apt-get install openjdk-7-jdk

Installing Oracle JDK (optional)
The Oracle JDK is the official JDK; however, it is no longer provided by Oracle as a default installation for Ubuntu.

You can still install it using apt-get. To install any version, first execute the following commands:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

Then, depending on the version you want to install, execute one of the following commands:

Oracle JDK 6

This is an old version but still in use.

sudo apt-get install oracle-java6-installer
Oracle JDK 7

This is the latest stable version.

sudo apt-get install oracle-java7-installer
Oracle JDK 8

This is a developer preview, the general release is scheduled for March 2014. This external article about Java 8 may help you to understand what it's all about.

sudo apt-get install oracle-java8-installer
Managing Java (optional)
When there are multiple Java installations on your Droplet, the Java version to use as default can be chosen. To do this, execute the following command:

sudo update-alternatives --config java

It will usually return something like this if you have 2 installations (if you have more, it will of course return more):

There are 2 choices for the alternative java (providing /usr/bin/java).

Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      manual mode

Press enter to keep the current choice[*], or type selection number:
You can now choose the number to use as default. This can also be done for the Java compiler (javac):

sudo update-alternatives --config javac

It is the same selection screen as the previous command and should be used in the same way. This command can be executed for all other commands which have different installations. In Java, this includes but is not limited to: keytool, javadoc and jarsigner.

Setting the "JAVA_HOME" environment variable
To set the JAVA_HOME environment variable, which is needed for some programs, first find out the path of your Java installation:

sudo update-alternatives --config java
It returns something like:

There are 2 choices for the alternative java (providing /usr/bin/java).

Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      manual mode

Press enter to keep the current choice[*], or type selection number:
The path of the installation is for each:

/usr/lib/jvm/java-7-oracle

/usr/lib/jvm/java-6-openjdk-amd64

/usr/lib/jvm/java-7-oracle

Copy the path from your preferred installation and then edit the file /etc/environment:

sudo nano /etc/environment

In this file, add the following line (replacing YOUR_PATH by the just copied path):

JAVA_HOME="YOUR_PATH"
JAVA_HOME=” /usr/lib/jvm/java-7-openjdk-amd64”
That should be enough to set the environment variable. Now reload this file:

source /etc/environment

Test it by executing:

echo $JAVA_HOME


If it returns the just set path, the environment variable has been set successfully.