This script retrieves the operating system, version, and architecture of a Linux distribution. It accurately identifies the distribution using standard system files and commands, ensuring broad compatibility across various Linux systems.
git clone git@github.com:TirsvadCLI/Linux.Bash.Distribution.git
Alternatively, you can download the script directly with:
curl -L https://github.com/TirsvadCLI/Linux.Bash.Distribution/tarball/master | tar xz --strip-components=2 --wildcards '*/src/*' -C ./
The script primarily checks for the presence of /etc/os-release, a standard file in many Linux distributions that contains identification data for the operating system. If /etc/os-release is not found, the script will attempt to use the lsb_release command if it is available. It also checks other files such as /etc/lsb-release and /etc/debian_version for compatibility with various distributions like Debian and Ubuntu. As a last resort, the script defaults to using uname commands to ensure it can retrieve the necessary information across different systems.
The script sets the following environment variables:
-
TCLI_LINUX_BASH_DISTRIBUTION_ID
: A string containing the name of the distribution. -
TCLI_LINUX_BASH_DISTRIBUTION_RELEASE
: A string containing the version of the distribution. -
TCLI_LINUX_BASH_DISTRIBUTION_ARCH
: A string containing the architecture of the distribution.Can return folling values
x64
: AMD64 or Intel64x86
: IA32 or Intel32arm
: ARM and combatible variantsriscv64
: RISC-V 64-bit- $(uname -m) for all others arch
#!/bin/bash
declare -r PATH_ROOT=$(realpath $(dirname "$0"))
declare -r PATH_VENDOR="$PATH_ROOT/Vendor"
declare -r PATH_VENDOR_LINUX_DISTRIBUTION="$PATH_VENDOR/Linux.Bash.Distribution"
# Create the Vendor directory if it doesn't exist
[[ -d "$PATH_VENDOR_LINUX_DISTRIBUTION" ]] || mkdir -p "$PATH_VENDOR_LINUX_DISTRIBUTION"
cd "$PATH_VENDOR_LINUX_DISTRIBUTION"
# Download and extract the contents from GitHub into the Vendor directory
curl -L https://github.com/TirsvadCLI/Linux.Bash.Distribution/tarball/master | tar xzf - --strip-components=2 --wildcards '*/src/*'
. "$PATH_VENDOR_LINUX_DISTRIBUTION/Distribution/Run.sh"
cd "$PATH_ROOT"
echo "Distribution = $TCLI_LINUX_BASH_DISTRIBUTION_ID"
echo "Release = $TCLI_LINUX_BASH_DISTRIBUTION_RELEASE"
echo "Arch = $TCLI_LINUX_BASH_DISTRIBUTION_ARCH"
To create a Docker image for testing, use the following command:
docker-compose build
To run the test in the Docker image, execute:
ubuntu
docker run --rm -it tirsvadclilinuxbashdistribution_ubuntu_service
bash test_Distribution.sh
debian
docker run --rm -it tirsvadclilinuxbashdistribution_debian_service
bash test_Distribution.sh
See more here
See more here