Automating Python and Java Installation on Linux Systems: Bash Scripting
- Omkar Kanase
- Jun 5, 2024
- 2 min read
Introduction
In this blog post, we'll guide you through the process of using a bash script to automate the installation of Python and Java on Linux systems. This script is designed to work with both yum (used on RHEL-based distributions) and apt (used on Debian-based distributions), making it versatile for different Linux environments. By automating these installations, you can save time and ensure consistency across multiple setups.
Step 1: Detecting the Operating System
detect_os() {
case "$(uname -s)" in
Linux*) OS=Linux;;
Darwin*) OS=Mac;;
CYGWIN*|MINGW*|MSYS*) OS=Windows;;
*) OS="UNKNOWN"
esac
echo "Detected OS: $OS"
}The detect_os function uses the uname -s command to identify the operating system and sets the OS variable accordingly. It handles various possible outputs using a case statement and provides a default value of "UNKNOWN" for unrecognized systems. Finally, it prints the detected OS to the terminal. This function is essential for adapting the script's behavior based on the operating system, ensuring compatibility and proper functionality.
Sample Output of Operating System Detection:

Step 2: Running Commands with Error Handling
run_command() {
command=$1
echo "Running: $command"
if eval "$command"; then
echo "Success: $command"
else
echo "Error: $command"
exit 1
fi
}The run_command function is designed to execute a given command and handle any errors that may occur during its execution.
Step 3: Installing Python
Installing Python (Yum):
install_python_yum()
{
run_command "sudo yum update -y"
run_command "sudo yum install -y python3 python3-pip"
}Installs Python and pip using yum package manager for RHEL-based distributions.
2. Installing Python (Apt):
install_python_apt() {
run_command "sudo apt update -y"
run_command "sudo apt install -y python3 python3-pip"
} Installs Python and pip using apt package manager for Debian-based distributions.
Sample Output of Python Installation:

Step 3: Installing Java
Installing Java (Yum):
install_java_yum()
{
run_command "sudo yum install -y java-11-openjdk-devel"
}Installs Java using yum package manager for RHEL-based distributions.
2. Installing Java (Apt):
install_java_apt()
{
run_command "sudo apt update -y"
run_command "sudo apt install -y openjdk-11-jdk"
}Installs Java using apt package manager for Debian-based distributions.
Sample Output of Java Installation:




Comments