Putting IP Address into bash variable. Is there a better way

前端 未结 18 1857
生来不讨喜
生来不讨喜 2020-12-07 20:09

I\'m trying to find a short and robust way to put my IP address into a bash variable and was curious if there was an easier way to do this. This is how I am currently doing

18条回答
  •  青春惊慌失措
    2020-12-07 20:57

    You can get just awk to do all the parsing of ifconfig:

    ip=$(ifconfig | gawk '
        /^[a-z]/ {interface = $1}
        interface == "eth0" && match($0, /^.*inet addr:([.0-9]+)/, a) {
            print a[1]
            exit
        }
    ')
    

提交回复
热议问题