Bash script - String within a string as option value -


i using nfcapd capture netflow export packets. has -x option call program when new flow output file available. want call nfdump whenever new file becomes available, run nfcapd this:

#!/bin/bash t="nfcapd -p 5566 -l /root/nfcapd_log/ -t 5 -x \"nfdump -r %d%f \"" echo $t eval $t 

which calls nfdump fine see output on screen.

i pass path new file nfcapd -r option in nfdump.

my problem need pass fmt: %ts %te %td %pr %sa %da %sp %dp %ra %in %out %pkt %ipkt %opkt %ibyt %obyt %fl %dir %ismc %odmc %idmc %osmc argument nfdump string tell type of flow information want, needs in quotes. have experimented escaping quotes getting nowhere. script far:

#!/bin/bash t1="nfcapd -p 5566 -l /root/nfcapd_log/ -t 5 -x  \"nfdump -r %d%f -o \\\"fmt: %ts %te %td %pr %sa %da %sp %dp %ra %in %out %pkt %ipkt %opkt %ibyt %obyt %fl %dir %ismc %odmc %idmc %osmc\\\" \" " echo $t1 eval $t1 

but nfdump doesn't format information printed.

i new bash there might simple solution. appreciated.

thanks.

first of all, can rid of 1 level of quotes using helper function (called output_and_exec() in following example) outputs , executes passed it. then, use single quotes when passing command nfcapd. these single quotes can include character except single quotes, use double quotes argument nfdump.

output_and_exec() { echo "$@" ; "$@" ; } output_and_exec nfcapd -p 5566 -l /root/nfcapd_log/ -t 5 -x  'nfdump -r %d%f -o "fmt: %ts %te %td %pr %sa %da %sp %dp %ra %in %out %pkt %ipkt %opkt %ibyt %obyt %fl %dir %ismc %odmc %idmc %osmc"'  

disclaimer: haven't tested since don't have nfapd/nfdump programs.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -