imports/README.md
2025-06-03 17:02:14 +02:00

174 lines
5.1 KiB
Markdown

# imports
## `distros.sh`
### showcase
---
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"
```
So now that you have imported the [distros.sh](distros.sh) script, you can run some fancy things in your script..
### `distro-variables`
There are most of the big linux distros as a variable which contains boolean values. </br>
Also there is a 'distros' variable, containing the fancy distro name. More
| variable | values |
| --------- | ----------------------------------------------------------------------- |
| $distros | One value of `arch`, `debian`, `ubuntu`, `fedora`, `opensuse`, `alpine` |
| $arch | boolean - `true/false` |
| $debian | boolean - `true/false` |
| $ubuntu | boolean - `true/false` |
| $fedora | boolean - `true/false` |
| $opensuse | boolean - `true/false` |
| $alpine | boolean - `true/false` |
Some really simple examples
```bash
if $arch; then
echo "This system is using arch!"
elif $debian; then
echo "This system is using debian!"
fi
```
> or..
```bash
case "$distros" in
ubuntu)
echo "Using ubuntu!"
;;
arch)
echo "Using archlinux!"
;;
opensuse)
echo "Using opensuse!"
;;
esac
```
---
### **Package managing functions**
There are two big functions here to mention:
- **\_install()**
- **\_checkAndInstall()**
#### **\_install()**
You can simply install a package with the respected packagemanager.
On arch, it will install with either paru, yay or pacman (in this order) </br>
On debian/ubuntu, it will install with either nala or apt-get (in this order)
Fedora, opensuse and alpine will always use their respected packagemanager
**Usage:**
```bash
_install curl
```
> Will install curl
```bash
deps=(curl bash git zoxide)
_install "${deps[@]}"
```
> Will install everything in the `deps` array, but it will install it in one go!
#### **\_checkAndInstall()**
If you have many packages being installed, you may want to check if they are installed first, and then install them one by one via an array. You can do this easily with the `checkAndInstall` function. Just pass in an array like `${array[@]}` and it will auto check and install the packages in the array list one by one.
```bash
deps=(git wget curl vim zsh)
checkAndInstall "${deps[@]}"
```
> This will install the packages one by one, it will basically do..
>
> ```bash
> for pkg in "${deps[@]}"; do
> if ! command_exists $pkg; then
> _install $pkg
> else
> echo "$pkg is already installed"
> fi
> done
> ```
_**BONUS:**_
Since bash v5, you can define declared arrays. Which will make dependency installation for various systems extremely easy:
This is a snippet which i often use with this script sourced..:
```bash
# INFO:
# ╭─────────────────────────────────────────────────────────────────────────╮
# │ You can define dependencies for various linux distros here. It will │
# │ automagically be pulled via the $pkgArray[$distro] variable │
# ╰─────────────────────────────────────────────────────────────────────────╯
genericDeps=(virt-manager)
depsDebian=(qemu-system libvirt-dev libvirt-glib-1.0-dev)
depsFedora=(qemu libvirt)
depsOpensuse=(qemu libvirt)
depsArch=(qemu-full libvirt)
depsAlpine=()
declare -A deps=(
[debian]="depsDebian"
[ubuntu]="depsUbuntu"
[fedora]="depsFedora"
[opensuse]="depsOpensuse"
[arch]="depsArch"
[alpine]="depsAlpine"
)
# INFO:
# ╭────────────────────────────────────────────────────────────────╮
# │ This variable stores the packages you provided for each distro │
# ╰────────────────────────────────────────────────────────────────╯
declare -n pkgArray="${deps[$distro]}"
case "$distro" in
debian | ubuntu | arch | fedora | opensuse)
echo_info "Installing generic dependencies.."
checkAndInstall "${genericDeps[@]}"
echo_info "Installing special dependencies.."
checkAndInstall "${pkgArray[@]}"
;;
*)
echo_error "There are no dependencies to install for $distro"
return 69
;;
esac
```