6

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 
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ayush joshi
  • 305
  • 1
  • 5
  • 14
  • Consider simplifying this by getting `ip` to produce more parseable output; for example, `ip -f inet -o addr show dev wlan0`. – Joe Oct 07 '13 at 12:32

2 Answers2

16

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • pi@raspberrypi ~/attendance $ sh ip.sh ip.sh: 14: [: Illegal number: – Ayush joshi Oct 07 '13 at 13:01
  • 1
    @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. – Ed Morton Oct 07 '13 at 13:21
  • 2
    Better yet to write it as `ip addr show dev wlan0 | awk -F'[ /]*' '/inet /{print $3}'`, I would think. Let `ip` feed you only the interface you want... – twalberg Oct 07 '13 at 15:19
  • 1
    idk, I never use the `ip` command and it's not available on my `cygwin` or `Solaris` installations so I don't know what it's output looks like. It's entirely up to the OP to post the output of his `ip` command if he wants help writing an awk script to parse it, as I mentioned in my answer. – Ed Morton Oct 07 '13 at 15:23
  • 1
    ip 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 – Ayush joshi Oct 08 '13 at 05:12
10

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=" ")
Community
  • 1
  • 1
Jotne
  • 40,548
  • 12
  • 51
  • 55