我需要grep到IP地址(如下面的例子),我使用ksh脚本,
# ifconfig -a | /usr/xpg4/bin/grep "100\.106\.2\.120 " inet 100.106.2.120 netmask ffffff00 broadcast 100.106.2.255
但如何从参数(IP_ADDRESS)grep IP地址?
在这种情况下如何添加反斜杠
IP_ADDRESS = 192.2.34.2,或IP_ADDRESS = 192.2.34.20 …等
# ifconfig -a | /usr/xpg4/bin/grep "$IP_ADDRESS "
如果你使用的是bash shell你可以使用参数扩展:
ifconfig -a | /usr/xpg4/bin/grep "${IP_ADDRESS//./\\.}"
如果你的shell不支持这种types的参数扩展,你可以使用shell扩展,例如sed:
ifconfig -a | /usr/xpg4/bin/grep `echo $IP_ADDRESS | sed 's/\./\\./g'`
或perl:
ifconfig -a | /usr/xpg4/bin/grep `echo $IP_ADDRESS | perl -pne 's/\./\\./g'`