2016-02-05 171 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地址,所以它在我的网络上工作。