Shell variables are not magically passed from client to server over ssh
. Some systems accept passing environment variables as long as their name starts with LC_
(look for AcceptEnv LC_*
in they sshd_config
), so you could do:
LC_type="$type" ssh -o SendEnv=LC_type user@hostname '
file=$(
find ~ -type f -name "installation_xx_int_${LC_type}_*log" -mtime -1 \
-exec basename "{}" \;
)
cat -- "$file"
' > ~/test.log
If $type
doesn't contain characters that are special within double quotes in the syntax of the remote shell ("
, `
, $
, \
if the remote shell is POSIX-like), you could have the local shell expand it in the code argument to ssh
:
ssh user@hostname '
file=$(
find ~ -type f -name "installation_xx_int_'"$type"'_*log" -mtime -1 \
-exec basename "{}" \;
)
cat -- "$file"
' > ~/test.log
For that expansion to happen, we need to get out of the single quotes (for the local shell) and into double quotes.
In all those, if $type
contains wildcards (*
, ?
, \
, [...]
), they will be treated as such by find
's -name
which takes a pattern, and you'd need to escape them with \
for them to be taken literally.
It's not clear to me why you're taking the basename and not just doing:
ssh user@hostname '
find ~ -type f -name "installation_xx_int_'"$type"'_*log" -mtime -1 \
-exec cat {} +
' > ~/test.log
If, as clarified in the later edit to your question, you want to store the contents of the remote files into files by the same name on the local system, you'd do something like (assuming those are GNU systems, though you should be able to do something similar with pax
, star
(which can also do the find
part by itself) or libarchive bsdtar
):
ssh user@hostname '
find ~ -type f -name "installation_xx_int_'"$type"'_*log" -mtime -1 -print0 |
tar --null -T - -cf - |
gzip -1' | tar --transform='s:.*/::' -zxf -
That is, use the tar
archive format to store both file path and contents (and the --transform
on the receiving tar
on the local machine, strips off the directory part).