在SHell中拆分字符串

在SHell中拆分字符串,shell,debian,Shell,Debian,我有一个IP地址字符串: 172.27.16.123 我有一个类似的第二个字符串 我需要四个不同变量中的四个部分,即v1=172,v2=27,v3=16,v4=123 我想这样做的原因是,我想取第三个ip,看看它是否位于这两个ip之间 我试过了 echo $givenip | cut -d\. -f3 v11=$(回声$ip1 |切割-d.-f3) echo$v11假设您的shell是bash,您可能应该使用数组: export v1=`echo $givenip | cut -d\. -

我有一个IP地址字符串:

172.27.16.123

我有一个类似的第二个字符串

我需要四个不同变量中的四个部分,即v1=172,v2=27,v3=16,v4=123 我想这样做的原因是,我想取第三个ip,看看它是否位于这两个ip之间

我试过了

echo $givenip | cut -d\. -f3  
v11=$(回声$ip1 |切割-d.-f3)


echo$v11

假设您的shell是
bash
,您可能应该使用数组:

export v1=`echo $givenip | cut -d\. -f3`
givenip=172.27.18.191
ip1=( $(sed 's/\./ /g' <<< 172.27.16.123) )
ip2=( $(sed 's/\./ /g' <<< 172.27.19.254) )
ip3=( $(sed 's/\./ /g' <<< $givenip) )
givenip=172.27.18.191

ip1=($(sed's/\.//g'一些详细信息如果我的分隔符字符是空格,我如何使用它?如果我尝试:echo$var | cut-d \-f3或echo$var | cut-d \''-f3不起作用。第一个命令说分隔符只能是一个字符,第二个命令将我的提示更改为“>”
export v1=
echo$givenip | cut-d”“-f3`
givenip=172.27.18.191
ip1=( $(sed 's/\./ /g' <<< 172.27.16.123) )
ip2=( $(sed 's/\./ /g' <<< 172.27.19.254) )
ip3=( $(sed 's/\./ /g' <<< $givenip) )

if [ "${ip3[0]}" -ge "${ip1[0]}" -a "${ip3[0]}" -le "${ip2[0]}" ] &&
   [ "${ip3[1]}" -ge "${ip1[1]}" -a "${ip3[1]}" -le "${ip2[1]}" ] &&
   [ "${ip3[2]}" -ge "${ip1[2]}" -a "${ip3[2]}" -le "${ip2[2]}" ] &&
   [ "${ip3[3]}" -ge "${ip1[3]}" -a "${ip3[3]}" -le "${ip2[3]}" ]
 then echo $givenip is between two established IP addresses
 else echo $givenip is not between the two established IP addresses
 fi