Je dois trouver le nom et la version du système d'exploitation sur la plate-forme Unix/Linux. Pour cela j'ai essayé de suivre:
lsb_release
utilitaire
/etc/redhat-release
ou fichier spécifique
Mais cela ne semble pas être la meilleure solution car LSB_RELEASE ne prend plus en charge RHEL 7.
Existe-t-il un moyen qui fonctionne sur toutes les plateformes Unix ou Linux?
Toute aide serait grandement appréciée.
Cela fonctionne bien pour tous les environnements Linux.
#!/bin/sh
cat /etc/*-release
Dans Ubuntu:
$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS"
ou 12.04:
$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
NAME="Ubuntu"
VERSION="12.04.4 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.4 LTS)"
VERSION_ID="12.04"
À RHEL:
$ cat /etc/*-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Ou utilisez ce script:
#!/bin/sh
# Detects which OS and if it is Linux then it will detect which Linux
# Distribution.
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`
GetVersionFromFile()
{
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}
if [ "${OS}" = "SunOS" ] ; then
OS=Solaris
Arch=`uname -p`
OSSTR="${OS} ${REV}(${Arch} `uname -v`)"
Elif [ "${OS}" = "AIX" ] ; then
OSSTR="${OS} `oslevel` (`oslevel -r`)"
Elif [ "${OS}" = "Linux" ] ; then
KERNEL=`uname -r`
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
Elif [ -f /etc/SuSE-release ] ; then
DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
Elif [ -f /etc/Mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=`cat /etc/Mandrake-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/Mandrake-release | sed s/.*release\ // | sed s/\ .*//`
Elif [ -f /etc/debian_version ] ; then
DIST="Debian `cat /etc/debian_version`"
REV=""
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
fi
echo ${OSSTR}
La commande suivante a bien fonctionné pour moi. Il vous donne le nom et la version du système d'exploitation.
lsb_release -a
La commande "lsb_release" fournit certaines informations spécifiques à Linux Standard Base et à la distribution . Vous pouvez donc obtenir la commande ci-dessous afin d'obtenir le nom et la version du système d'exploitation.
" lsb_release -a "
Avec Perl et Linux :: Distribution , la solution la plus propre à un problème ancien:
#!/bin/sh
Perl -e '
use Linux::Distribution qw(distribution_name distribution_version);
my $linux = Linux::Distribution->new;
if(my $distro = $linux->distribution_name()) {
my $version = $linux->distribution_version();
print "you are running $distro";
print " version $version" if $version;
print "\n";
} else {
print "distribution unknown\n";
}
'
Dans chaque distribution, il y a des fichiers de différences, donc j'écris les plus courants:
---- CentOS Linux distro
`cat /proc/version`
---- Debian Linux distro
`cat /etc/debian_version`
---- Redhat Linux distro
`cat /etc/redhat-release`
---- Ubuntu Linux distro
`cat /etc/issue` or `cat /etc/lsb-release`
dans le dernier/etc/issue n’existait pas alors j’ai essayé le second et il a renvoyé la bonne réponse
Ma propre prise à script de @ kvivek , avec plus facilement une sortie analysable par machine:
#!/bin/sh
# Outputs OS Name, Version & misc. info in a machine-readable way.
# See also NeoFetch for a more professional and elaborate bash script:
# https://github.com/dylanaraps/neofetch
SEP=","
PRINT_HEADER=false
print_help() {
echo "`basename $0` - Outputs OS Name, Version & misc. info"
echo "in a machine-readable way."
echo
echo "Usage:"
echo " `basename $0` [OPTIONS]"
echo "Options:"
echo " -h, --help print this help message"
echo " -n, --names print a header line, naming the fields"
echo " -s, --separator SEP overrides the default field-separator ('$SEP') with the supplied one"
}
# parse command-line args
while [ $# -gt 0 ]
do
arg="$1"
shift # past switch
case "${arg}" in
-h|--help)
print_help
exit 0
;;
-n|--names)
PRINT_HEADER=true
;;
-s|--separator)
SEP="$1"
shift # past value
;;
*) # non-/unknown option
echo "Unknown switch '$arg'" >&2
print_help
;;
esac
done
OS=`uname -s`
DIST="N/A"
REV=`uname -r`
MACH=`uname -m`
PSUEDONAME="N/A"
GetVersionFromFile()
{
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}
if [ "${OS}" = "SunOS" ] ; then
DIST=Solaris
DIST_VER=`uname -v`
# also: cat /etc/release
Elif [ "${OS}" = "AIX" ] ; then
DIST="${OS}"
DIST_VER=`oslevel -r`
Elif [ "${OS}" = "Linux" ] ; then
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/redhat-release `
DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/redhat-release `
Elif [ -f /etc/SuSE-release ] ; then
DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
DIST_VER=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
Elif [ -f /etc/Mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/Mandrake-release`
DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/Mandrake-release`
Elif [ -f /etc/debian_version ] ; then
DIST="Debian"
DIST_VER=`cat /etc/debian_version`
PSUEDONAME=`lsb_release -a 2> /dev/null | grep '^Codename:' | sed -e 's/.*[[:space:]]//'`
#Elif [ -f /etc/gentoo-release ] ; then
#TODO
#Elif [ -f /etc/slackware-version ] ; then
#TODO
Elif [ -f /etc/issue ] ; then
# We use this indirection because /etc/issue may look like
# "Debian GNU/Linux 10 \n \l"
ISSUE=`cat /etc/issue`
ISSUE=`echo -e "${ISSUE}" | head -n 1 | sed -e 's/[[:space:]]\+$//'`
DIST=`echo -e "${ISSUE}" | sed -e 's/[[:space:]].*//'`
DIST_VER=`echo -e "${ISSUE}" | sed -e 's/.*[[:space:]]//'`
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
# NOTE `sed -e 's/.*(//' -e 's/).*//' /proc/version`
# is an option that worked ~ 2010 and earlier
fi
if $PRINT_HEADER
then
echo "OS${SEP}Distribution${SEP}Distribution-Version${SEP}Pseudo-Name${SEP}Kernel-Revision${SEP}Machine-Architecture"
fi
echo "${OS}${SEP}${DIST}${SEP}${DIST_VER}${SEP}${PSUEDONAME}${SEP}${REV}${SEP}${MACH}"
NOTE: Testé uniquement sur Debian 11
osInfo
sortie:
Linux,Debian,10.0,buster,4.19.0-5-AMD64,x86_64
osInfo --names -s "\t| "
sortie:
OS | Distribution | Distribution-Version | Pseudo-Name | Kernel-Revision | Machine-Architecture
Linux | Debian | 10.0 | buster | 4.19.0-5-AMD64 | x86_64
osInfo | awk -e 'BEGIN { FS=","; } { print $2 " " $3 " (" $4 ")" }'
sortie:
Debian 10.0 (buster)
avec guillemets:
cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g'
donne la sortie comme:
"CentOS Linux 7 (Core)"
sans guillemets:
cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/"//g'
donne la sortie comme:
CentOS Linux 7 (Core)
Utilisez la commande suivante
lsb_release -irc
Mon système d'exploitation est Centos et m'a donné comme
Distributor ID: CentOS
Description: CentOS Linux release 7.2.1511 (Core)
Release: 7.2.1511
Codename: Core
cette commande vous donne une description de votre système d'exploitation
cat /etc/os-release