interpret bash positional parameter within awk regex string
I've got the following line:
Code:
ip address show | awk -F "[ /]*" '/inet.*eth0/ { print $3 }' Any ideas? |
the 'replace' part of what you're saying is confusing, but the code says find and print.
You're trying to get the ip for your eth0, I got wlan0 not eth0, so this still applies to the same just change the what you're looking for with a little help from grep ( wlan0 to eth0 ).
Code:
$ ip address show | grep wlan0 | awk 'FNR == 2 {print $2}' |
I don't understand what is confusing exactly about replacing 'eth0' with whatever first positional parameter you're going to call the bash script with. Maybe that was confusing, the fact that I didn't mention that it's supposed to be part of a small bash script, so that I can run it simply like this: "bash_script eth0" - that would show me the ip of 'eth0'.
Your solution does the same thing the line I've written already does. Moreover, I'm using one tool (awk), instead of grep + awk, even if the awk syntax becomes somewhat more complicated. So the point is to use bash positional parameters. That's what I'm looking for. |
Quote:
Code:
./scriptName wlan0 Code:
whatIwant=$1 see what this does for you Code:
#!/bin/bash giving this more thought: "I'm trying to replace 'eth0' with the first bash positional parameter," I am wanting to use the first positional parameter in a bash script instead of hard coding the 'eth0' into my awk to find the device and print out the IP attached to it by piping in the output from the 'ip address show' command into awk. if that is what you are trying to do. Code:
$ ./positionalparameter wlan0 |
You could select device in ip show command
Code:
d=eth0 |
Quote:
You might want to add something like: Code:
if [[ ! $1 ]] |
Quote:
Code:
ip address show | awk -F "[ /]*" '/inet.*'$1'/ { print $3 }' |
While the smart solution is in post#5,
here is how you can handle it with an awk variable Code:
ip address show | awk -v iface="$1" -F "[ /]*" '$0~("inet.*" iface) { print $3 }' Code:
ip address show | awk -v iface="$1" -F "[ /]*" 'BEGIN {srch=("inet.*" iface) } $0~srch { print $3 }' |
Not sure if anyone has tested any of these solutions, but on my machine they just return ALL ip's on the inet lines, ie not just whatever everyone thinks $1 is set to,
which by the way I would think is nothing (which also agrees with my findings) The pipe is not a bash script nor does it create output for one. @OP - Exactly what would $1 be in your mind as the output from your command (assuming it did work that way)? |
Quote:
Quote:
|
Quote:
Quote:
$1 Code:
#!/bin/bash |
Quote:
In that case I would do: Code:
ip a s "$1" | awk '/inet/{print gensub(/\/.*/,"",1,$2)}' |
Quote:
it might work with eth0? Code:
$ ./positionalparameter wlan0 Code:
inet |
Quote:
If that is a requirement, simply put a space after the 't' |
Quote:
|
All times are GMT -5. The time now is 11:06 PM. |