Skip to content

Instantly share code, notes, and snippets.

@jwalton
Last active December 20, 2015 13:38
Show Gist options
  • Save jwalton/6139835 to your computer and use it in GitHub Desktop.
Save jwalton/6139835 to your computer and use it in GitHub Desktop.
Generate a platform-specific tarball name
#!/bin/bash
# Print usage instructions.
printUsage() {
cat <<END
usage: `basename $0` [-hv] [-s version] [-p prefix]
where:
prefix - The filename prefix to use in generating the tarball name.
version - The version number to use in generating the tarball name.
Result will be of the format: prefix-version-os-osver-arch.tgz
Optional Arguments:
-h - Display this help message.
END
}
# Parse command line options.
while getopts hvs:p: OPT; do
case "$OPT" in
h)
printUsage
exit 0
;;
v)
echo "`basename $0` version 0.1"
exit 0
;;
s)
PRODUCT_VERSION=$OPTARG
;;
p)
PREFIX=$OPTARG
;;
\?)
# getopts issues an error message
printUsage >&2
exit 1
;;
esac
done
# Remove the switches we parsed above.
shift `expr $OPTIND - 1`
# We want at least one non-option argument.
# Remove this block if you don't need it.
if [ $# -gt 0 ]; then
printUsage >&2
exit 1
fi
if [ -z "$PRODUCT_VERSION" -o -z "$PREFIX" ]; then
printUsage >&2
exit 1
fi
# Work out OS details
if [ "$(uname -s)" == "Darwin" ]; then
OS=darwin
OS_VER=`uname -r`
else
if [ -f /etc/os-release ]; then
source /etc/os-release
OS=$ID
OS_VER=$VERSION_ID
fi
if [ -z "$OS" -o -z "$OS_VER" ] && [ -f /etc/lsb-release ]; then
source /etc/lsb-release
OS=$(echo ${DISTRIB_ID} | tr '[A-Z]' '[a-z]')
OS_VER=$DISTRIB_RELEASE
fi
if [ -z "$OS" -o -z "$OS_VER" ] && [ ! -z "$(which lsb_release)" ]; then
OS=$(lsb_release -s -i | tr '[A-Z]' '[a-z]')
OS_VER=`lsb_release -s -r`
fi
if [ -z "$OS" -o -z "$OS_VER" ]; then
# Fallback to uname
OS=$(uname -s | tr '[A-Z]' '[a-z]')
OS_VER=`uname -r`
fi
fi
# Processor architecture
case $(uname -m) in
x86_64)
ARCH=x86_64 # or AMD64 or Intel64 or whatever
;;
i*86)
ARCH=i386 # or IA32 or Intel32 or whatever
;;
*)
# leave ARCH as-is
;;
esac
echo ${PREFIX}-${PRODUCT_VERSION}-${OS}-${OS_VER}-${ARCH}.tgz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment