Skip to content

Instantly share code, notes, and snippets.

@nicholasadamou
Created January 23, 2018 19:34
Show Gist options
  • Select an option

  • Save nicholasadamou/e18edb6b6d9143895029748fdbc85e08 to your computer and use it in GitHub Desktop.

Select an option

Save nicholasadamou/e18edb6b6d9143895029748fdbc85e08 to your computer and use it in GitHub Desktop.
OS utility functions.
#!/bin/bash
get_os() {
local os=""
local kernelName=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
kernelName="$(uname -s)"
if [ "$kernelName" == "Darwin" ]; then
os="macos"
elif [ "$kernelName" == "Linux" ] && [ -e "/etc/lsb-release" ]; then
if [ "$(cat < '/etc/lsb-release' | grep DISTRIB_ID | grep -o Ubuntu)" == "Ubuntu" ]; then
os="ubuntu"
elif [ "$(bash <(cat /etc/os-release; echo "echo ${ID/*, /}"))" == "kali" ]; then
os="kali-linux"
fi
else
os="$kernelName"
fi
printf "%s" "$os"
}
get_os_version() {
local os=""
local os_version=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
os="$(get_os)"
if [ "$os" == "macos" ]; then
os_version="$(sw_vers -productVersion)"
elif [ "$os" == "ubuntu" ]; then
os_version="$(lsb_release -d | cut -f2 | cut -d' ' -f2)"
elif [ "$os" == "kali-linux" ]; then
os_version="$(bash <(cat /etc/os-release; echo "echo ${VERSION/*, /}"))"
fi
printf "%s" "$os_version"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment