2011-11-19 58 views
0

我想知道如何从苹果的shell命令的输出只提取某些数据。我想只能从这样的一个“ping -o”命令通过IP地址变量:壳牌在Applescript输出

do shell script "ping -o " & blockedURL 

    -- Set the IP to blockedIP -- 

    set blockedIP to .. 

但我收到此:

“PING example.com(192.0 .43.10):56个数据字节从 192.0.43.10 64个字节:icmp_seq = 0 TTL = 239时间= 101.587毫秒

--- example.com ping统计--- 1层的报文发送,接收的数据包1,0.0%分组丢失往返最小/平均/最大/ stddev = 101.587/101.587/101.587/0.000 ms“

当我执行ping命令时,我收到很多我不需要的数据。有没有什么办法可以只记得(192.0.43.10)

回答

4
set a to "PING example.com (192.0.43.10): 56 data bytes 64 bytes from 192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms 

--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 101.587/101.587/101.587/0.000 ms" 

set text item delimiters to "(" 
set temp to text item 2 of a 
set text item delimiters to ")" 
set temp to first text item of temp 
return temp 

以上是一个完整的applescript解决方案。你也可以使用下面的内容获取IP,只需使用shell ping -o www.google.com | cut -d'(' -f2|cut -d')' -f1 | head -n1,所以在苹果脚本中它看起来像这样: do shell script "ping -o " & blockedURL & " | cut -d'(' -f2 | cut -d')' -f1 | head -n1"

+0

非常感谢你的快速和简洁的回答,这真的帮助我:) –