0

I'm hitting a strange issue with SCP on cygwin,

scp $HOME/deploy.sh user@host:/tmp

works fine, files get copied to the remote system.

But the same command inside a test.sh won't work. I can see that scp command output is same as the last stdout, But when I go to the remote system, These files are not present!

test.sh:

#!/bin/sh
scp $HOME/deploy.sh user@host:/tmp

Looks like it's got something to do with the cygwin, This behavior is not seen on a Linux system. But unfortunately my dev set-up is on windows and can't switch easily.

3
  • What does verbose mode tell you (scp -v)? Jul 16, 2015 at 7:04
  • ...or try adding echo in front of scp, its probably not writing out the statement correctly. Or it might be a terminal issue, try compiling sshpass in cygwin and use it to enter the password. Also just to be pedantic, on some linux systems sh is not necessarily bash. Dunno about cygwin since I can never find a working mirror when I want to use it. Jul 16, 2015 at 7:41
  • @deer-hunter The debug doesn't show anything significant. But the issue is solved when destination is specified as a file instead of a folder name
    – gkns
    Jul 16, 2015 at 8:07

4 Answers 4

1

I just got this behaviour now and the issue was the linebreak. (^M)

Try converting the script file with dos2unix. It solved for me.

0

This might be a bug, But I'm not sure.

The issue is not seen with :

scp $HOME/deploy.sh user@host:/tmp/deploy.sh

Issue is seen when a folder name is given as destination:

scp $HOME/deploy.sh user@host:/tmp
1
  • Thanks, the issue is resolved when destination is specified as a file instead of a folder name
    – gkns
    Jul 16, 2015 at 8:08
0

I am pretty sure that it's a typo in your script. To verify

1) create a directory under /tmp. Let's say script_test.

2) cd to script_test and run your script using sh /path/to/script.

3) verify that files are not present on remote server

4) check script_test directory for any newly created files.

0

I think in some scenarios (a lot of times depending on OS), scp behavior interprets directory trailing slashes differently, depending on the situation. This is actually ALWAYS the case with utilities such as rsync, but scp tends to be OS-dependent.

Because of this, if I am using scp in a script to copy a file to a remote directory and want it to retain the original filename, I always add a trailing slash to the directory. I just confirmed that this works:

Cygwin Bash Script:

#!/bin/bash
scp /cygdrive/c/users/jamey/desktop/program.sh 10.0.0.254:/tmp/

Remote Verification:

$ ssh 10.0.0.254
Password:
~/.bash_profile LOADED.
cnu326bxdx:~ Jamey$ ls -l /tmp/program.sh
-rwxr-xr-x  1 Jamey  wheel  77 Sep 24 09:51 /tmp/program.sh

Another important thing to note is the permissions on the directory that you are trying to copy to. If your user doesn't have access privileges to the directory you are trying to copy to, the file will obviously not get written. That should be obvious, but sometimes it's the most obvious caveats that create the biggest issues.

One last thing (which I am not a pro on by any means, but am only offering as another possible suggestion) is that if you have spaces in whatever directory your $HOME variable resolves to, that variable could be getting expanded in the shell, which allows the command to work correctly. Alternately, if you do not surround the variable in double-quotes in the script, it may not be getting expanded -- but that would generally cause a visible error, which leads me to believe this particular scenario not to be the case. Either way, you can never go wrong with double-quoting your variables.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .