Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Friday, 24 April 2015

Installing Sun-Java JDK Using Bash Script :: Ubuntu

Getting Started

We are going to write simple bash script and execute it to install Sun Java. We will write content to text file using any of your favorite test editor. give it permission to execute and enjoy. hope it  works without error, else let me know issue to update it, it works fine for me on 2 different machines running Linux mint 17 and Ubuntu 14.04 respectively.

What is Bash?

Descended from the Bourne Shell, Bash is a GNU product, the "Bourne Again SHell." It's the standard command line interface on most Linux machines. It excels at interactivity, supporting command line editing, completion, and recall. It also supports configurable prompts - most people realize this, but don't know how much can be done.

Writing Script:

To write script for installation open new file named install_java.sh and put following content in it.
#!/bin/bash

# Script to install Sun Java

clear

# This command is used to tell shell, turn installation mode to non interactive
# and set auto selection of agreement for Sun Java
export DEBIAN_FRONTEND=noninteractive
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections


echo "Bash Script for Installing Sun Java for Ubuntu!"

echo "Now Script will try to purge OpenJdk if installed..."

# purge openjdk if installed to remove conflict
sudo apt-get purge openjdk-\* -y

echo "Now we will update repository..."

sudo apt-get update -y

echo "Adding Java Repository...."

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

echo "Updating Repository to load java repository"

sudo apt-get update -y

echo "Installing Sun Java....."
sudo -E apt-get purge oracle-java7-installer -y
sudo -E apt-get install oracle-java7-installer -y

echo "Installation completed...."

echo "Installed java version is...."

java -version
   

Now we will give it permission to make it executable.
 ~$ chmod 755 install_java.sh

Now we can execute script using following command, Since installation require root excess and use sudo, script may prompt to ask password. provide password to initiate installation process.
 ~$ ./install_java.sh  


Enjoy.

How to write Bash Script Basic (Shell Script)

Get Started

To automate installations and configurations or routine task, we are required to write scripts to run using bash. Here we will learn how to write simple bash script and make it executable.

What is Bash?

Descended from the Bourne Shell, Bash is a GNU product, the "Bourne Again SHell." It's the standard command line interface on most Linux machines. It excels at interactivity, supporting command line editing, completion, and recall. It also supports configurable prompts - most people realize this, but don't know how much can be done. 

Write your first script

To write a bash script process include,
  • Writing bash script
  • Give it Execution permission to make it executable

Script:

You need to write to any text file, you may use any of your favorite text editor, I will use nano for this post to stop arguments from Emacs or vim(vi). you may use any. shell script is a simple file that contain ASCII text content. Now we will write simple script.
 ~$ nano hello_test.sh

Now put following content in file and save and close (For nano CTRL+O Enter CTRL+X)
 #!/bin/bash
 # My Hello World Script
 echo "Hello World!"

We have written a very simple ever common "Hello World" Program, This script simple printout Hello World! and quit. first line in a script is written to tell shell, which program is used to interpret this script. in this case we are using bash, this path may change if your bash is inst alled in some other location to check your bash path simple type following command.
 ~$ which bash
 /bin/bash

Second line is a comment, every content that is appear after # is skipped by bash. Comments are highly recommended to make your script readable. echo command is used to print in bash.

Give Permission:

Now script is ready, we will give it execution permission. use following command to give script permission.
 ~$ chmod 755 hello_test.sh

Now we can test our script by executing it from shell using following command.
 ~$ ./hello_test.sh

Enjoy.