Click to See Complete Forum and Search --> : A little awk problem.


Carl Pender
04-23-2003, 11:59 AM
I have an awk script below that reads in an IP address from a web page. It then matches the IP address with a MAC address and then allows the MAC address onto the network. Here's the script anyway:

#!/bin/bash

read ip_address

sudo arp > /usr/local/apache/logs/users.txt

sudo awk '{if ($1 ==$ip_address) print $3}' /usr/local/apache/logs/users.txt | /usr/local/apache/cgi-bin/add

The thing is that this works when I change the $ip_address on the awk line with an actual ip address. e.g.


#!/bin/bash

read ip_address

sudo arp > /usr/local/apache/logs/users.txt

sudo awk '{if ($1 =="123.123.123.123" print $3}' /usr/local/apache/logs/users.txt | /usr/local/apache/cgi-bin/add

This will check the arp table for the ip address 123.123.123.123 and then add the corresponding mac address onto the network.

I have also tried putting $ip_address in quotations like so

sudo awk '{if ($1 =="$ip_address") print $3}' /usr/local/apache/logs/users.txt | /usr/local/apache/cgi-bin/add

The script add works perfectly so there's no problem there.

Any suggestions guys?

Thanks again

Carl

bwkaz
04-23-2003, 01:04 PM
bash doesn't expand variables if they're in single quotes. Remove the single quotes (change them to double quotes) around your awk script. ;)

You will have to put $1 and $3 in single quotes, but do those on their own.

Carl Pender
04-24-2003, 02:50 PM
I'm not fully sure what you mean but I tried:

sudo awk "{if ('$1' ==$ip_address) print '$3'}" /usr/local/apache/logs/users.txt | /usr/local/apache/cgi-bin/add


but it doesn't work.

bwkaz
04-24-2003, 03:15 PM
Hmm... that should work...

Well, hang on. Maybe use \$1 instead of '$1' (and the same for $3) inside the double quotes?

Carl Pender
04-25-2003, 05:36 AM
I'm afraid that doen't work either, I'm getting this error message:



awk: cmd. line:1: {if ($1==) print $3}
awk: cmd. line:1: ^ parse error

bwkaz
04-25-2003, 09:11 AM
Does the ip_address variable actually contain anything? It doesn't look like it, and that looks to be the problem.

Notice how there's nothing after the ==?

Carl Pender
04-25-2003, 11:32 AM
I got it working. I used:


sudo awk '{if ($1 == "'"$ip_address"'") print $3}' /usr/local/apache/logs/users.txt | /usr/local/apache/cgi-bin/add


Thanks very much guys you've been extremely helpful and I really appreciate you trying to help me.

Thanks again

Carl

bwkaz
04-25-2003, 02:22 PM
Hmm, that's ... interesting. Oh well, if it works, I guess. ;)

hotleadpdx
04-26-2003, 07:11 PM
@bwkaz - awk uses $1 to mean the first field of the line awk is parsing rather than the first argument to the shell script.

The base format for an awk command from the command line (or from within a shell script) is:

awk -F"<field sep>" '{if (<field ie, $1 or ""variable"" > == < field or ""variable"" >) do this command}'

To pass a shell variable into an awk command, you have to put it in two sets of double quotes. I don't know how many times this has caused me to beat my head on the desk :)