Reputation: 521
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?
Upvotes: 4
Views: 600
Reputation: 34145
The problem is with quoting the variables fed to the read
call. If you do this instead (add the quotes):
IFS=. read -r i1 i2 i3 i4 <<< "$i"
IFS=. read -r m1 m2 m3 m4 <<< "$s"
Everything works again. Still not sure why.
Upvotes: 4