3.6 KiB
imports
distros.sh
Import the script inside your own script like this:
url="https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh"
import="$(mktemp)"
if command_exists curl; then
curl -fsSL $url -o $import
else
echo "curl is required, but missing.."
exit 1
fi
source "$import"
sleep 0.3
rm "$import"
DISTRIBUTION VARIABLES
After importing, these boolean variables are available for distribution checks:
$arch
- true/false for Arch Linux and derivatives
$debian
- true/false for Debian and derivatives
$ubuntu
- true/false for Ubuntu and derivatives
$fedora
- true/false for Fedora and derivatives
$opensuse
- true/false for openSUSE
$alpine
- true/false for Alpine Linux
$distro
- contains the detected distro name (e.g. "arch", "debian", "fedora"..)
Example usage:
if $arch; then
echo "Using Arch Linux!"
fi
case "$distro" in
arch) echo "Arch detected" ;;
debian) echo "Debian detected" ;;
esac
CORE FUNCTIONS
- Package Management:
_install pkg1 pkg2
- Installs packages using the correct package manager
_remove pkg1 pkg2
- Removes packages (CAUTION: no confirmation!)
checkAndInstall pkg1 pkg2
- Checks if command exists, installs if missing
Example:
_install neovim git
_remove nano
checkAndInstall curl wget
- Logging Functions:
echo_error "message" - Red error message
echo_warning "message" - Yellow warning message
echo_info "message" - Blue info message
echo_note "message" - Green note message
echo_pkg type "message" - Special package-related messages
Types for echo_pkg:
- build: Building messages
- clone: Cloning messages
- install: Installation messages
- deps: Dependency messages
Example:
echo_error "Failed to install!"
echo_pkg install "Installing package"
- Utility Functions:
command_exists cmd - Checks if command exists (silent) run cmd - Runs command with silent mode support source_script url - Sources another script from URL or file silentexec cmd - Runs command silently
Example:
if command_exists git; then
run git clone url
fi
source_script "https://example.com/script.sh"
DISTRIBUTION-SPECIFIC FEATURES
For Debian/Ubuntu:
- Version codename variables:
$trixie (Debian 13)
$bookworm (Debian 12)
$jammy (Ubuntu 22.04) $focal (Ubuntu 20.04)
For Fedora:
- Version variables like $fedora_40 for Fedora 40
For Arch:
- $aur - true if yay/paru is available
ADVANCED USAGE
- Handling dependencies per distro:
getDeps() {
local depsDebian=(git cmake)
local depsArch=(git base-devel)
declare -A deps=(
[debian]="depsDebian"
[arch]="depsArch"
)
declare -n pkgArray="${deps[$distro]}"
checkAndInstall "${pkgArray[@]}"
}
- Silent mode:
silent=true # Enable silent mode
_install pkg # Will install without output
- Conditional operations:
if ! command_exists node; then
echo_info "Installing Node.js..."
_install nodejs
fi
BEST PRACTICES
- Always check for commands before using them
- Use the provided echo functions for consistent output
- For distro-specific code, check the boolean variables first
- Use arrays with checkAndInstall for multiple packages
- Use source_script for modular script organization
TROUBLESHOOTING
If something doesn't work:
- Check if /etc/os-release exists
- Verify internet connection for remote scripts
- Check permissions (script handles sudo automatically)
- Verify $distro variable matches your system
- Check if required package managers are available