0

I know that there are some related questions already about this, but can't make it work for me!

So I can run grep in the command line and it works fine, but if I do that on a bash script, I have the following error:

grep: secondword:No such file or directory 

I am connecting via ssh to the server, then I run some commands. The path to grep in the server is /bin/grep, but still it does not work. Here is the sample code:

#!/bin/bash

$host="user@host";
ssh $host "
myinfo=\$(grep "word secondword" path/to/file);
"

I also verified that it does not have the CR that is created in Windows with Notepad++. Any ideas on how to fix this?

EDIT:

As suggested, I made the following change with the quotes:

#!/bin/bash

$host="user@host";
ssh $host "
myinfo=\$(grep \"word secondword\" path/to/file);
"

but now I have a very weird behavior: it looks like is listing all the files that are on the home server path.Doing echo to the variable:

file1 file2 file 3
file4 file5 etc.

Why it as this behavior? Did I miss something?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Edwardo
  • 643
  • 3
  • 9
  • 23
  • You need to escape the quotes for the first grep param or use single quotes. See http://unix.stackexchange.com/questions/30903/how-to-escape-quotes-in-shell – pvg Dec 09 '15 at 07:52
  • @4ae1e1 wrt "shitty syntax highlighting" you know you can change the language in the code block, don't you ? – 123 Dec 09 '15 at 08:04
  • Put the `grep` command inside quotes, otherwise it will execute it on the home server. – cdarke Dec 09 '15 at 08:35
  • 1
    @123 Yes I do (and that involves an HTML comment which is really weird...), but just like every other syntax highlighter there are things that it can't parse. I call this particular one shitty because its limits are really low; for instance, good luck quoting inside quoted command substitution. – 4ae1e1 Dec 09 '15 at 08:39
  • @cdarke (only for clarification) grep command has to be executed on the server. What I mean by home server path is that it is listing the name of the files on the server. So, the quotes still applies or not? (Will check tomorrow, just want to make sure that I am on the same page as you) – Edwardo Dec 09 '15 at 09:04
  • @4ae1e1 probs got flagged for swearing, think if it gets flagged N times it gets automatically removed. Also i agree the highlighting is pretty awful for anything moderately complicated, but it's normally ok for most things. – 123 Dec 09 '15 at 09:07
  • 1
    Are you really sure you need the output in a variable? The common idiom is to use a pipeline or perhaps a temporary file. Assigning a variable *on the remote host* like that certainly makes no sense at all in isolation. – tripleee Dec 10 '15 at 10:49
  • And `ssh $host "myinfo=\$(grep \"word secondword\" path/to/file)"` will definitely not work on the command line, either. Try `myinfo=$(ssh $host grep 'word secondword' path/to/file)` to get the results in a *local* variable, which also allows you to use single quotes for the command instead of double. – tripleee Dec 10 '15 at 10:52
  • What OS run the server and the client? What is `path/to/file` _really_? What files are listed _really_? How do you output `$myinfo`? – Armali Sep 13 '17 at 12:14

1 Answers1

-2

Please Put Script working Dir. While run crontab it will take as user home as default path.

#!/bin/bash
cd Your_Path
$host="user@host"
ssh $host
myinfo=\$(grep "word secondword" path/to/file)
Arun Binoy
  • 327
  • 1
  • 10