From d34792d34d4e44466771e72dc56afa1b724c6eeb Mon Sep 17 00:00:00 2001 From: askiiart Date: Tue, 22 Aug 2023 22:12:01 -0500 Subject: [PATCH] Add minor files --- random-bash.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ test.bash | 10 +++++++++ 2 files changed, 67 insertions(+) create mode 100644 random-bash.md create mode 100755 test.bash diff --git a/random-bash.md b/random-bash.md new file mode 100644 index 0000000..ce4ea8a --- /dev/null +++ b/random-bash.md @@ -0,0 +1,57 @@ +# Random bash + +Here's random useful bash stuff: + +## Check if program is installed + +Checks if `$1` is installed + +```bash +#!/usr/bin/env bash +command_exists() { type "$1" &> /dev/null; } + +if command_exists "$1"; then + echo "$1 installed" +else + echo "$1 not installed" + exit 1 +fi +``` + +### Check which package manager is installed + +```bash +#!/usr/bin/env bash +command_exists() { type "$1" &> /dev/null; } + +if command_exists "apt-get"; then + PM="apt-get" +elif command_exists "yum"; then + PM="yum" +elif command_exists "pacman"; then + PM="pacman" +elif command_exists "zypper"; then + PM="zypper" +elif command_exists "emerge"; then + PM="emerge" +elif command_exists "apk"; then + PM="apk" +else + >&2 echo "Unsupported: unknown package manager" + exit 1 +fi + +echo "Package manager: ${PM}" +``` + +## Check if root + +This exits with and error message if it's run as root. + +```bash +#!/bin/bash +if [ $(whoami) != "root" ]; then + >&2 echo Rerun as non-root user + exit 1 +fi +``` \ No newline at end of file diff --git a/test.bash b/test.bash new file mode 100755 index 0000000..46361b3 --- /dev/null +++ b/test.bash @@ -0,0 +1,10 @@ +#!/bin/bash +command_exists() { type "$1" &> /dev/null; } + +if command_exists "apt-get"; then + PM="apt-get" +elif command_exists "yum"; then + PM="yum" +fi + +echo $PM \ No newline at end of file