I'm working on a simple bash script that should be able to run on Ubuntu and CentOS distributions (support for Debian and Fedora/RHEL would be a plus) and I need to know the name and version of the distribution the script is running (in order to trigger specific actions, for instance the creation of repositories). So far what I've got is this:
OS=$(awk '/DISTRIB_ID=/' /etc/*-release | sed 's/DISTRIB_ID=//' | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VERSION=$(awk '/DISTRIB_RELEASE=/' /etc/*-release | sed 's/DISTRIB_RELEASE=//' | sed 's/[.]0/./')
if [ -z "$OS" ]; then
OS=$(awk '{print $1}' /etc/*-release | tr '[:upper:]' '[:lower:]')
fi
if [ -z "$VERSION" ]; then
VERSION=$(awk '{print $3}' /etc/*-release)
fi
echo $OS
echo $ARCH
echo $VERSION
This seems to work, returning
ubuntu
or centos
(I haven't tried others) as the release name however, I have a feeling that there must be an easier, more reliable way of finding this out... No?
It doesn't work for RedHat. /etc/redhat-release contains : Redhat Linux Entreprise release 5.5
So, the version is not the third word, you'd better use :
OS_MAJOR_VERSION=`sed -rn 's/.*([0-9])\.[0-9].*/\1/p' /etc/redhat-release`
OS_MINOR_VERSION=`sed -rn 's/.*[0-9].([0-9]).*/\1/p' /etc/redhat-release`
echo "RedHat/CentOS $OS_MAJOR_VERSION.$OS_MINOR_VERSION"
ANSWER:-
Most recent distributions have a tool calledlsb_release
. Your/etc/*-release
will be using/etc/lsb-release
anyway, so if that file is there, runninglsb_release
should work too.I thinkuname
to getARCH
is still the best way.e.g.OS=$(lsb_release -si) ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/') VER=$(lsb_release -sr)
Or you could just source/etc/lsb-release
:. /etc/lsb-release OS=$DISTRIB_ID ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/') VER=$DISTRIB_RELEASE
If you have to be compatible with older distributions, there is no single file you can rely on. Either fall back to the output fromuname
, e.g.OS=$(uname -s) ARCH=$(uname -m) VER=$(uname -r)
or handle each distribution separately:if [ -f /etc/debian_version ]; then OS=Debian # XXX or Ubuntu?? VER=$(cat /etc/debian_version) elif [ -f /etc/redhat-release ]; then ...
Of course, you can combine all this:ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/') if [ -f /etc/lsb-release ]; then . /etc/lsb-release OS=$DISTRIB_ID VER=$DISTRIB_RELEASE elif [ -f /etc/debian_version ]; then OS=Debian # XXX or Ubuntu?? VER=$(cat /etc/debian_version) elif [ -f /etc/redhat-release ]; then # TODO add code for Red Hat and CentOS here ... else OS=$(uname -s) VER=$(uname -r) fi
Finally, yourARCH
obviously only handles Intel systems. I'd either call itBITS
like this:case $(uname -m) in x86_64) BITS=64 ;; i*86) BITS=32 ;; *) BITS=? ;; esac
Or changeARCH
to be the more common, yet unambiguous versions:x86
andx64
or similar:case $(uname -m) in x86_64) ARCH=x64 # or AMD64 or Intel64 or whatever ;; i*86) ARCH=x86 # or IA32 or Intel32 or whatever ;; *) # leave ARCH as-is ;; esac
but of course that's up to you.
0 comments:
Post a Comment
Don't Forget to comment