Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

bash-java-installer

A bash script that automates Java (OpenJDK 17) installation on Ubuntu/Debian systems and validates the installation by checking three conditions:

  1. Whether Java is installed at all
  2. Whether an older version (below 11) is installed
  3. Whether Java 11 or higher was successfully installed

Prerequisites

  • Ubuntu/Debian-based Linux (tested on Ubuntu 24.04 via WSL)
  • sudo privileges
  • Vim (optional, for editing)

Usage

chmod +x install_java.sh
./install_java.sh

Expected Output (Success)

Detected java version: 17.0.18
SUCCESS: Java 17.0.18 (11 or higher) is installed

How It Works

The script runs sudo apt install -y openjdk-17-jdk, then uses awk to extract the major version number from java -version output and evaluates it against the three conditions using bash conditionals.

Key implementation details:

  • java -version outputs to stderr, not stdout, so 2>&1 redirects it for processing
  • awk -F '"' '{print $2}' splits the version output by quote characters to extract the version string
  • awk -F '.' '{print $1}' extracts the major version number for comparison
  • [ "$major_version" -lt 11 ] uses bash's integer comparison (-lt = less than)

Learnings

This was built as a learning exercise covering foundational DevOps and SWE concepts. Full notes below.

WSL (Windows Subsystem for Linux)

  • wsl -d Ubuntu opens an Ubuntu terminal inside Windows without needing a VM
  • WSL is isolated from Windows: it can read Windows files at /mnt/c/ but cannot control Windows programs
  • -u root flag logs in as superuser, useful for password resets via passwd <username>
  • Work in ~ (Linux home) instead of /mnt/c/... to avoid permission and line-ending issues (\r\n vs \n)

Linux Fundamentals

  • cd ~ navigates to home directory (/home/<username>)
  • chmod +x <file> adds execute permission. Without a user prefix (u+x, g+x, o+x), it applies to all
  • ./<filename> runs a script from the current directory. Without ./, bash only searches PATH
  • sudo elevates to superuser. cat <file> prints file contents
  • File creator automatically gets read/write permissions (default rw-r--r--)

Package Managers

  • apt (Ubuntu/Debian), winget (Windows), brew (Mac) are CLI tools for installing, updating, and removing software
  • sudo apt update refreshes available package lists
  • sudo apt install -y <package> installs with auto-confirm
  • apt search <name> finds available packages
  • Package managers are one of several installation methods (others: GUI installers, manual downloads)

Java

  • Java 17 and 21 are the current LTS versions preferred in production
  • Adoptium/Temurin is the standard free distribution (Oracle JDK has commercial licensing restrictions)
  • java -version outputs to stderr, which is an exception to normal command behavior

Vim

  • i enters insert mode, Esc returns to command mode
  • :wq saves and quits, :q! quits without saving
  • x deletes a character in command mode
  • :%s/old/new/g is find-and-replace across the whole file (% = all lines, g = all occurrences per line)
  • Vim is a text editor only. It does not execute code.

Bash Scripting

  • #!/bin/bash (shebang) declares the interpreter
  • var=$(command) captures command output. No spaces around =
  • $var retrieves the value. $ is the dereference operator
  • Exit codes: 0 = success, non-zero = failure. ! inverts the result
  • if/then/else/fi: every if requires a closing fi. Nested ifs need their own fi
  • [ "$var" -lt 11 ] is bash's test syntax. Spaces inside [ ] are mandatory
  • Comparison operators: -lt, -gt, -eq, -le, -ge (bash uses these because < > are redirect operators)
  • Indentation is optional but standard for readability

Redirection and Pipes

  • 2>&1 redirects stderr to stdout
  • > /dev/null discards output
  • | pipes one command's stdout into the next command's stdin
  • echo "$var" | awk ... converts a variable to text input for processing

awk

  • Named after creators Aho, Weinberger, Kernighan
  • -F sets the field separator
  • $1, $2 etc. reference fields after splitting (awk syntax, not bash)
  • '{print $2}' uses single quotes so bash passes $2 literally to awk instead of interpreting it
  • {} is the action block. print outputs the value. Without print, awk computes but produces no output
  • Single quotes '' = literal string. Double quotes "" = allows variable expansion

SSH and GitHub Authentication

  • ssh-keygen -t ed25519 generates a key pair (private + public)
  • Private key (~/.ssh/id_ed25519): never shared, never moved
  • Public key (id_ed25519.pub): uploaded to GitHub for authentication
  • gh auth login with SSH protocol is the standard for personal dev environments
  • In production, Personal Access Tokens (PATs) are used for CI/CD and automated workflows

Author

Built as part of a self-directed SWE/DevOps learning path - TechWorldNana Bootcamp

About

Automates OpenJDK 17 installation on Ubuntu/Debian with three-condition version validation

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages