Skip to content

Instantly share code, notes, and snippets.

@skx
Last active August 20, 2023 14:31
Show Gist options
  • Save skx/1f9e5240856c5976193a to your computer and use it in GitHub Desktop.
Save skx/1f9e5240856c5976193a to your computer and use it in GitHub Desktop.
Simple alternative to /usr/bin/lsb_release
#!/bin/sh
#
# This is a simple alternative to /usr/bin/lsb_release which
# doesn't require python.
#
# If /etc/os-release exists then we use that to output data
# in a compatible format to the original lsb_release utility.
#
# I consider this script trivial enough to be in the public-domain,
# but any patches or suggestsions will be welcome.
#
# Steve
# --
#
show_help() {
cat << EOF
Usage: lsb_release [options]
Options:
-h, --help show this help message and exit
-i, --id show distributor ID
-d, --description show description of this distribution
-r, --release show release number of this distribution
-c, --codename show code name of this distribution
-a, --all show all of the above information
-s, --short show requested information in short format
EOF
exit 0
}
#
# Potential command-line options.
#
all=0
codename=0
description=0
id=0
release=0
short=0
version=0
#
# Process each argument, and set the appropriate flag
# if we recognize it.
#
while :; do
case $1 in
-a|--all)
all=1
;;
-c|--codename)
codename=1
;;
-d|--description)
description=1
;;
-h|--help)
show_help
break
;;
-i|--id)
id=1
;;
-r|--release)
release=1
;;
-s|--short)
short=1
;;
-v|--version)
version=1
;;
*)
break
esac
shift
done
#
# Read our variables.
#
if [ -e /etc/os-release ]; then
. /etc/os-release
else
echo "/etc/os-release is not present. Aborting" >&2
exit 1
fi
#
# Now output the data - The order of these was chosen to match
# what the original lsb_release used, and while I suspect it doesn't
# matter I kept it the same.
#
if [ "$all" = "1" ] || [ "$id" = "1" ]; then
if [ "$short" = "0" ]; then
printf "Distributor ID:\t"
fi
echo $ID
fi
if [ "$all" = "1" ] || [ "$description" = "1" ]; then
if [ "$short" = "0" ]; then
printf "Description:\t"
fi
echo $PRETTY_NAME
fi
if [ "$all" = "1" ] || [ "$release" = "1" ]; then
if [ "$short" = "0" ]; then
printf "Release:\t"
fi
echo $VERSION_ID
fi
if [ "$all" = "1" ] || [ "$codename" = "1" ]; then
if [ "$short" = "0" ]; then
printf "Codename:\t"
fi
#
# Codename comes from: VERSION="7 (wheezy)"
#
ver=$(echo $VERSION | awk '{print $2}' | tr -d \(\))
echo "$ver"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment