[英]Get single IP address without pipe
Centos7
using bash
. Centos7
使用bash
。 I need to set an environmental variable, $IP
. 我需要设置一个环境变量
$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
). 成为变量之间的分隔符(即,我以
myIP=something|myOtherIP=something_else|envVar3=17
的形式向系统传递列表)。
I am planning for a use case where the IP address is always internal (10.xxx). 我正在计划IP地址始终是内部(10.xxx)的用例。 Is there any command that can return just the one IP address I care about, without using pipe?
是否有任何命令可以不使用管道而仅返回我关心的一个IP地址?
hostname -i
returns the wrong address (doesn't start with 10.). hostname -i
返回错误的地址(不以10开头)。 hostname -I
includes the correct address, but it's second in the list of seven. hostname -I
包含正确的地址,但在七个列表中排名第二。 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. 我可以使用
hostname -I
或ifconfig
轻松获取IP,但是它涉及管道传输到grep,然后使用regex查找以10开头的IP。我怀疑是否有任何简便的方法(老实说,我怀疑是否可能),但是我想通了,我还是要问。
这样的东西?
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: 作为abhishek phukan答案的替代方法,如果您始终想要列表中的第二个IP,则可以使用:
cut -f2 -d' ' <<< `hostname -I`
Thanks for the great answer abhishek! 感谢您的精彩回答abhishek!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.