Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Last active June 30, 2022 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongkongkiwi/df259fd2f139a771148169390d48ffd5 to your computer and use it in GitHub Desktop.
Save hongkongkiwi/df259fd2f139a771148169390d48ffd5 to your computer and use it in GitHub Desktop.
Script for getting system arch in various formats, you simply pass outputs depending on what kind of format you want. e.g. get-sys-arch --x64 --x32 --armhf --arm64
#!/bin/bash -ue
HELP="$0 [--i686|--i386|--x86_32|--x32] [--amd64|--x86_64|--x64] [--armv7l|--armv7|--arm|--armhf] [--arm64|--aarch64]"
I386=''; I686=''; X86_32=''; X32=''; BITS_32='';
ONLY_686=''; ONLY_386=''; ONLY_32=''; ONLY_64='';
AMD64=''; X86_64=''; X64=''; BIT_64='';
AARCH64=''; ARM64='';
ARM_V6=''; ARMV7L=''; ARMV7=''; ARM=''; ARM_HF='';
while [ $# -gt 0 ]; do
case "${1-}" in
'--32bit'|'-32bit') BIT_64='yes'; ONLY_32=''; I686=''; X32=''; ONLY_386=''; ONLY_686=''; I386=''; X86_32='';;
'--32'|'-32') ONLY_32='yes'; BIT_32=''; I686=''; X32=''; ONLY_386=''; ONLY_686=''; I386=''; X86_32='';;
'--i686'|'-i686') I686='yes'; X32=''; ONLY_386=''; ONLY_686=''; I386=''; X86_32=''; BIT_32='';;
'--i386'|'-i386') I386='yes'; X32=''; ONLY_386=''; ONLY_686=''; I686=''; X86_32=''; BIT_32='';;
'--386'|'-386') ONLY_386='yes'; X32=''; ONLY_686=''; I386=''; I686=''; X86_32=''; BIT_32='';;
'--686'|'-686') ONLY_686='yes'; X32=''; ONLY_386=''; I386=''; I686=''; X86_32=''; BIT_32='';;
'--x32'|'-x32') X32='yes'; X86_32=''; I386=''; I686=''; ONLY_386=''; ONLY_686=''; BIT_32='';;
'--x86_32'|'-x86_32') X86_32='yes'; I386=''; I686=''; ONLY_386=''; ONLY_686=''; BIT_32='';;
'--64bit'|'-64bit') BIT_64='yes'; ONLY_64=''; AMD64=''; X64=''; X86_64='';;
'--64'|'-64') ONLY_64='yes'; BIT_64=''; AMD64=''; X64=''; X86_64='';;
'--amd64'|'-amd64') AMD64='yes'; BIT_64=''; ONLY_64=''; X64=''; X86_64='';;
'--x64'|'-x64') X64='yes'; BIT_64=''; ONLY_64=''; X86_64=''; AMD64='';;
'--x86_64'|'-x86_64') X86_64='yes'; BIT_64=''; ONLY_64=''; X64=''; AMD64='';;
'--aarch64'|'-aarch64') AARCH64='yes'; ARM64='';;
'--arm64'|'-arm64') ARM64='yes'; AARCH64='';;
'--armv6'|'--arm-v6'|'-armv6'|'-arm-v6') ARM_V6='yes'; ARM_V7L=''; ARMV_V7=''; ARM='';;
'--arm-v7l'|'--armv7l'|'-arm-v7l'|'-armv7l') ARM_V7L='yes'; ARMV_V7=''; ARM='';;
'--arm-v7'|'--armv7'|'-arm-v7'|'-armv7') ARMV_V7='yes'; ARM_V7L=''; ARM='';;
'--arm'|'-arm') ARM='yes'; ARM_V7L=''; ARM_V7=''; ARM_HF='';;
'--armhf'|'-armhf') ARM_HF='yes'; ARM=''; ARM_V7L=''; ARM_V7='';;
'--help'|'-help'|'-h') echo >&2 "$HELP"; exit 255;;
esac
shift
done
ARCH=''; SYS_64BIT='';
[ "$(getconf LONG_BIT)" == '64' ] && SYS_64BIT='yes'
SYS_ARCH="$(lscpu | awk '/Architecture:/{print $2}')"
case "$SYS_ARCH" in
'386'|'i386'|'686'|'i686'|'x86_32'|'x32'|'32bit'|'32')
ARCH="$SYS_ARCH"
if [ -n "$I386" ]; then
ARCH="i386"
elif [ -n "$I686" ]; then
ARCH="i686"
elif [ -n "$X86_32" ]; then
ARCH="x86_32"
elif [ -n "$ONLY_386" ]; then
ARCH="386"
elif [ -n "$ONLY_686" ]; then
ARCH="686"
elif [ -n "$X32" ]; then
ARCH="x32"
elif [ -n "$BIT_32" ]; then
ARCH="32bit"
elif [ -n "$ONLY_32" ]; then
ARCH="32"
fi
;;
'amd64'|'x86_64'|'x64'|'64bit'|'64')
ARCH="$SYS_ARCH"
if [ -n "$AMD64" ]; then
ARCH="amd64"
elif [ -n "$X86_64" ]; then
ARCH="x86_64"
elif [ -n "$X64" ]; then
ARCH="x64"
elif [ -n "$BIT_64" ]; then
ARCH="64bit"
elif [ -n "$ONLY_64" ]; then
ARCH="64"
fi
;;
'arm64'|'aarch64')
ARCH="$SYS_ARCH"
if [ -n "$AARCH64" ]; then
ARCH="aarch64"
elif [ -n "$ARM64" ]; then
ARCH="arm64"
fi
;;
'arm'|'armv6'|'armv7l'|'armv7')
if [ -n "$SYS_64BIT" ]; then
ARCH="$SYS_ARCH"
if [ -n "$AARCH64" ]; then
ARCH="aarch64"
elif [ -n "$ARM64" ]; then
ARCH="arm64"
fi
else
ARCH="arm"
if [ -n "$ARM_V7L" -a "$SYS_ARCH" == "armv7l" ]; then
ARCH="armv7l"
elif [ -n "$ARM_V7L" -a "$SYS_ARCH" == "armv7" ]; then
ARCH="armv7l"
elif [ -n "$ARM_V7" -a "$SYS_ARCH=" == "armv7l" ]; then
ARCH="arm-v7"
elif [ -n "$ARM_V7" -a "$SYS_ARCH=" == "armv7" ]; then
ARCH="arm-v7"
elif [ -n "$ARM_HF" -a "$SYS_ARCH=" == "armhf" ]; then
ARCH="armhf"
elif [ -n "$ARM_V6" ]; then
ARCH="armv6"
fi
fi
;;
'mips') ARCH="mips";;
*) echo >&2 "ERROR: unknown arch '$SYS_ARCH'"; exit 100;;
esac
echo "$ARCH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment