How can I pass an optional argument to a bash script that will replace an existing variable in the script? For example:
#!/bin/bash
#hostfinder.sh
#Find hosts in current /24 network
subnet=$(hostname -i | cut -d. -f1,2,3)
for j in \
$(for i in $subnet.{1..255}; do host $i; done | grep -v not | cut -d" " -f5)
do ping -c1 -w 1 $j; done | grep -i from | cut -d" " -f3,4,5 | tr ':' ' ' | \
sed -e 's/from/Alive:/'
This will grab the IP of the current host, run a reverse lookup against possible neighbors, ping test any hostnames it finds, and display an output similar to this:
Alive: host1.domain (10.64.17.23)
Alive: host2.domain (10.64.17.24)
...
Call me crazy, but it's way faster than nmap and spits out a nice list.
Anyway, I'd like to optionally pass the first three octets of any class C network address to the $subnet variable as the $1 argument when executing the script. For example:
./hostfinder.sh 10.20.0
My first thought was to try something like $subnet=$1, but I assume that will not work. I'm not really interested in re-writing the script to be more elegant or anything, I am mostly just curious about what I put in the subject line.
subnet=$1
not work for you? – fbynite Mar 13 at 10:22