Hacker News new | past | comments | ask | show | jobs | submit login

getting an IP is still pretty ugly, somehow:

  ip address show dev eno1|grep 'inet '|cut '-d ' -f6
But I wouldn't call it unreliable. The cut is the only really nasty bit.

Changing your fstab is easy with a little awk:

  $2=="/tmp"{$4=$4 ",nosuid"}
  {print $0}
Given, it won't remove contradicting options...



It's unreliable, because you don't know where you'll find 'inet ' or what's the specific spacing of the output. Also, that's not the ip, because you've still got the cidr attached.

And no, fstab is not easy either. For example it fails for:

    # /tmp has option_blah in order to ...
changing it to:

    # /tmp has option_blah,nosuid in order to ...
Confusing the next person to look at the well intended comment.

It's no impossible to implement fairly reliably, it's not going to take days to do, but you're likely make mistakes. And in both cases you're spawning extra processes instead of just getting the information you need.

PS. also slightly annoying is that everyone is so concerned about the text processing part that barely anyone checks what the tool itself can do. (ip -4 -o a s dev eno1 | awk '{print $4}')


>PS. also slightly annoying is that everyone is so concerned about the text processing part that barely anyone checks what the tool itself can do. (ip -4 -o a s dev eno1 | awk '{print $4}')

Actually, I spend a while digging through the manpage to try and find this. ip has awful docs.

As for the comment problem, yeah, not optimal.


Oh, I forgot: the comment you showed wouldn't trigger my AWK script. Read it more carefully.


Yes it will. If you mean the $2 part, the comment character takes the place of $1. Or did you mean something else? (either way, I tested it before posting)


Nope, you're right. I just miscounted.

You could always add

  && $1 != "#" 
To fix this, though.


And either the "ip" command can never change how it formats its output, or your script with that command will break some day.


...Which is why I said it was ugly. The grep probably won't break, the cut is more likely to. This is in part because the 'ip' command isn't very well designed. A better solution I found is

  ip addr show wlan0 | grep -Po 'inet \K[\d.]+'
Which is less likely to break. If your system supports it,

  hostname -I
Works great. However, this seems to be an ubuntuism, and it doesn't work on my system.


  ip addr show eth0 | awk '/inet/ { print $2 }'
Will be better ;) 'inet[::space::] ' not working with ipv6 addresses.


I though there was a specific desire for ipv4 addrs. That's why the space was there.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: