2011-11-19 93 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

非常感謝你的快速和簡潔的回答,這真的幫助我:) –