xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2

I am trying to do an rsh grep to search for date records inside server logs by doing this :

xx=`date +"%a %b %d"`
rsh xxx grep "^$XX" zzz

gives :
grep: 0652-033 Cannot open Jun.
grep: 0652-033 Cannot open 11.

But if I do :
xx=`date +"%a %b %d"`
grep "^$XX" zzz

it works !

It is as if when used with something like the 'rsh', the double quotes are removed.

Any way around this ?

The reason for your problem is the command evaluation process in ksh. You might want to read what i have written for a different (but related) problem here.

Your commandline is first interpreted locally and the content of the variable is filled in and protected by the double quotes. Now a remote connection is opened and the rest of the line is passed as command line to the remote shell interpreter. Guess what? At the remote site the commandline is getting interpreted *again* and now the double quotes are not there any more because they are already "used". The command will look like this (first line=local, second line=remote):

rsh host grep ^Sat Jun 13 06:19:07 CEST 2009 file
              --------one argument-----
grep ^Sat Jun 13 06:19:07 CEST 2009 file

And of course "grep" will try to find "^Sat" is a file called "Jun", which leads to the error.

The solution is to protect your double quotes by escaping them:

rsh host grep "\"$xx\"" file

I hope this helps.

bakunin

It worked !

Thank you.