Ubuntu Forums Archive Viewer

bash scripting ..wild card in if statement

Archived thread 1000773 from Programming Talk. Markdown source: Programming_Talk/thread_1000773_bash_scripting_..wild_card_in_if_statement.md

Original URL About this archive
#1

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

#2

how does your file look like? also describe how you want the output.

#3

ghostdog74 said: 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

#4

# awk -F"." '$NF==254{print $0 " is a router"}' file
10.202.101.254 is a router

#5

*.*.*.254 will match any file of this form ending on 254.

#6

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
#7

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

#8

slavik said: 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)
#9

sorry about the late reply but thanks for the help