简体   繁体   中英

Get single IP address without pipe

Centos7 using bash . I need to set an environmental variable, $IP . However, the command to set the variable is done through a company system that expects | to be the delimiter between variables (ie, I'd pass the system a list in the form myIP=something|myOtherIP=something_else|envVar3=17 ).

I am planning for a use case where the IP address is always internal (10.xxx). Is there any command that can return just the one IP address I care about, without using pipe?

hostname -i returns the wrong address (doesn't start with 10.). hostname -I includes the correct address, but it's second in the list of seven. I can easily get the IP with either hostname -I or ifconfig , but it involves piping to grep and then using regex to find the one that starts with 10. I doubt there's any easy way (honestly, I doubt it's possible), but I figured I'd ask anyway.

这样的东西?

grep -o  "10[^ ]*"<<<`hostname -I`

As an alternative to the answer from abhishek phukan , if you always want the second IP in the list - you could use:

cut -f2 -d' ' <<< `hostname -I`

Thanks for the great answer abhishek!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM