Using a find command in ssh but using local variables?

I have a script like this (Yes, I know the DAY6 number isn't right - I'm just testing at this point):

DAY0=`date -I`
DAY1=`date -I -d "1 day ago"`
DAY6=`date -I -d "2 days ago"`

if [ ssh root@synology1 -d /volume1/Fileserver/$DAY6 ]
then
ssh root@synology1 nohup rm -rf "/volume1/Fileserver/$DAY6"
fi

I've tested the line to remove the files at the command line and it works great, but I'd like to skip it doing this if the folder doesn't exist.

Now, I've read that I need to modify the if line to something like:

ssh root@synology1 'if [ -d /volume1/Fileserver/$DAY6 ] then nohup rm -rf "/volume1/Fileserver/$DAY6" fi'

My question is, I don't think this will work as $DAY6 is a local variable to the local script and won't be available on the system I'm SSH'ing into.

Anyone know how I could do this?

Thanks in advance!

If I am correct,

When you are doing ssh all the local varibles will get expanded before the connection made to remote machine.

Instead of "rm" just try with "ls" command and see if that's working.

Why don't you go ahead and try (replacing the rm command by e.g. echo )?
I think if you use double quotes instead of single quotes, the shell should expand your variables before running the ssh cmd.

Ok, I tried just to create a test script:

DAY6=`date -I -d "2 days ago"`

ssh root@synology1 `if [ -d /volume1/Fileserver/$DAY6 ] then echo "Found" fi`

But I get the following error upon execution:

./test.sh: command substitution: line 4: syntax error: unexpected end of file

I then changed the back quotes to forward quotes and got the following error:

ash: syntax error: unexpected end of file (expecting "then")

I'm stuck - any ideas on what I need to do?

---------- Post updated at 01:14 PM ---------- Previous update was at 01:13 PM ----------

Ok, that showed up as I was writing my reply. Let me give that a try...

---------- Post updated at 01:15 PM ---------- Previous update was at 01:14 PM ----------

Ok, I changed the command to:

DAY6=`date -I -d "2 days ago"`

ssh root@synology1 "if [ -d /volume1/Fileserver/$DAY6 ] then echo "Found" fi"

Got the same error message:

ash: syntax error: unexpected end of file (expecting "then")

try with this:

ssh root@synology1 "if [ -d /volume1/Fileserver/$DAY6 ] ; then echo "Found"; fi"

Actually just found this code online:

DAY6=`date -I -d "2 days ago"`

ssh root@synology1 "if [[ -d /volume1/Fileserver/$DAY6 ]]; then echo Found; fi"

Works!

Thanks!