2016-02-05 269 views
-1

我寫了一個awk腳本,從file.txt的和這個文件的每一行進行調用system("...")需要一定的值。 問題是,終端只接受最後一次系統調用。系統(「」)調用

我需要每調用一次system("...")呼叫,我的file.txt的每一行都會被執行。

例如:file.txt

00:00:6c:19:8f:b1:d8:27 10.0.0.10 1 
00:00:6c:19:8f:b1:d8:27 10.0.0.11 1 
00:00:6c:19:8f:b1:d8:28 10.0.0.12 3 
00:00:6c:19:8f:b1:d8:28 10.0.0.11 2 

例如file.awk:

BEGIN { 

} 
{ 
switch_mac=$1 
ip_dst=$2 
output_port=$3 

system("curl" " -d" " '{" "\"switch\""":" "\""switch_mac"\"""," "\"cookie\""":""\"1\"""," "\"priority\""":""\"2\"""," "\"eth_type\""":""\"0x0800\"""," "\"ipv4_dst\""":""\""ip_dst"\"""," "\"active\""":""\"true\"""," "\"actions\""":""\"output="output_port"\"""}'" " http://10.0.0.11:8080/wm/staticflowpusher/json") 

//here i need to have the execution of the system call 
} 
+1

你爲什麼要把它放在'awk'中? – pfnuesel

+0

你想運行的實際'curl'命令是什麼 - 你能舉一個沒有awk所需的可怕引號的例子嗎? –

+0

在AWK中,將兩個字符串相鄰放置是字符串連接。所以這些報價都不需要;他們什麼都不做,只是混淆了代碼。 – e0k

回答

1

你每行調用外部過程中一旦反正所以任何加速,你可能期望從使用awk來看看讀取文件不值得擔心:

while read -r switch_mac ip_dst output_port; do 
    # your curl command with "$switch_mac", "$ip_dst" and "$output_port" (include the quotes!) 
done < file.txt 

您可以捕獲如果您願意,可以使用output=$(curl ...)作爲響應,或者甚至在需要時將循環的輸出傳遞給awk。

0

你或許可以用GNU並行沿着這些線路做這一切只是足夠的並行:

parallel --colsep ' ' -a file.txt echo curl -d '{"switch": "{1}", "cookie":"1", "priority":"2", "ip4_dst":"{2}", "active":"true", "actions":"output={3}"}' http://192.168.0.1:8080/wm/staticflowpusher/json 

輸出

curl -d {switch: 00:00:6c:19:8f:b1:d8:27, cookie:1, priority:2, ip4_dst:10.0.0.10, active:true, actions:output=1} http://192.168.0.1:8080/wm/staticflowpusher/json 
curl -d {switch: 00:00:6c:19:8f:b1:d8:27, cookie:1, priority:2, ip4_dst:10.0.0.11, active:true, actions:output=1} http://192.168.0.1:8080/wm/staticflowpusher/json 
curl -d {switch: 00:00:6c:19:8f:b1:d8:28, cookie:1, priority:2, ip4_dst:10.0.0.12, active:true, actions:output=3} http://192.168.0.1:8080/wm/staticflowpusher/json 
curl -d {switch: 00:00:6c:19:8f:b1:d8:28, cookie:1, priority:2, ip4_dst:10.0.0.11, active:true, actions:output=2} http://192.168.0.1:8080/wm/staticflowpusher/json 

從命令刪除單詞echo如果輸出看起來是正確的,那麼它將實際運行命令而不是回顯它們。請注意,echo目前隱藏了一些雙引號。還要注意我改變了最終的IP地址,所以它在我的網絡上工作。