185 lines
3.8 KiB
Markdown
185 lines
3.8 KiB
Markdown
# imports
|
|
|
|
## distros.sh
|
|
|
|
Import the script inside your own script like this:
|
|
|
|
```bash
|
|
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 </br>
|
|
`$debian` - true/false for Debian and derivatives </br>
|
|
`$ubuntu` - true/false for Ubuntu and derivatives </br>
|
|
`$fedora` - true/false for Fedora and derivatives </br>
|
|
`$opensuse` - true/false for openSUSE </br>
|
|
`$alpine` - true/false for Alpine Linux </br>
|
|
`$distro` - contains the detected distro name (e.g. "arch", "debian", "fedora"..)
|
|
|
|
> [!TIP]
|
|
>
|
|
> `$_sudo` variable is set, if the user is not root, and sudo exists on the system. </br>
|
|
> So you can just use this syntax:
|
|
>
|
|
> ```bash
|
|
> $_sudo make install
|
|
> ```
|
|
|
|
Example usage:
|
|
|
|
```bash
|
|
if $arch; then
|
|
echo "Using Arch Linux!"
|
|
fi
|
|
|
|
case "$distro" in
|
|
arch) echo "Arch detected" ;;
|
|
debian) echo "Debian detected" ;;
|
|
esac
|
|
```
|
|
|
|
### CORE FUNCTIONS
|
|
|
|
0.1 Permissions/sudo:
|
|
|
|
---
|
|
|
|
1. Package Management:
|
|
|
|
`_install pkg1 pkg2` - Installs packages using the correct package manager </br>
|
|
`_remove pkg1 pkg2` - Removes packages (CAUTION: no confirmation!) </br>
|
|
|
|
`checkAndInstall pkg1 pkg2` - Checks if command exists, installs if missing
|
|
|
|
Example:
|
|
|
|
```bash
|
|
_install neovim git
|
|
_remove nano
|
|
checkAndInstall curl wget
|
|
```
|
|
|
|
2. Logging Functions:
|
|
|
|
`echo_error` "message" - Red error message </br>
|
|
`echo_warning` "message" - Yellow warning message </br>
|
|
`echo_info` "message" - Blue info message </br>
|
|
`echo_note` "message" - Green note message </br>
|
|
`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:
|
|
|
|
```bash
|
|
echo_error "Failed to install!"
|
|
echo_pkg install "Installing package"
|
|
```
|
|
|
|
3. Utility Functions:
|
|
|
|
`command_exists cmd` - Checks if command exists (silent) </br>
|
|
`run cmd` - Runs command with silent mode support </br>
|
|
`source_script url` - Sources another script from URL or file </br>
|
|
`silentexec cmd` - Runs command silently
|
|
|
|
Example:
|
|
|
|
```bash
|
|
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
|
|
|
|
1. Handling dependencies per distro:
|
|
|
|
```bash
|
|
getDeps() {
|
|
local depsDebian=(git cmake)
|
|
local depsArch=(git base-devel)
|
|
|
|
declare -A deps=(
|
|
[debian]="depsDebian"
|
|
[arch]="depsArch"
|
|
)
|
|
|
|
declare -n pkgArray="${deps[$distro]}"
|
|
checkAndInstall "${pkgArray[@]}"
|
|
}
|
|
```
|
|
|
|
2. Silent mode:
|
|
|
|
```bash
|
|
silent=true # Enable silent mode
|
|
_install pkg # Will install without output
|
|
```
|
|
|
|
3. Conditional operations:
|
|
|
|
```bash
|
|
if ! command_exists node; then
|
|
echo_info "Installing Node.js..."
|
|
_install nodejs
|
|
fi
|
|
```
|
|
|
|
### BEST PRACTICES
|
|
|
|
1. Always check for commands before using them
|
|
2. Use the provided echo functions for consistent output
|
|
3. For distro-specific code, check the boolean variables first
|
|
4. Use arrays with checkAndInstall for multiple packages
|
|
5. Use `source_script` for modular script organization
|
|
|
|
### TROUBLESHOOTING
|
|
|
|
If something doesn't work:
|
|
|
|
1. Check if `/etc/os-release` exists
|
|
2. Verify internet connection for remote scripts
|
|
3. Check permissions (script handles sudo automatically)
|
|
4. Verify `$distro` variable matches your system
|
|
5. Check if required package managers are available
|