Skip to content

Instantly share code, notes, and snippets.

@paulbdavis
Last active October 1, 2017 15:56
Show Gist options
  • Save paulbdavis/4744581 to your computer and use it in GitHub Desktop.
Save paulbdavis/4744581 to your computer and use it in GitHub Desktop.
Bash Script for getting your current Linux Kernel's Codename from the kernel source makefile. Uses a cache file so it does not do excessive curl calls (especially when run in something like conky)
#!/usr/bin/env bash
pattern="NAME\s\?=\s\?"
kernelVersion=$(uname -r | sed "s/-.*//g")
# strip trailing .0 version, since the git tags on kernel.org do not have them
kernelVersion=${kernelVersion/%".0"/}
cacheFile="$HOME/.kernelcodename"
if [ -f "$cacheFile" ]
then
cache=$(cat $cacheFile)
cachedVersion=$(expr "$cache" : '\([0-9.]*\)')
cachedName=$(expr "$cache" : '.*@\([-A-Za-z ]*\)')
# echo "v:$cachedVersion n:$cachedName"
if [ "$cachedVersion" = "$kernelVersion" ]
then
kernelCodename="$cachedName"
fi
fi
if [ ! "$kernelCodename" ]
then
url="http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/plain/Makefile?id=refs/tags/v${kernelVersion}"
kernelCodename=$(grep $pattern <<<"$(curl -s "$url")" | sed "s/$pattern//g")
fi
echo "$kernelVersion@$kernelCodename" > "$cacheFile"
if [ "$1" == "-a" ]
then
echo "$kernelCodename ($kernelVersion)"
else
echo "$kernelCodename"
fi
@kapsh
Copy link

kapsh commented Mar 6, 2016

Thanks for the script!
I thinks that getting codename should be standard feature of uname.

@ellery-lunafi
Copy link

Hi git.kernel.org has moved location of Makefile again. I got mine working by updating my url to be.

url="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/plain/Makefile?id=refs/tags/v${kernelVersion}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment