Est-il possible de détecter 32 bits contre 64 bits dans un script bash? [dupliquer]

StackOverflow https://stackoverflow.com/questions/106387

  •  01-07-2019
  •  | 
  •  

Question

    

Cette question a déjà une réponse ici:

         

J'écris un script bash pour gérer certaines installations de manière automatisée ... J'ai la possibilité d'obtenir un tel programme en binaire 32 ou 64 bits ... est-il possible de détecter l'architecture de la machine à partir de bash Je peux choisir le bon binaire?

Ce sera pour les machines Ubuntu.

Était-ce utile?

La solution

Est-ce que

uname -a

vous donner tout ce que vous pouvez utiliser? Je n'ai pas de machine 64 bits à tester.

Note de Mike Stone: Cela fonctionne, bien que ce soit spécifiquement

uname -m

Donnera " x86_64 " pour 64 bits et autre chose pour les autres types 32 bits (dans ma machine virtuelle 32 bits, il s’agit du "i686").

Autres conseils

MACHINE_TYPE=`uname -m`
if [ ${MACHINE_TYPE} == 'x86_64' ]; then
  # 64-bit stuff here
else
  # 32-bit stuff here
fi

getconf LONG_BIT semble également faire l'affaire, ce qui rend encore plus facile la vérification car ceci retourne simplement l'entier au lieu d'une expression compliquée.

if [ `getconf LONG_BIT` = "64" ]
then
    echo "I'm 64-bit"
else
    echo "I'm 32-bit"
fi

Attention, dans un envoi 32 bits chroot , l'uname répond toujours comme le système hôte 64 bits.

getconf LONG_BIT fonctionne correctement.

fichier / bin / cp ou tout exécutable ou bibliothèque connu devrait faire l'affaire si vous n'avez pas getconf (mais vous pouvez stocker des programmes que vous ne pouvez pas utiliser, et peut-être qu'il pas à cet endroit).

Vous pouvez utiliser le script suivant (je l'extrais du script officiel de "ioquake3"): par exemple

archs=`uname -m`
case "$archs" in
    i?86) archs=i386 ;;
    x86_64) archs="x86_64 i386" ;;
    ppc64) archs="ppc64 ppc" ;;
esac

for arch in $archs; do
    test -x ./ioquake3.$arch || continue
    exec ./ioquake3.$arch "$@"
done

============================================ ===================================== <= p>

Je suis en train de créer un script pour détecter l’architecture, c’est mon code simple (je l’utilise avec wine, pour mes jeux Windows, sous Linux, pour chaque jeu, j’utilise une version différente de WineHQ, téléchargée depuis le site "PlayOnLinux".

# First Obtain "kernel" name
KERNEL=$(uname -s)

if      [ $KERNEL = "Darwin" ]; then
        KERNEL=mac
elif        [ $Nucleo = "Linux" ]; then
        KERNEL=linux
elif        [ $Nucleo = "FreeBSD" ]; then
        KERNEL=linux
else
        echo "Unsupported OS"
fi

# Second get the right Arquitecture
ARCH=$(uname -m)

if         [ $ARCH = "i386" ]; then
            PATH="$PWD/wine/$KERNEL/x86/bin:$PATH"
            export WINESERVER="$PWD/wine/$KERNEL/x86/bin/wineserver"
            export WINELOADER="$PWD/wine/$KERNEL/x86/bin/wine"
            export WINEPREFIX="$PWD/wine/data"
            export WINEDEBUG=-all:$WINEDEBUG
            ARCH="32 Bits"
    elif    [ $ARCH = "i486" ]; then
            PATH="$PWD/wine/$KERNEL/x86/bin:$PATH"
            export WINESERVER="$PWD/wine/$KERNEL/x86/bin/wineserver"
            export WINELOADER="$PWD/wine/$KERNEL/x86/bin/wine"
            export WINEPREFIX="$PWD/wine/data"
            export WINEDEBUG=-all:$WINEDEBUG
            ARCH="32 Bits"
    elif    [ $ARCH = "i586" ]; then
            PATH="$PWD/wine/$KERNEL/x86/bin:$PATH"
            export WINESERVER="$PWD/wine/$KERNEL/x86/bin/wineserver"
            export WINELOADER="$PWD/wine/$Nucleo/x86/bin/wine"
            export WINEPREFIX="$PWD/wine/data"
            export WINEDEBUG=-all:$WINEDEBUG
            ARCH="32 Bits"
    elif    [ $ARCH = "i686" ]; then
            PATH="$PWD/wine/$KERNEL/x86/bin:$PATH"
            export WINESERVER="$PWD/wine/$KERNEL/x86/bin/wineserver"
            export WINELOADER="$PWD/wine/$KERNEL/x86/bin/wine"
            export WINEPREFIX="$PWD/wine/data"
            export WINEDEBUG=-all:$WINEDEBUG
            ARCH="32 Bits"
         elif [ $ARCH = "x86_64" ]; then
            export WINESERVER="$PWD/wine/$KERNEL/x86_64/bin/wineserver"
            export WINELOADER="$PWD/wine/$KERNEL/x86_64/bin/wine"
            export WINEPREFIX="$PWD/wine/data"
            export WINEDEBUG=-all:$WINEDEBUG
            ARCH="64 Bits"
    else
        echo "Unsoportted Architecture"
fi

============================================ ===================================== <= p>

Maintenant, je l'utilise dans mes scripts bash, car cela fonctionne mieux dans n'importe quelle distribution.

# Get the Kernel Name
Kernel=$(uname -s)
case "$Kernel" in
    Linux)  Kernel="linux"              ;;
    Darwin) Kernel="mac"                ;;
    FreeBSD)    Kernel="freebsd"            ;;
* ) echo "Your Operating System -> ITS NOT SUPPORTED"   ;;
esac

echo
echo "Operating System Kernel : $Kernel"
echo
# Get the machine Architecture
Architecture=$(uname -m)
case "$Architecture" in
    x86)    Architecture="x86"                  ;;
    ia64)   Architecture="ia64"                 ;;
    i?86)   Architecture="x86"                  ;;
    amd64)  Architecture="amd64"                    ;;
    x86_64) Architecture="x86_64"                   ;;
    sparc64)    Architecture="sparc64"                  ;;
* ) echo    "Your Architecture '$Architecture' -> ITS NOT SUPPORTED."   ;;
esac

echo
echo "Operating System Architecture : $Architecture"
echo
slot8(msd):/opt # uname -a
Linux slot8a 2.6.21_mvlcge500-electra #1 SMP PREEMPT Wed Jun 18 16:29:33 \
EDT 2008 ppc64 GNU/Linux


N'oubliez pas qu'il existe d'autres architectures de processeur que celles d'Intel / AMD ...

Vous pouvez faire quelque chose comme ceci:

if $(uname -a | grep 'x86_64'); then
  echo "I'm 64-bit"
else
  echo "I'm 32-bit"
fi

Oui, uname -a devrait suffire. voir: http://www.stata.com/support/faqs/win/ 64bit.html .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top