A better way to get codename of Debian
cmplstofB
Status: New User - Welcome
Joined: 31 Jul 2019
Posts: 2
Location: Osaka, Japan
Reply Quote
A recent Debian --- at least the stable and oldstable --- has a file /etc/os-release where you can get codename in easy way.
Here is example of /etc/os-release.
:: Code ::

PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"


Now, the official page ([link]) obtains codename in a complicated way combining pipes and regular expressions.
I think it is better way to get codename using the file /etc/os-release.

Thank you.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
I don't believe you understand what that code is doing. It's finding the ACTUAL Debian that is running, not the Debian listed in a static text file.

By looking into the apt configuration, it can see what the user actually has setup internaly, not what a static os-release file says is there.

I believe you're confusing the word 'better' for 'easier for me to understand and try to replace with a less robust solution'.
Back to top
cmplstofB
Status: New User - Welcome
Joined: 31 Jul 2019
Posts: 2
Location: Osaka, Japan
Reply Quote
I see.
Back to top
damentz
Status: Assistant
Joined: 09 Sep 2008
Posts: 1122
Reply Quote
Here's the codename variable assignment in particular with some annotations for you to read:

:: Code ::
codename="$(
    # Get current sources files
    find /etc/apt -type f -name '*.list' |\
   
    # Extract active binary deb lines
    xargs grep -E '^deb' |\
   
    # Cut line down to just the release
    awk '{print $3}' |
   
    # Cut everything after letter range, a-z (gets rid of -update, -security, -backport, etc)
    grep -Eo '^[a-z]+' |\
   
    # Get count of releases from least to most used
    sort | uniq -c | sort -n |\
   
    # Pick the last line (most commonly used)
    tail -n1 |\
   
    # Extract the release at the end of the line
    grep -Eo '[a-z]+$'
)"


This is done to determine the real distribution release behind the scenes. It's common for meta distributions to use Debian as their base but populate their sources files with the real Debian version for upstream packages.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
To make it explicitly clear:

:: Code ::
cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"


Then we try the real method:
:: Code ::
find /etc/apt -type f -name '*.list' | xargs grep -E '^deb' | awk '{print $3}' | grep -Eo '^[a-z]+' | sort | uniq -c | sort -n | tail -n1 | grep -Eo '[a-z]+$'
bullseye


What is this system actually? It's Debian Testing bullseye, not upgraded yet from buster to bullseye, but it is testing, not stable. And that is why you do not use /etc/os-release to determine the actual debian release.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours