我需要编写一个脚本来输入2个IP地址,然后将它们更改为二进制。
我试过这个脚本,但它只适用于数字。我不能这样写IP:192.168.1.1
,但当我把它写成100
时,它工作正常。
#!/bin/bash
echo "Ebter the first ip"
read ip1
echo "Enter the second ip"
read ip2
a=`echo "obase=2;$ip1" | bc`
b=`echo "obase=2;$ip2" | bc`
echo $a
echo $b
我的剧本有什么问题?
答案 0 :(得分:0)
你能做什么:
#!/bin/bash
function convip()
{
CONV=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
ip=""
for byte in `echo ${1} | tr "." " "`; do
ip="${ip}.${CONV[${byte}]}"
done
echo ${ip:1}
}
echo "Enter the first ip"
read ip1
echo "Enter the second ip"
read ip2
a=`convip "${ip1}"`
b=`convip "${ip2}"`
echo "${a}"
echo "${b}"
<强>结果:强>
Enter the first ip
1.1.1.1
Enter the second ip
2.2.2.2
00000001.00000001.00000001.00000001
00000010.00000010.00000010.00000010
修改更新以保持点数