是否可以在 bash 脚本中检测 32 位和 64 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/106387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:41:08  来源:igfitidea点击:

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

bash

提问by Mike Stone

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?

我正在编写一个 bash 脚本来以自动方式处理一些安装......我有可能在 32 位或 64 位二进制文​​件中获得一个这样的程序......是否可以从 bash 检测机器架构,以便我可以选择正确的二进制?

This will be for Ubuntu machines.

这将适用于 Ubuntu 机器。

采纳答案by shoover

Does

uname -a

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

给你什么你可以用?我没有要测试的 64 位机器。



Note from Mike Stone:This works, though specifically

迈克·斯通 (Mike Stone) 的笔记:虽然特别有效

uname -m

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

将为 64 位提供“x86_64”,为其他 32 位类型提供其他信息(在我的 32 位 VM 中,它是“i686”)。

回答by bmdhacks

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

回答by Victor Zamanian

getconf LONG_BITseems 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.

getconf LONG_BIT似乎也能做到这一点,这使得检查这一点变得更加容易,因为它只返回整数而不是一些复杂的表达式。

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

回答by lolesque

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

小心,在chrooted 32 位环境中,uname 仍然像 64 位主机系统一样回答。

getconf LONG_BITworks fine.

getconf LONG_BIT工作正常。

file /bin/cpor 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).

file /bin/cp如果您没有 getconf,或者任何知名的可执行文件或库都应该可以解决问题(但您可以存储您无法使用的程序,也许这里没有)。

回答by inukaze

You can use , the follow script (i extract this from officially script of "ioquake3") : for example

您可以使用以下脚本(我从“ioquake3”的官方脚本中提取此脚本):例如

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.

我正在制作一个脚本来检测“架构”,这是我的简单代码(我将它与 wine 一起使用,对于我的 Windows 游戏,在 Linux 下,每个游戏,我使用不同版本的 WineHQ,从“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

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

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

Now i use this in my bash scripts , because works better in any distro .

现在我在我的 bash 脚本中使用它,因为在任何发行版中都能更好地工作。

# 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

回答by Kevin Little

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, there are other CPU architectures than Intel/AMD...


请记住,除了 Intel/AMD 之外,还有其他 CPU 架构......

回答by hoyhoy

You could do something like this:

你可以这样做:

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

回答by hoyhoy

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

是的,uname -a应该可以解决问题。请参阅:http: //www.stata.com/support/faqs/win/64bit.html