0

I'm trying to grep a string in the log file in a remote host. but I'm having trouble to return the echo to the host where I'm running the script.

#!/bin/bash
#dt=$(date +"%Y%m%d")
#HHMM=$(date '+%H:%M')
key="keys/cash_prod_key"



if
        ssh -o StrictHostKeyChecking=no -qni $key user@host "cat /var/log/FILE_send.log | grep FILENAME | grep -i success"\;
then
        echo "Success"
fi

I don't think this is the most correct way to do it.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon \; at the end of the ssh command, because that will cause grep to search only for lines containing success; (success followed by a semi-colon). You probably do not need the backslash/semi-colon at all. – RobertL Apr 06 '19 at 07:04
  • @RobertL The escaped ; would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used by grep. 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
  • @RobertL when I run the script it doesn't return the echo success. – anmoreira Apr 06 '19 at 20:11
  • @anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)? – Kusalananda Apr 06 '19 at 21:24
  • @Kusalananda it doesn't return nothing. No é ho and no error – anmoreira Apr 06 '19 at 21:54

1 Answers1

3

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.

Kusalananda
  • 333,661
  • Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also. – anmoreira Apr 06 '19 at 22:01
  • @anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things. – Kusalananda Apr 06 '19 at 22:05
  • found the problem, the hostname was slightly incorrect since the host is part of cluster – anmoreira Apr 06 '19 at 22:09