You've got too many shells doing some processing in there. Also, using backticks is a bad idea especially when there's going to be backslashes in them. You should use the $(...)
syntax instead.
sudo -s
starts a shell to run the command, but with sudo
trying to escape some of the special characters for the shell. You don't want to use that.
ssh
runs a shell on the remote host to interpret the command line that is made of the concatenation of the arguments (with space in between).
So in:
var1=`sudo -u psoadmin -H -s ssh [email protected] find . -maxdepth 1 -type f -mtime +14 -exec ls -lh \{} \;`
sudo
runs:
"/bin/bash", ["/bin/bash", "-c",
"ssh daill_scp\\@files.dc1.responsys.net find \\. -maxdepth 1 -type f -mtime \\+14 -exec ls -lh \\{\\} \\;"]
(/bin/bash
or whatever the login shell of the user is).
Notice how sudo
escaped .
, +
, }
, but not backslash for no particularly good reason.
Then that bash will run:
"/usr/bin/ssh", ["ssh", "[email protected]", "find", ".", "-maxdepth", "1", "-type", "f", "-mtime", "+14", "-exec", "ls", "-lh", "{}", ";"]
ssh
will concatenate those and run on remote host:
"$SHELL", ["$SHELL", "-c", "find . -maxdepth 1 -type f -mtime + 14 -exec ls -lh {} ;"]
(where $SHELL
is the login shell of the remote user this time).
That ;
above is not escaped, so interpreted as a command separator and not passed to find
which is why find
complains that that -exec
is not terminated.
Here, you want:
var1=$(
sudo -u psoadmin -H ssh [email protected] '
find . -maxdepth 1 -type f -mtime +14 -exec ls -lh {} \;' |
awk '{print $5, $9}' |
egrep -v '^./upload|^./download|^./archive|^\.'
)
(not that that command (especially the egrep
part) makes a lot of sense).