Thursday 6 September 2018

awk does not print the expected field and hellip; (nothing for the moment)

I know this appears a bit much for what I am trying to receive but it is part of a greater script so it is necessary to have the two set of awk commands piped the way they are.
Provided that I have an environment such as:
interface=br1
and my command being:
ip address show | awk '/\<inet\>/ { print $NF, '\t', $2 }' | awk '/\<"$(echo $interface)\>/ { print $3 }'

does anyone know why I can't get the appropriate IP output that I am expecting?
EDIT:
I wanted to edit this a bit to explain a bit more about what I am trying to do. I am pulling the information from ip address show to supply information to two different variables.
One variable is chosen by the user. That is the interface name. The second variable is derived from the user's choice. and that derivative is directly placed into another command which is to be run at the end of the sequence.
To put it simply I am creating a commandline helper for virt-install.
The user chooses the bridge or interface they want to use with the VM and then that gets set to the $interface variable destined for the --network=bridge: flag.
However the console redirect is directly affected by the interface chosen. Therefore I have substituted this command in its place:
NETWORK=ip address show | awk '/\<inet\>/ { print $NF, '\t', $2 }'
.....
echo "Please choose your bridge interface"
$NETWORK
printf "Interface: ": read interface
......
--graphics vnc,listen=`$NETWORK | awk -v i=$interface '/\<i\>/ { print $2 }'`,port=5910,keymap=jp


You don't need 2 awk because you can filter what you want in one step
export interface=br1
ip address show | awk '/\<inet\>/ && $NF == interface { print $NF, '\t', $2 }' interface="$interface"

or, you can just provide the interface to ip
ip address show $interface | awk '/\<inet\>/ { print $NF, '\t', $2 }'

EDIT

If you only want the IP address without the mask
ip address show $interface | awk '/\<inet\>/ { sub("/.*$", "", $2); print $2; }'

Try
man awk

to see the available functions.

0 comments:

Post a Comment