需要将 bash 变量中存储的 IP 地址分解为八位字节

发布于 2024-12-10 13:17:37 字数 199 浏览 1 评论 0原文

我有一个包含 IP 地址的 bash 变量(没有 CIDR 或任何东西,只有四个八位字节)。

我需要将该变量分成四个单独的八位字节,如下所示:

$ip = 1.2.3.4; 
$ip1 = 1
$ip2 = 2
# etc

这样我就可以在 sed 中转义句点。有更好的方法吗? awk 是我要找的吗?

i've got a bash variable that contains an IP address (no CIDR or anything, just the four octets).

i need to break that variable into four separate octets like this:

$ip = 1.2.3.4; 
$ip1 = 1
$ip2 = 2
# etc

so i can escape the period in sed. is there a better way to do this? is awk what i'm looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人间☆小暴躁 2024-12-17 13:17:37

你可以使用 bash。这是一个单行代码,假设您的地址位于 $ip 中:

IFS=. read ip1 ip2 ip3 ip4 <<< "$ip"

它的工作原理是仅为一个命令设置“内部字段分隔符”,将其从通常的空格分隔符更改为句点。 read 命令将遵循它。

You could use bash. Here's a one-liner that assumes your address is in $ip:

IFS=. read ip1 ip2 ip3 ip4 <<< "$ip"

It works by setting the "internal field separator" for one command only, changing it from the usual white space delimiter to a period. The read command will honor it.

风筝有风,海豚有海 2024-12-17 13:17:37

如果您想将每个八位字节分配给它自己的变量,而不使用数组或带有换行符的单个变量(这样您可以轻松地通过 for 循环运行它),您可以使用 #% 修饰符 ${x} 如下所示:

[ 20:08 jon@MacBookPro ~ ]$ x=192.160.1.1 && echo $x
192.160.1.1
[ 20:08 jon@MacBookPro ~ ]$ oc1=${x%%.*} && echo $o1
192
[ 20:08 jon@MacBookPro ~ ]$ x=${x#*.*} && echo $x
160.1.1
[ 20:08 jon@MacBookPro ~ ]$ oc2={x%%.*} && echo $o2
160
[ 20:08 jon@MacBookPro ~ ]$ x=${x#*.*} && echo $x
1.1
[ 20:08 jon@MacBookPro ~ ]$ oc3=${x%%.*} && echo $o3
1
[ 20:08 jon@MacBookPro ~ ]$ x=${x#*.*} && echo $x
1
[ 20:08 jon@MacBookPro ~ ]$ oc4=${x%%.*} && echo $oc4
1

[ 20:09 jon@MacBookPro ~ ]$ echo "$oc1\.$oc2\.$oc3\.$oc4"
192\.160\.1\.1

请参阅此 /wiki/Bash:_Append_to_array_using_while-loop
以及本文中的更多内容。

If you want to assign each octet to its own variable without using an array or a single variable with newline breaks (so you can easily run it through a for loop), you could use # and % modifiers to ${x} like so:

[ 20:08 jon@MacBookPro ~ ]$ x=192.160.1.1 && echo $x
192.160.1.1
[ 20:08 jon@MacBookPro ~ ]$ oc1=${x%%.*} && echo $o1
192
[ 20:08 jon@MacBookPro ~ ]$ x=${x#*.*} && echo $x
160.1.1
[ 20:08 jon@MacBookPro ~ ]$ oc2={x%%.*} && echo $o2
160
[ 20:08 jon@MacBookPro ~ ]$ x=${x#*.*} && echo $x
1.1
[ 20:08 jon@MacBookPro ~ ]$ oc3=${x%%.*} && echo $o3
1
[ 20:08 jon@MacBookPro ~ ]$ x=${x#*.*} && echo $x
1
[ 20:08 jon@MacBookPro ~ ]$ oc4=${x%%.*} && echo $oc4
1

[ 20:09 jon@MacBookPro ~ ]$ echo "$oc1\.$oc2\.$oc3\.$oc4"
192\.160\.1\.1

See this /wiki/Bash:_Append_to_array_using_while-loop
and more in this article.

み青杉依旧 2024-12-17 13:17:37

您可以使用内置的 set 来分割字符串,并使用 IFS 作为分隔符(通常是空格和制表符)。

splitip () {
    local IFS
    IFS=.
    set -- $*
    echo "$@"
}

splitip 12.34.56.78
# Now $1 contains 12, $2 contains 34, etc

如果您只需要反斜杠转义点,请使用字符串替换 - bash 有 ${ip//./\\.}

You can split strings using the set built-in, with IFS as separator (normally space and tab).

splitip () {
    local IFS
    IFS=.
    set -- $*
    echo "$@"
}

splitip 12.34.56.78
# Now $1 contains 12, $2 contains 34, etc

If you just need to backslash-escape the dots, use string substitution - bash has ${ip//./\\.}

随遇而安 2024-12-17 13:17:37

当我想做同样的事情时,我在另一个网站上找到了这段代码。非常适合我的应用程序。

   read ICINGAIPADDRESS
# The following lines will break the ICINGAIPADDRESS variable into the four octets 
# and assign each octet to a variable.

ipoct1=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $1 }')
ipoct2=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $2 }')
ipoct3=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $3 }')
ipoct4=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $4 }')

This code is something that I found on another site when I was looking to do the same thing. Works perfectly for my application.

   read ICINGAIPADDRESS
# The following lines will break the ICINGAIPADDRESS variable into the four octets 
# and assign each octet to a variable.

ipoct1=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $1 }')
ipoct2=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $2 }')
ipoct3=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $3 }')
ipoct4=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print $4 }')
千寻… 2024-12-17 13:17:37

更简单的方法是使用 AWK:

echo 192.168.0.12 | awk -F. '{print $1 $2 $3 $4}'

-F 是字段分隔符,在本例中我们使用点“.”作为分隔符并单独打印每一列。

mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $1 $2 $3 $4}'
76220156100
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $1}'
76
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $2}'
220
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $3}'
156
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $4}'
100

The easier way is using AWK:

echo 192.168.0.12 | awk -F. '{print $1 $2 $3 $4}'

-F is a field separator, in this case we use the dot "." as separator and print each column individually.

mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $1 $2 $3 $4}'
76220156100
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $1}'
76
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $2}'
220
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $3}'
156
mortiz@florida:~/Documents/projects$ echo 76.220.156.100 | awk -F. '{print $4}'
100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文