Skip to content

Instantly share code, notes, and snippets.

@thikade
Created December 11, 2017 21:14
Show Gist options
  • Save thikade/a86b860070f740bea19a0de74d44a255 to your computer and use it in GitHub Desktop.
Save thikade/a86b860070f740bea19a0de74d44a255 to your computer and use it in GitHub Desktop.
bash regex
# INSTANCE_RAM examples
# INSTANCE_RAM=8G or INSTANCE_RAM=12m
regex='^([[:digit:]]+)([GgMm])$'
if [[ "${INSTANCE_RAM}" =~ $regex ]]; then
num=${BASH_REMATCH[1]}
unit=${BASH_REMATCH[2]}
if [[ $unit =~ [Gg] ]]; then
((num = num * 1024)) # enables math to work out for odd gigs
fi
if [[ $num -lt 512 ]]; then
echo "INSTANCE_RAM set to ${INSTANCE_RAM} but must be at least 512M"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment