A bash script that automates Java (OpenJDK 17) installation on Ubuntu/Debian systems and validates the installation by checking three conditions:
- Whether Java is installed at all
- Whether an older version (below 11) is installed
- Whether Java 11 or higher was successfully installed
- Ubuntu/Debian-based Linux (tested on Ubuntu 24.04 via WSL)
sudoprivileges- Vim (optional, for editing)
chmod +x install_java.sh
./install_java.shDetected java version: 17.0.18
SUCCESS: Java 17.0.18 (11 or higher) is installed
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 -versionoutputs to stderr, not stdout, so2>&1redirects it for processingawk -F '"' '{print $2}'splits the version output by quote characters to extract the version stringawk -F '.' '{print $1}'extracts the major version number for comparison[ "$major_version" -lt 11 ]uses bash's integer comparison (-lt= less than)
This was built as a learning exercise covering foundational DevOps and SWE concepts. Full notes below.
wsl -d Ubuntuopens 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 rootflag logs in as superuser, useful for password resets viapasswd <username>- Work in
~(Linux home) instead of/mnt/c/...to avoid permission and line-ending issues (\r\nvs\n)
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 PATHsudoelevates to superuser.cat <file>prints file contents- File creator automatically gets read/write permissions (default
rw-r--r--)
apt(Ubuntu/Debian),winget(Windows),brew(Mac) are CLI tools for installing, updating, and removing softwaresudo apt updaterefreshes available package listssudo apt install -y <package>installs with auto-confirmapt search <name>finds available packages- Package managers are one of several installation methods (others: GUI installers, manual downloads)
- 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 -versionoutputs to stderr, which is an exception to normal command behavior
ienters insert mode,Escreturns to command mode:wqsaves and quits,:q!quits without savingxdeletes a character in command mode:%s/old/new/gis 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.
#!/bin/bash(shebang) declares the interpretervar=$(command)captures command output. No spaces around=$varretrieves the value.$is the dereference operator- Exit codes: 0 = success, non-zero = failure.
!inverts the result if/then/else/fi: everyifrequires a closingfi. Nested ifs need their ownfi[ "$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
2>&1redirects stderr to stdout> /dev/nulldiscards output|pipes one command's stdout into the next command's stdinecho "$var" | awk ...converts a variable to text input for processing
- Named after creators Aho, Weinberger, Kernighan
-Fsets the field separator$1,$2etc. reference fields after splitting (awk syntax, not bash)'{print $2}'uses single quotes so bash passes$2literally to awk instead of interpreting it{}is the action block.printoutputs the value. Withoutprint, awk computes but produces no output- Single quotes
''= literal string. Double quotes""= allows variable expansion
ssh-keygen -t ed25519generates 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 loginwith SSH protocol is the standard for personal dev environments- In production, Personal Access Tokens (PATs) are used for CI/CD and automated workflows
Built as part of a self-directed SWE/DevOps learning path - TechWorldNana Bootcamp