问题
I am trying to copy a group of files within a certain time range from a remote server(server B) with a script run on a local server(Server A). I'm using the -newermt option in find to specify my time range. So if I ssh to the remote server(server B) this works:
find /appl/backup/monsters/green/y.y.y.y/2016-04-26_08-00-01/jelly/ -newermt "2016-04-26 07:40:00" \! -newermt "2016-04-26 07:50:00"
And I get the list of files for the times specified.
However, If I try to involke this from the local server (server A) via ssh:
#!/bin/bash
#
CLUSTER="green"
node="x.x.x.x"
STIME="2016-04-26_08-00-01"
#T1=1461678000
#T2=1461678600
T1="2016-04-26 07:40:00"
T2="2016-04-26 07:50:00"
ssh user@y.y.y.y find /appl/backup/monsters/${CLUSTER}/${node}/${STIME}/jelly/ -newermt "$T1" \\! -newermt "$T2"
echo $latest_file
echo SCP Completed.
exit
This gives me a find error:
find: paths must precede expression: 07:40:00
So it appears that with this method find does not like the space in "2016-04-26 07:40:00"
I tried converting to unixtimestamps to avoid using a space but find does not like those:
find: I cannot figure out how to interpret `1461678000' as a date or time
My question is do I have to delimit the space character for this or curly brackets, I'm trying a few things but totally fumbling around :-/ any help is appreciated.
Thanks!
回答1:
The problem is that you are quoting on the local side but not on the remote side.
The double quotes you are using around "$T1"
and "$T2"
are being removed by the local shell before running the ssh
command but the remote command then doesn't have the quotes protecting the space from the remote shell.
So the local shell sees:
[ssh] [user@y.y.y.y] [find] [/appl/backup/monsters/${CLUSTER}/${node}/${STIME}/jelly/] [-newermt] [2016-04-26 07:40:00] [\!] [-newermt] [2016-04-26 07:50:00]
but the remote shell sees:
[ssh] [user@y.y.y.y] [find] [/appl/backup/monsters/${CLUSTER}/${node}/${STIME}/jelly/] [-newermt] [2016-04-26] [07:40:00] [\!] [-newermt] [2016-04-26] [07:50:00]
and that's the problem.
The argument to -newermt
is seen as 2016-04-26
and 07:40:00
is seen as a stray argument which find
sees as a path and so it gives you the "paths must come first" error.
You need the quotes to make it through to the remote side so double quote the entire string (to allow for variable expansion locally) and then use single quotes around the expanded values for the remote shell.
ssh user@y.y.y.y "find '/appl/backup/monsters/${CLUSTER}/${node}/${STIME}/jelly/' -newermt '$T1' \! -newermt '$T2'"
来源:https://stackoverflow.com/questions/36893033/pass-variable-to-find-via-ssh-for-newermt-option