find ip address of my system for a particular interface with shell script (bash)
Solution 1
Let's clean up your code first. You don't need chains of a dozen different commands and pipes when you're already using awk. This:
wifiip=$(ip addr | grep inet | grep wlan0 | awk -F" " '{print $2}'| sed -e 's/\/.*$//')
can be written simply as this:
wifiip=$(ip addr | awk '/inet/ && /wlan0/{sub(/\/.*$/,"",$2); print $2}')
but your whole script can be written as just one awk command.
I need you to update your question with some sample output of the ip addr
command, the output you want from the awk command given that input, and explain more clearly what you're trying to do in order to show you the correct way to write that but it might be something like this:
ip addr | awk '
/inet/ { ip[$NF] = $2; sub(/\/.*$/,"",ip[$NF]) }
END { print ( "eth0" in ip ? ip["eth0"] : ip["wlan0"] ) }
' > /home/pi/att/ip.txt
Solution 2
Here is a nice way to get your IP address. This gives you the address used to reach the internet at the test, so it will give you correct IP even if you change from Wifi to eth or to any other IF type.
See more detailed post here: Linux bash script to extract IP address
my_ip=$(ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $NF}')
To get interface name:
my_if=$(ip route get 8.8.8.8 | awk '/dev/ {f=NR} f&&NR-1==f' RS=" ")
Related videos on Youtube
Ayush joshi
Updated on September 18, 2022Comments
-
Ayush joshi over 1 year
I am trying to find ip-address of my own system through a shell script and write into a text thats my script content
#!/bin/bash wifiip=$(ip addr | grep inet | grep wlan0 | awk -F" " '{print $2}'| sed -e 's/\/.*$//') eth0ip=$(ip addr | grep inet | grep eth0 | awk -F" " '{print $2}' | sed -e 's/\/.*$//') if [ "$eth0ip" == "0" ]; then echo "$eth0ip" | grep [0-9]$ > /home/pi/att/ip.txt else echo "$wifiip" | grep [0-9]$ > /home/pi/att/ip.txt fi
and trying to do something like if one interface is not up print another ip in ip.txt
but it's giving
ip.sh: 14: [: unexpected operator
-
Ayush joshi over 10 yearspi@raspberrypi ~/attendance $ sh ip.sh ip.sh: 14: [: Illegal number:
-
Ed Morton over 10 years@Ayushjoshi - What did you run to get that error message? Again, update your question as I asked, especially with the
ip addr
output, otherwise I can neither test nor debug any script I post. I suspect there's something in your shell script that you haven't shown us yet (probably an unmatched quote) and THAT is what is causing your errors - update your script to be as small as possible but still produce the error message and then post the whole script. -
twalberg over 10 yearsBetter yet to write it as
ip addr show dev wlan0 | awk -F'[ /]*' '/inet /{print $3}'
, I would think. Letip
feed you only the interface you want... -
Ed Morton over 10 yearsidk, I never use the
ip
command and it's not available on mycygwin
orSolaris
installations so I don't know what it's output looks like. It's entirely up to the OP to post the output of hisip
command if he wants help writing an awk script to parse it, as I mentioned in my answer. -
Ayush joshi over 10 yearsip addr | awk ' /inet/ { ip[$NF] = $2; sub(/\/.*$/,"",ip[$NF]) } END { print ( "eth0" in ip ? ip["eth0"] : ip["wlan0"] ) } ' > /home/pi/att/ip.txt ya its successfull i have replaced my complete script with this much Thanks @Ed Morton