Using ssh to transfer file not working inside if-condition

Hi all,

ssh uname@remote_server 'cat /tmp/remote_file_name > home_dir/a512386/new/local_file_name'

The above given command is working fine.
but if i try to move the file only if exists in the remote server i.e) giving the same within if condition it executes but the file is not stored in my local.

 
cmd= ssh a512386@10.116.167.146 "
if [ -f /tmp/remote_file_name ]
then
cat /tmp/remote_file_name > local_file_name
fi"
`${cmd}`

Thanks in advance

This is because everything you do is remote. If you want to create/copy the file locally (you are not moving it btw.), then you can try something like this:

ssh a512386@10.116.167.146 "if [ -f /tmp/remote_file_name ]; then cat /tmp/remote_file_name; fi" > local_file_name

#or without redirection stdout and scp instead

ssh a512386@10.116.167.146 "if [ -f /tmp/remote_file_name ]; then scp /tmp/remote_file_name yourbox:/somedir; fi"

You could also just skip the test if the file exists and scp right away while redireting stdout and stderr to /dev/null, if that is ok for you.

scp a512386@10.116.167.146:/tmp/remote_file_name /somedir > /dev/null 2>&1

.. or to some logfile instead of course.

EDIT:
If I didn't get you wrong and meant with "local" your local box.