The issue was an incorrect host name.
This answer is instead focusing on how to write the code in a better way.
You don't need to run the complete pipeline on the remote host. Running complicated commands on the command line with ssh
is rather error prone, not least from a quoting point of view.
Instead:
#!/bin/bash
key="keys/cash_prod_key"
ssh_args=( -o StrictHostKeyChecking=no -qni "$key" user@host )
if ssh "${ssh_args[@]}" cat /var/log/FILE_send.log |
grep -F 'FILENAME' |
grep -q -i -F 'success'
then
echo 'Success'
fi
This would run only the cat
on the remote host and then run the two grep
commands locally.
I've also added the -F
flag to the grep
commands, assuming that the strings used as patterns are not regular expressions but literal strings that you'd like to search for. The last grep
uses -q
as we're not actually interested in seeing the output, just in whether it contains the string success
or not.
To make it easier to read, I've put the arguments used with ssh
into an array that I later use in the call, and I've also made the lines a bit shorter by simply inserting newlines after the pipe symbols. The script is still syntactically correct. Also note that any expansion of variable etc. should always be double quoted, unless you know in what contexts this is not needed.
Note that I'm assuming that the ssh
command is invoked in the correct way.
\;
at the end of thessh
command, because that will causegrep
to search only for lines containingsuccess;
(success followed by a semi-colon). You probably do not need the backslash/semi-colon at all. – RobertL Apr 06 '19 at 07:04;
would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used bygrep
. To be part of the pattern, it would have to be escaped as\\\;
or as\;
inside the double quotes. – Kusalananda Apr 06 '19 at 08:54