What you need is to convert the if
statement into an argument to bash
. Doing a simplistic transform, assuming that the code in the question has a chance of working, you get:
tail -f nohup.out |
xargs -I '{}' bash -c "if [[ {} == *"denied"* ]]; then dig -x $(cut -d '-' -f 6 {} | cut -d ':' -f 1) & fi"
This is exactly the same basic treatment as was needed for a for
loop being executed by nohup
— you need a shell to run the built-in command. See Why can't I use Unix nohup
with Bash for
loop? for an exactly analogous situation.
However, on further reflection, you want to cut the string which is the IP address, not the file with that as a name, so the command needs to echo the string into the cut
commands. You also have to tear your hair getting the sub-commands executed correctly; you need a backslash before the $
of $(…)
, or before each of the back-ticks if you insist on using `…`
notation, as well as using backslash-double-quote to protect the angle-brackets in the string.
tail -f nohup.out |
xargs -I '{}' bash -c "if [[ '{}' != *denied* ]]; then echo dig -x \"\$(echo '{}' | cut -d '-' -f 6 | cut -d ':' -f 1)\" & fi"
Now we need to debate the use of the condition and two cut
commands (and the general hair loss). You could use:
tail -f nohup.out |
grep -v denied |
xargs -I '{}' bash -c "echo dig -x \$(echo '{}' | cut -d '-' -f 6 | cut -d ':' -f 1) &"
or, more sensibly:
tail -f nohup.out |
awk -F '[-:]' '/denied/ { next } { print "dig -x " $7 " &" }' |
sh -x
or any of a myriad other ways to do it.