ifconfig返回的信息中包含IP地址,在shell中可以通过如下方法获取IP地址。
通过使用grep搜索,并排除127.0.0.1,截取第二部分,然后输出。
拷贝键入如下内容:
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
返回并打印IP地址。
或许也可以参考下面的2个文章:
- http://www.cyberciti.biz/tips/read-unixlinux-system-ip-address-in-a-shell-script.html
- http://bash.cyberciti.biz/misc-shell/read-local-ip-address/
【结语】
在制作shell脚本的时候可能会用到,备注一下。
转载请注明:大地博客 » Linux shell获取当前IP地址