0

I have the following command substitution in a command passed to a ssh host.

shpass -f /home/user/folder/password ssh o StrictHostKeyChecking=no user@host "/bin/grep INFO "$(find /app/logs/* -type f -name "catalina*.log" |grep -i batch | xargs ls -lrth | tail -1 | awk '{print $9}')" | /usr/bin/tail -10 > /home/user/folder/monitor/bcs01_batch_check_catalina.log; grep -i '\''Server startup in'\'' /home/user/folder/monitor/bcs01_batch_check_catalina.log | wc -l"

What I am trying to do is to grep INFO from the latest catalina log available on the host server that I am connecting with ssh. But the find command runs on my local host.

Expected result should be 1, as the file exists on the ssh host, but I am getting the following error message

********************************************************************
*                                                                  *
* This system is for the use of authorized users only.  Usage of   *
* this system may be monitored and recorded by system personnel.   *
*                                                                  *
* Anyone using this system expressly consents to such monitoring   *
* and is advised that if such monitoring reveals possible          *
* evidence of criminal activity, system personnel may provide the  *
* evidence from such monitoring to law enforcement officials.      *
*                                                                  *
********************************************************************
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file

I tried with putting the command

"$(find /app/logs/* -type f -name "catalina*.log" |grep -i batch | xargs ls -lrth | tail -1 | awk '{print $9}')" 

between ``, also escape the " and $ characters, but still no success

\"\$(find /app/logs/* -type f -name "catalina*.log" | grep -i batch  | xargs ls -lrth | tail -1 | awk '{print $9}')\" 

It seems that the find command is ran locally, instead of the ssh host.

What can I do?

Thanks

Steven84
  • 3
  • 2
  • You should first ask for help with the command `/bin/grep INFO "$(find /app/logs/* -type f -name "catalina*.log" |grep -i batch | xargs ls -lrth | tail -1 | awk '{print $9}')" | /usr/bin/tail -10 > /home/user/folder/monitor/bcs01_batch_check_catalina.log; grep -i '\''Server startup in'\'' /home/user/folder/monitor/bcs01_batch_check_catalina.log | wc -l` outside of calling it from `ssh` as there's a lot of problems with it, including a couple of anti-pattterns. Get that command working robustly outside of ssh first and then worry about calling it from ssh. – Ed Morton Sep 09 '22 at 13:57
  • For example `grep INFO $(find...)` should be `find ... -exec grep` or similar or it'll fail given various file names find could output and `ls -lrth | ...` is parsing the output of `ls` which you should never do, see https://mywiki.wooledge.org/ParsingLs. – Ed Morton Sep 09 '22 at 14:02
  • It *is* run locally; you are attempting to nest a `"` inside a `"`-quoted string. It has to be escaped, or you are actually *closing* the quotes. – chepner Sep 09 '22 at 14:29
  • @EdMorton Not sure I follow you. I am trying to grep something from the output of find which should be the path to the latest available catalina log. I tried the below command and it fails: `find /app/logs/* -type f -name "catalina*.log" -printf "%t - %p\n" | grep -i batch | sort -n | tail -1 | awk '{print $7}' -exec grep -i INFO {}\; awk: fatal: cannot open file `-exec' for reading (No such file or directory)` – Steven84 Sep 09 '22 at 14:40
  • Right, so ask for help fixing THAT first, not calling it from inside a call to ssh as that adds an extra layer of unnecessary complexity. If you THEN need help calling the command you end up with from ssh THEN you could ask a question about that part. – Ed Morton Sep 09 '22 at 14:48
  • To be clear: If you get it working _without_ ssh, you can _tell the shell itself_ to escape it to work over ssh -- we already have Q&A entries showing how to do that for completely arbitrary code, so you have no reason to do that escaping by hand. So first forget about SSH and build a working command; _then_ put that working command in a function and use one of our previously-answered questions telling you how to run a shell function over ssh. – Charles Duffy Sep 09 '22 at 15:14
  • Insofar as this question is focused around SSH, I closed it as a duplicate of one of those Q&A entries telling how to run arbitrary code over SSH (by shipping it over-the-wire as a function). The _other_ part of your question is how to modify your code to work reliably at all (even without SSH being in play). For that you should follow the advice Ed gave earlier. – Charles Duffy Sep 09 '22 at 15:16
  • (or ask a separate question with the SSH parts factored out, just asking about how to perform your desired operation reliably; on that point, see [BashFAQ #3](https://mywiki.wooledge.org/BashFAQ/003) discussing finding the newest file, and read up on awk -- you never need `awk | grep` or `grep | awk` since awk can do everything grep can do, and often better) – Charles Duffy Sep 09 '22 at 15:17
  • BTW, in Ed's advice, `-exec` is a `find` action. It's not expected to be given as an argument to any command other than `find`. – Charles Duffy Sep 09 '22 at 15:20

0 Answers0