aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2022-01-15 10:53:33 +0800
committerAaron LI <aly@aaronly.me>2023-06-13 10:01:03 +0800
commit8e94814d0708d312293e94e6a35f4b5ad555c263 (patch)
treef546aa458edeaae4ef23386411c090a422a45195
parent384b1ecc194d31e2e35d361a7715d32047c32537 (diff)
downloaddotfiles-8e94814d0708d312293e94e6a35f4b5ad555c263.tar.bz2
zsh: Add ip2dec() and dec2ip() functions
-rw-r--r--_zshrc10
1 files changed, 10 insertions, 0 deletions
diff --git a/_zshrc b/_zshrc
index 085e6c1..0a27cc7 100644
--- a/_zshrc
+++ b/_zshrc
@@ -230,6 +230,16 @@ hex2ip() {
printf '%d.%d.%d.%d' $(echo "${1#0[xX]}" | sed 's/\(..\)/0x\1 /g')
}
+## Convert IP between dotted-decimal and decimal notation
+# Credit: https://stackoverflow.com/a/24136051
+ip2dec() {
+ echo "$1" | awk 'BEGIN { RS = "." } { d = d * 256 + $1 } END { print d }'
+}
+# Credit: https://stackoverflow.com/a/31281331
+dec2ip() {
+ printf '%d.%d.%d.%d' $(echo "obase=256; $1" | bc)
+}
+
#
# Terminal settings
#