是否有可能在bash脚本中检测32位与64位?

我正在写一个bash脚本来处理一些自动化方式的安装…我有可能得到一个这样的程序在32位或64位二进制…是否有可能从bash检测机器的架构,所以我可以select正确的二进制?

这将用于Ubuntu机器。

是否

 uname -a 

给你任何你可以使用的东西? 我没有一个64位的机器来testing。


迈克·斯通(Mike Stone)的注意事项:虽然具体,

 uname -m 

将64位“x86_64”,其他32位types(在我的32位虚拟机,它是“i686”)。

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

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

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

要小心,在chroot 32位env中,uname仍然像64位主机系统一样回答。

getconf LONG_BIT工作正常。

file /bin/cp或任何已知的可执行文件或库应该做的伎俩,如果你没有getconf(但你可以stored procedures,你不能使用,也许没有在这个地方)。

 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 

请记住,还有其他CPU架构比Intel / AMD …

你可以使用下面的脚本(例如,我从“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 

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

我正在编写一个脚本来检测“架构”,这是我简单的代码(我正在使用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 

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

现在我在我的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 

你可以做这样的事情:

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

是的, uname -a应该做的伎俩。 请参阅: http : //www.stata.com/support/faqs/win/64bit.html 。