Grepping the file into a variable after SSH into a server

Hi All,

When i am logged into a server , i am able to assign grep value to a variable as follows.

VAR=`grep 'Listener stopped' /logs/perf.log`

However, when i log out of the server, and try to execute the following command by first SSHing into server, it throws error.

$ VAR=`ssh Server grep 'Listener stopped' /logs/perf.log`
grep: stopped: No such file or directory

How to correct this?

This way:

VAR=$(ssh Server "grep 'Listener stopped' /logs/perf.log")

Thanks a lot . That worked perfectly.

I have another doubt. How to append 2 variables into a new variables adding new line.

i.e.

var1="x";
var2="y";

I want to display my o/p as

x
y

When i do this

var3=$var1$'\n'$var2;

I get o/p as :

x y

Sounds like you're not quoting the variable on output.

$ var1="x";
$ var2="y";
$ var3=$var1$'\n'$var2;
$ echo $var3
x y
$ echo "$var3"
x
y

Hey that worked great as well. Thanks. I have one more doubt.

I want to retain the result of this variable whenver i am logging into a server . When i am echoing the result of this variable inside server , it is showing blank.

i.e.

$ var1="x";
$ var2="y";
$ var3=$var1$'\n'$var2;

ssh Server
echo "$var3"
---blank---

Actually, i want to do this as due to some reason, i am not able to send mail from putty terminal. the following command is not working(not receiving mail). It is only working inside the server(receiving mail) . If anyone can shed light on this as well, it would be great.

echo "$VAR3" | mailx -s "subject" abc@xyz.com