Bash if uname linux

Is it possible to detect 32 bit vs 64 bit in a bash script? [duplicate]

I am writing a bash script to deal with some installations in an automated way. I have the possibility of getting one such program in 32 or 64 bit binary. is it possible to detect the machine architecture from bash so I can select the correct binary? This will be for Ubuntu machines.

Detect what? CPU? OS? Userland availability of a cpu architecture? /proc/cpuinfo returning 64-bit when the OS is 32-bit is not very helpful in most cases.

8 Answers 8

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

On 32 bits machines, this method is not recommended as the answer will be i386, i686, etc. I would advice to favor the getconf LONG_BIT method.

@Speredenn the above doesn’t check for the 32-bit name, e.g. i386, i686, etc, but for the 64-bit name. Your contention is moot unless there are also synonyms for x86_64 that it could return (e.g. amd64, but can it?)

FYI that does not always seems to match i.e. I see a system where LONG_BIT reports 32, but uname -m reports aarch64

getconf LONG_BIT seems to do the trick as well, which makes it even easier to check this since this returns simply the integer instead of some complicated expression.

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

I think, this should be used carefully on other architectures. I think this will return 64 on aarch64 too.

Читайте также:  Linux полное удаление пакета

give you anything you can use? I don’t have a 64-bit machine to test on.

Note from Mike Stone: This works, though specifically

Will give «x86_64» for 64 bit, and something else for other 32 bit types (in my 32 bit VM, it’s «i686»).

Be careful, in a chroot ed 32-bit env, the uname is still answering like the 64-bit host system.

getconf LONG_BIT works fine.

file /bin/cp or any well-known executable or library should do the trick if you don’t have getconf (but you can store programs you can’t use, and maybe there are not at this place).

You can use , the follow script (i extract this from officially script of «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’m making a script to detect the «Architecture», this is my simple code (I am using it with wine , for my Windows Games , under Linux , by each game , i use diferrent version of WineHQ, downloaded from «PlayOnLinux» site.

# 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 works better in any distro .

# 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 

Источник

Читайте также:  Install windows 10 on virtualbox linux

How to know if the running platform is Ubuntu or CentOS with help of a Bash script?

How do I get the output from the terminal and compare to see if it is UBUNTU or CENTOS and perform the following commands?

Ubuntu 14.04

14 Answers 14

Unfortunately, there is no surefire, simple way of getting the distribution name. Most major distros are moving towards a system where they use /etc/os-release to store this information. Most modern distributions also include the lsb_release tools but these are not always installed by default. So, here are some approaches you can use:

# Determine OS platform UNAME=$(uname | tr "[:upper:]" "[:lower:]") # If Linux, try to determine specific distribution if [ "$UNAME" == "linux" ]; then # If available, use LSB to identify distribution if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//) # Otherwise, use release info file else export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1) fi fi # For everything else (or if above failed), just use generic identifier [ "$DISTRO" == "" ] && export DISTRO=$UNAME unset UNAME 
$ gcc --version gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) Copyright (C) 2006 Free Software Foundation, Inc. 
$ gcc --version gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3) Copyright (C) 2010 Free Software Foundation, Inc. 
$ gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. 
$ gcc --version gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 Copyright (C) 2013 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

This has basically been directly copied from @slm’s great answer to my question here.

Читайте также:  Command line login linux

Источник

Оцените статью
Adblock
detector