把IP地址的bash变量。有没有更好的办法 [英] Putting IP Address into bash variable. Is there a better way

查看:274
本文介绍了把IP地址的bash变量。有没有更好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个短期和稳健的方式把我的IP地址变成一个bash变量,很好奇,如果有办法做到这一点更简单的方法。这就是我现在怎么做:

  IP =`使用ifconfig | xargs的| awk的'{$打印7}'| sed的-e'S / [A-Z] *:/''/'`


解决方案

您可以看看的本网站替代品。

的一种方法是:

 使用ifconfig | grep的INET地址:'| grep的-v127.0.0.1|切-d:-f2 | AWK'{打印$ 1}

一个有点较小的一个,尽管它是不是在所有坚固,并能根据你的系统返回错误值:

  $ / sbin目录/使用ifconfig | SED -n'2 P'| AWK'{打印$ 3}

(从 http://www.htmlstaff.org/ver.php?id= 22346

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 it:

ip=`ifconfig|xargs|awk '{print $7}'|sed -e 's/[a-z]*:/''/'`

解决方案

You can take a look at this site for alternatives.

One way would be:

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

A bit smaller one, although it is not at all robust, and can return the wrong value depending on your system:

$ /sbin/ifconfig | sed -n '2 p' | awk '{print $3}'

(from http://www.htmlstaff.org/ver.php?id=22346)

这篇关于把IP地址的bash变量。有没有更好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆