Is it possible to detect a 32-bit and 64-bit number in a bash script?

I am writing a bash script to work with some installations in an automatic way ... I have the opportunity to get one such program in binary 32 or 64 bit format ... is it possible to detect an architecture machine from bash, so I can choose the right binary?

This will be for Ubuntu machines.

+59
bash
Sep 19 '08 at 23:29
source share
8 answers

whether

uname -a 

give you everything you can use? I do not have a 64-bit machine for testing.




Note from Mike Stone: It works, although specifically

 uname -m 

It gives "x86_64" for 64 bits, and something else for the other 32-bit types (in my 32-bit VM, it is "i686").

+39
Sep 19 '08 at 23:33
source share
 MACHINE_TYPE=`uname -m` if [ ${MACHINE_TYPE} == 'x86_64' ]; then # 64-bit stuff here else # 32-bit stuff here fi 
+65
Sep 19 '08 at 23:37
source share

getconf LONG_BIT seems to do the trick too, which makes it even easier to check, since it returns just an integer instead of some complex expression.

 if [ `getconf LONG_BIT` = "64" ] then echo "I'm 64-bit" else echo "I'm 32-bit" fi 
+50
Sep 05 2018-11-12T00:
source share

Be careful, in 32-bit env chroot ed, uname still responds as a 64-bit host system.

getconf LONG_BIT works fine.

file /bin/cp or any known executable or library should do the trick if you don't have getconf (but you can store programs that you can't use, and maybe don't have them).

+10
Dec 12 '11 at 14:20
source share
 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 


Remember that there are other processor architectures than Intel / AMD ...

+7
Sep 19 '08 at 23:44
source share

You can use the following script (I extract this from the official script from "ioquake3"): for example

 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 

==================================================== ==================================

I create a script to detect "Architecture", this is my simple code (I use it with wine for my Windows games under Linux, every game, I use a different version of WineHQ downloaded from the PlayOnLinux website.

 # 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 

==================================================== ==================================

Now I use this in my bash scripts because it works better on any 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 
+6
Oct 18 '11 at 9:01 a.m.
source share

You can do something like this:

 if $(uname -a | grep 'x86_64'); then echo "I'm 64-bit" else echo "I'm 32-bit" fi 
+4
Sep 19 '08 at 23:36
source share

Yes, uname -a should do the trick. see http://www.stata.com/support/faqs/win/64bit.html .

-one
Sep 19 '08 at 23:37
source share



All Articles