4

I am trying to learn some shell scripting and I though it would be a nice way to combine it with my ccna study. So I am writing a simple script that scans some network devices based upon a ip address and a subnetmask provided by the user. To determine the network a host belongs to I use the following lines of code, but it keeps getting me errors.

read -p "Geef een IP-adres op "  i
read -p "Geef een subnetmask op "  s

IFS=. read -r i1 i2 i3 i4 <<< $i
IFS=. read -r m1 m2 m3 m4 <<< $s

ip=`printf "%d.%d.%d.%d\n" $i1 $i2 $i3 $i4`
mask=`printf "%d.%d.%d.%d\n" $m1 $m2 $m3 $m4`

#bepaal netwerk id
id=`printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"`

The error is produce is as follows:

192 168 178 15 &  0xff 0xff 0xff 0x00: syntax error in expression (error token is "168 178 15 &  0xff 0xff 0xff 0x00")

Anyone here that can tell me why?

4

1 回答 1

4

问题在于引用提供给read调用的变量。如果您改为这样做(添加引号):

IFS=. read -r i1 i2 i3 i4 <<< "$i"
IFS=. read -r m1 m2 m3 m4 <<< "$s"

一切都恢复正常。仍然不确定为什么。

于 2013-09-28T18:49:33.697 回答