LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scp in shell script does not work with variables (https://www.linuxquestions.org/questions/linux-newbie-8/scp-in-shell-script-does-not-work-with-variables-4175497934/)

Sorcha11 03-12-2014 09:51 AM

scp in shell script does not work with variables
 
I am currently writing a simple shell script, copydirectory.sh
The script is supposed to automatically copy a directory, including all subfolders and files to a different server.

The code snippet in question is the following, based on only variables:
Code:

scp -rp $ORIGFOLD $USER@$SERVER:$TARGETDIR
However, the command does not work, nor does it work when I put the variables in brackets like this.
Code:

scp -rp ${ORIGFOLD} ${USER}@${SERVER}:${TARGETDIR}
When, just for test reasons, I enter values instead of the variables as shown below, it works.
Code:

scp -rp /folder/subfolder1/subfolder2/ user123@server123:/folder/subfolder/
I have used 'echo' to show me the values of all the used variables, all of them are set correctly, so I am currently at a loss as what exactly is wrong.

The error message I get is : ": No such file or directory"

Any help would be appreciated, as this error has been driving me mad for the past two hours, since I unsuccessfully tried everything I could think of to find either a solution or a workaround.

colucix 03-12-2014 10:11 AM

Hi and welcome to LinuxQuestions!

Any clue from the verbose output of scp (option -v)?

Just an aside note: USER is a reserved shell variable. Have you changed its value before running the scp command or do you use the default?

schneidz 03-12-2014 10:14 AM

worx for me:
Code:

[schneidz@hyper ~]$ i=/home/schneidz/temp-cc/
[schneidz@hyper ~]$ u=schneidz
[schneidz@hyper ~]$ s=xbmc
[schneidz@hyper ~]$ o=/home/schneidz/mag-stripe-reader
[schneidz@hyper ~]$ scp -rp $i $u@$s:$o
whs.tmp                                                                    100% 7072    6.9KB/s  00:00   
hello~                                                                    100%    0    0.0KB/s  00:00   
payflow_test.bat                                                          100% 2589    2.5KB/s  00:00   
readme_paypal_oracle.txt                                                  100% 5169    5.1KB/s  00:00   
deprecated-list.html                                                      100% 4760    4.7KB/s  00:00   
allclasses-frame.html                                                      100%  10KB  9.8KB/s  00:00   
package-list                                                              100%  16    0.0KB/s  00:00   
...

is there a : in any of your paths ?
your profile indicates you are using windows... maybe there is something fubar with microsoft's version of ssh/scp ?

Sorcha11 03-12-2014 10:31 AM

Here is the verbose output of the scp (I left out the part before it asks for the password)

Code:

debug1: Authentication succeeded (keyboard-interactive).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -r -p -t /oracle/sw/
: No such file or directory
: No such file or directory0582/


No, there aren't any : or similiar stuff in the paths. I'm accessing the server (Linux x86_64) via Putty from a Windows Laptop ;)

P.S.: In the code, I actually do not user USER but ACCOUNTID, which is set as follows:
Code:

MYTTY=`tty | cut -d/ -f 3,4`
ACCOUNTID=`who | grep "$MYTTY " | cut -d " " -f1`


TenTenths 03-12-2014 10:34 AM

If you're using bash then use the following at the top of your script:

Code:

#!/bin/bash
set -x

Then when you run the script from the command line you'll see what's happening line by line and see how your parameters are being treated.

grail 03-12-2014 10:38 AM

Always start at the beginning:

1. Always quote variables when not sure if they are affecting the outcome (generally, quoting can almost never hurt)

2. Assuming we are talking bash, as soon as the script is not providing the expected output, place the following as the second line of the script and run again:
Code:

set -xv

colucix 03-12-2014 10:40 AM

Maybe the usual problem of \r\n in the shell script? :scratch:

Sorcha11 03-12-2014 11:05 AM

Thank you all so much. colucix was right. Using the "set -xv" I found that two variables, which I read from a configuration file, had an attached "\r"
I solved it by now setting the variables as follows:

Code:

TARGETDIR=`grep COPYDEST: $MYCONFIGFILE | awk -F: '{ print $2 }' | tr -d '\r'`

grail 03-12-2014 11:32 AM

Neither grep nor tr are required as awk can search for strings and have its record separator (RS) to include additional characters, such as '\r'

lleb 03-12-2014 01:36 PM

Quote:

Originally Posted by Sorcha11 (Post 5133294)
Here is the verbose output of the scp (I left out the part before it asks for the password)

Code:

debug1: Authentication succeeded (keyboard-interactive).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -r -p -t /oracle/sw/
: No such file or directory
: No such file or directory0582/


you are being told write there you are trying to send something that is not there.


All times are GMT -5. The time now is 06:51 PM.