Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading