Execute Shell Script verifying 32bit or 64bit system architecture

1

How can I make a script that checks the system architecture and so it would execute the commands for the appropriate architecture, for example:

if 32bit; then

comandos para 32 bits

else

comandos para 64 bits
    
asked by anonymous 08.04.2018 / 22:54

3 answers

6

I found an answer that detects different types of architectures in the SOen , note that every% of_% checks: / p>

#!/bin/bash

HOST_ARCH=$(uname -m)

case "$HOST_ARCH" in
    x86)     HOST_ARCH="x86"          ;;
    i?86)    HOST_ARCH="x86"          ;;
    ia64)    HOST_ARCH="ia64"         ;;
    amd64)   HOST_ARCH="amd64"        ;;
    x86_64)  HOST_ARCH="x86_64"       ;;
    sparc64) HOST_ARCH="sparc64"      ;;
    * )      HOST_ARCH="desconhecido" ;;
esac

echo
echo "A arquitetura do seu sistema é: $HOST_ARCH"
echo

IDEONE example: link

Or you can even simplify it to something like:

#!/bin/bash

HOST_ARCH=$(uname -m)

case "$HOST_ARCH" in
    x86)     HOST_ARCH="32"           ;;
    i?86)    HOST_ARCH="32"           ;;
    ia64)    HOST_ARCH="64"           ;;
    amd64)   HOST_ARCH="64"           ;;
    x86_64)  HOST_ARCH="64"           ;;
    sparc64) HOST_ARCH="64"           ;;
    * )      HOST_ARCH="desconhecido" ;;
esac

if [ $HOST_ARCH = "64" ]; then
   # comandos para 64
elif [ $HOST_ARCH = "32" ]; then
   # comandos para 32
else
   echo "Sistema não suportado";
fi

Example on IDEONE

The response from Miguel looks good, but case does not guarantee that it is 64-bit, I think that it is best to save to a variable and use else , for example:

HOST_ARCH=$(file /bin/bash | cut -d' ' -f3);

if [ $HOST_ARCH = "32-bit" ]; then

   # comandos para 32 bits

elif [ $HOST_ARCH = "64-bit" ]; then

   # comandos para 64 bits

else
   echo "Sistema não suportado";
fi
    
10.04.2018 / 00:00
1
#!/bin/bash
if [ $(uname -m) = "x86_64" ]; then
    echo "x64"
else
    echo "x86"
fi
    
09.04.2018 / 01:06
1

The LONG_BIT system variable check follows:

if [ $(getconf LONG_BIT) = 64 ]; then
  echo "64bits";
elif [ $(getconf LONG_BIT) = 32  ]; then
  echo "32bits";
else 
  echo "another";
fi

Another option would be to check uname -m which returns:

  • x64, ia64, amd64, and x86_64 would be 64 bits;

  • i686, i586, i486 and i386, would be 32 bits;

09.04.2018 / 23:03