|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2021-2022 Gioele Barabucci |
| 4 | +# SPDX-License-Identifier: ISC |
| 5 | + |
| 6 | +set -eu |
| 7 | + |
| 8 | +export LC_ALL="C.UTF-8" |
| 9 | + |
| 10 | +help () { |
| 11 | + cat <<-EOD |
| 12 | + Usage: lsb_release [options] |
| 13 | +
|
| 14 | + Options: |
| 15 | + -h, --help show this help message and exit |
| 16 | + -v, --version show LSB modules this system supports |
| 17 | + -i, --id show distributor ID |
| 18 | + -d, --description show description of this distribution |
| 19 | + -r, --release show release number of this distribution |
| 20 | + -c, --codename show code name of this distribution |
| 21 | + -a, --all show all of the above information |
| 22 | + -s, --short show requested information in short format |
| 23 | + EOD |
| 24 | + exit |
| 25 | +} |
| 26 | + |
| 27 | +show_id=false |
| 28 | +show_desc=false |
| 29 | +show_release=false |
| 30 | +show_codename=false |
| 31 | +short_format=false |
| 32 | + |
| 33 | +options=$(getopt --name lsb_release -o hvidrcas -l help,version,id,description,release,codename,all,short -- "$@") || exit 2 |
| 34 | +eval set -- "$options" |
| 35 | +while [ $# -gt 0 ] ; do |
| 36 | + case "$1" in |
| 37 | + -h|--help) help ;; |
| 38 | + -v|--version) ;; |
| 39 | + -i|--id) show_id=true ;; |
| 40 | + -d|--description) show_desc=true ;; |
| 41 | + -r|--release) show_release=true ;; |
| 42 | + -c|--codename) show_codename=true ;; |
| 43 | + -a|--all) show_id=true ; show_desc=true ; show_release=true ; show_codename=true ;; |
| 44 | + -s|--short) short_format=true ;; |
| 45 | + *) break ;; |
| 46 | + esac |
| 47 | + shift |
| 48 | +done |
| 49 | + |
| 50 | +display_line () { |
| 51 | + label="$1" |
| 52 | + value="$2" |
| 53 | + |
| 54 | + if $short_format ; then |
| 55 | + printf "%s\n" "$value" |
| 56 | + else |
| 57 | + printf "%s:\t%s\n" "$label" "$value" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +# Load release info from standard identification data files |
| 62 | +[ -f /usr/lib/os-release ] && os_release=/usr/lib/os-release |
| 63 | +[ -f /etc/os-release ] && os_release=/etc/os-release |
| 64 | +[ "${LSB_OS_RELEASE-x}" != "x" ] && [ -f "$LSB_OS_RELEASE" ] && os_release="$LSB_OS_RELEASE" |
| 65 | +[ "${os_release-x}" != "x" ] && . "$os_release" |
| 66 | + |
| 67 | +# Mimic the output of Debian's Python-based lsb_release |
| 68 | +# Capitalize ID |
| 69 | +: "${ID=}" |
| 70 | +ID="$(printf "%s" "$ID" | cut -c1 | tr '[:lower:]' '[:upper:]')$(printf "%s" "$ID" | cut -c2-)" |
| 71 | +# Use NAME if set and different from ID only in capitalization. |
| 72 | +if [ "${NAME-x}" != "x" ] ; then |
| 73 | + lower_case_id=$(printf "%s" "$ID" | tr '[:upper:]' '[:lower:]') |
| 74 | + lower_case_name=$(printf "%s" "$NAME" | tr '[:upper:]' '[:lower:]') |
| 75 | + if [ "${lower_case_id}" = "${lower_case_name}" ] ; then |
| 76 | + ID="$NAME" |
| 77 | + fi |
| 78 | +fi |
| 79 | + |
| 80 | +# Generate minimal standard-conform output (if stdout is a TTY). |
| 81 | +[ -t 1 ] && echo "No LSB modules are available." >& 2 |
| 82 | + |
| 83 | +if $show_id ; then |
| 84 | + display_line "Distributor ID" "${ID:-n/a}" |
| 85 | +fi |
| 86 | + |
| 87 | +if $show_desc ; then |
| 88 | + display_line "Description" "${PRETTY_NAME:-n/a}" |
| 89 | +fi |
| 90 | + |
| 91 | +if $show_release ; then |
| 92 | + display_line "Release" "${VERSION_ID:-n/a}" |
| 93 | +fi |
| 94 | + |
| 95 | +if $show_codename ; then |
| 96 | + display_line "Codename" "${VERSION_CODENAME:-n/a}" |
| 97 | +fi |
0 commit comments