我必须在shell和PowerShell中尽可能缩短ipv6地址。在shell和PowerShell中缩短IPV6地址
像:
Input: 2001:0db8:03cd:0000:0000:ef45:0006:
Output: 2001:db8:3cd:::ef45:6:123
和脚本应该给其自身的描述,如果使用-help参数,但我不知道该怎么做在PowerShell中。
这是我在PowerShell代码,它正确地缩短地址:
param([parameter(Mandatory=$true)]$file)
if (test-path $file){
foreach ($ip in Get-Content $file){
$ip=$ip.Replace("0000","")
Write-Host $ip
}
}
我不知道该怎么做壳的缩短,我想这样的,但没有奏效:
#!/bin/sh
if [ $1 = "-help" ]
then
echo description
else file = $1
fi
for ip in `cat ipv6.txt`
do
$ip=$line
$replace=""
$echo ${var//0000/$replace}
done
这是地址txt文件: http://uptobox.com/6woujdvdfkmh
我怀疑三重结肠不是一个正确的缩短。 '03cd:0000:0000:ef'会变成'3cd :: ef',不是吗?而且你的代码会错误地缩短地址的两个独立的四个零('2001:0db8:0000:0000:004f:0000:0000:0123')集合,因为双冒号只能在缩短的地址中出现一次。 – TessellatingHeckler 2014-12-06 23:43:09