篇首语:本文由小编为大家整理,主要介绍了shell脚本切割字符串分割字符串(split)相关的知识,希望对你有一定的参考价值。
参考文章:【shell】shell字符串分割|文件分割|剪切|截取
比如,要将addr:192.168.1.1
按:
分割,获取后面的ip地址
我们ifconfig的结果是:
root@xy-virtual-machine:/kyai_nv# root@xy-virtual-machine:/kyai_nv# ifconfigens33 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx inet addr:192.168.1.139 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: xxxx::xxxx:xxxx:xxxx:xxxx/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:71062 errors:0 dropped:0 overruns:0 frame:0 TX packets:25946 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7956811 (7.9 MB) TX bytes:56476592 (56.4 MB)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1377 errors:0 dropped:0 overruns:0 frame:0 TX packets:1377 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:27715123 (27.7 MB) TX bytes:27715123 (27.7 MB)root@xy-virtual-machine:/kyai_nv#
用下面这个脚本就能将外网ip提取出来
run.sh
#!/bin/bashNETWORK_CARD=`route | awk "NR==3 print $8"`IP=`ifconfig $NETWORK_CARD | grep inet | grep -v inet6 | awk "print $2" | awk "split($1, arr, ":"); print arr[2]"`echo IP=$IP
以上是关于shell脚本切割字符串分割字符串(split)的主要内容,如果未能解决你的问题,请参考以下文章