1

Possible Duplicate:
How to determine whether a given Linux is 32 bit or 64 bit?

Does anybody know how to create a shell script sh file that can shell one program if its a 64-bit system or shell another if its a 32-bit system? Thank so much.

Community
  • 1
  • 1
romeo 8812
  • 75
  • 2
  • 7
  • 1
    What exactly are you trying to do? We might be able to help you with a better overall solution if we knew what the actual problem is... – thkala Sep 03 '12 at 16:05
  • Does uname -i work for both platforms? – squiguy Sep 03 '12 at 16:10
  • 1
    http://unix.stackexchange.com/questions/12453/how-to-determine-linux-kernel-architecture – perilbrain Sep 03 '12 at 16:10
  • 2
    What aspect of 32-bittiness vs 64-bittiness do you need to know? There are 64-bit systems that can run 32-bit or 64-bit programs. You can have a CPU capable of running 32-bit or 64-bit and the O/S booted might be a 32-bit kernel or a 64-bit kernel. – Jonathan Leffler Sep 03 '12 at 16:14

4 Answers4

5
if $(uname -m | grep '64'); then
  echo "ARCH: 64-bit"
else
  echo "ARCH: 32-bit"
fi
twid
  • 6,368
  • 4
  • 32
  • 50
  • 2
    This doesn't work for me -- all I get is a shell prompt: $. Simple string comparison, though, does the trick: if [ "$(uname -m | grep '64')" != "" ]; then – Gilead Jan 16 '14 at 01:39
2

Try uname -m: x86_64 is a 64-bit kernel, i686 is 32-bit kernel. Based on this, you can call either one program or the other.

j0nes
  • 8,041
  • 3
  • 37
  • 40
1

(In response to thkala's comment.)

if echo __SIZEOF_POINTER__ | cpp -E - - | grep '^8$' >/dev/null; then
    do_stuff
fi

Unlikely to work everywhere, but it works if cpp is from GCC. Has the advantage of detecting any 64-bit architecture, not just x64 (POWER, SPARC, IA64, whatever).

Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
  • I was thinking about checking the value of `sizeof(void *)`, but that would require a compiler and would be about as reliable as running `file` on some random system binary... – thkala Sep 03 '12 at 16:16
0

If you want to know whether the processor is 64-bit, rather than the kernel, You can search for the long mode (-lm) flag on your system. It will be present on 64-bit, and not on 32-bit:

cat /proc/cpuinfo | grep lm

jam
  • 3,640
  • 5
  • 34
  • 50