Created
January 22, 2016 00:50
-
-
Save nijotz/16ea5ae6bebf5573413c to your computer and use it in GitHub Desktop.
IP address from store ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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