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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7815989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:58:36  来源:igfitidea点击:

need to break IP address stored in bash variable into octets

regexbashvariablessedip-address

提问by brent saner

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

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

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?

所以我可以逃避 sed 中的时期。有一个更好的方法吗?awk 是我要找的东西吗?

回答by Rob Davis

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

你可以使用 bash。这是一个单行,假设您的地址在$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 readcommand will honor it.

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

回答by chown

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:

如果您想在不使用数组或带有换行符的单个变量的情况下将每个八位字节分配给它自己的变量(这样您就可以轻松地通过 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

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

请参阅本文中的/wiki/Bash:_Append_to_array_using_while-loop
和更多内容

回答by tripleee

You can split strings using the setbuilt-in, with IFSas separator (normally space and tab).

您可以使用set内置的IFS分隔符(通常是空格和制表符)拆分字符串。

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

splitip 12.34.56.78
# Now  contains 12,  contains 34, etc

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

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

回答by Kyle

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  }')
ipoct2=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print  }')
ipoct3=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print  }')
ipoct4=$(echo ${ICINGAIPADDRESS} | tr "." " " | awk '{ print  }')

回答by Miguel Ortiz

The easier way is using AWK:

更简单的方法是使用 AWK:

echo 192.168.0.12 | awk -F. '{print    }'

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

-F 是字段分隔符,在这种情况下我们使用点“。” 作为分隔符并单独打印每一列。

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