bashsshgrepfindcommand-substitution

Bash command substitution issues


I'm writing a Python script to parse various logs across different hosts. I can successfully run bash scripts via Python's subprocess module, however, I want to minimize the number of ssh calls, so I'm trying to combine ssh, grep, and find into 1 command.

The problem begins when some hosts may not have any log files. My command is something like:

ssh SOME_HOST 'grep "SOME_VALUE" $(find SOME_PATH -type f -name "*.log")'

When there are no log files, this becomes grep "SOME_VALUE" and hangs forever. I tried wrapping it in double quotes so that it becomes grep "SOME_VALUE" "" instead:

ssh SOME_HOST 'grep "SOME_VALUE" "$(find SOME_PATH -type f -name "*.log")"'

Now I have the exact opposite problem: when there are some logs found, I get:

grep: FILE_A^JFILE_B^J... : No such file or directory

I've been stuck on this for a few days and googling / RTFMing hasn't helped much. I have tried tr to replace newlines with spaces but got some other weird errors :( Is there an easy way to do this in one command?


Solution

  • Add an extra filename argument so the argument list is never empty. You can use /dev/null as a dummy file that never matches grep.

    ssh SOME_HOST 'grep "SOME_VALUE" $(find SOME_PATH -type f -name "*.log") /dev/null'