PDA

View Full Version : bash scripting ..wild card in if statement



b-boy
December 3rd, 2008, 03:16 PM
Hi everyone I writing a simple bash script and the problem I am having is:
I need to search a file with ip addresses and exec a command for each excluding the ip address that ends with .254 which in most cases is the router

for ip in `cat /home/tux/Desktop/file`
do
if [ $ip = *.254 ];then
echo this is a router

done
this is not what my script must do but if this works my script will work

ghostdog74
December 3rd, 2008, 03:33 PM
how does your file look like? also describe how you want the output.

b-boy
December 3rd, 2008, 04:09 PM
how does your file look like? also describe how you want the output.

the file is something like this

10.0.0.1
10.0.0.2
10.0.0.3
10.4.5.12


and it goes on


the output i want is if on of those ips end with 254 i must get a msg "this is a router

ghostdog74
December 3rd, 2008, 04:13 PM
# awk -F"." '$NF==254{print $0 " is a router"}' file
10.202.101.254 is a router

loomsen
December 3rd, 2008, 04:15 PM
*.*.*.254 will match any file of this form ending on 254.

geirha
December 4th, 2008, 12:47 AM
You can also use a case statement


while read ip; do
case "$ip" in
*.254)
echo "$ip is a router"
;;
*)
echo "$ip is not a router"
;;
esac
done < /home/tux/Desktop/file

slavik
December 4th, 2008, 01:26 AM
the below will produce all lines that do not end with 254

for i in [grep -v "254$" file]; do
ping -c 1 $i
done

ghostdog74
December 4th, 2008, 01:32 AM
the below will produce all lines that do not end with 254

for i in [grep -v "254$" file]; do
ping -c 1 $i
done

that's wrong syntax


for i in `grep -v "254$"` file`
# for i in $(grep -v "254$" file)

b-boy
December 5th, 2008, 03:47 PM
sorry about the late reply but thanks for the help