Skip to content

Instantly share code, notes, and snippets.

@nijotz
Created January 22, 2016 00:50
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 nijotz/16ea5ae6bebf5573413c to your computer and use it in GitHub Desktop.
Save nijotz/16ea5ae6bebf5573413c to your computer and use it in GitHub Desktop.
IP address from store ID
declare -a ip_address
for i in {3..1}; do
# bash fails when using ${var: -n} to trim strings if the number is greater
# than the number of characters. So this uses ${#var} to test the length
# huehuehue
last_3=${store_id: ${#store_id}<3?0:-3}
if [ $last_3 -gt 255 ]; then
last_3=${last_3: -2}
store_id=${store_id::-2}
else
# head -c-n doesn't fail as badly as bash
store_id=$(echo $store_id | head -c-3)
fi
ip_address[$i]=$last_3;
done
ip_address="10.$(( 10#${ip_address[1]} )).$(( 10#${ip_address[2]} )).$(( 10#${ip_address[3]} ))"
echo $ip_address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment