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 Code:
scp -rp ${ORIGFOLD} ${USER}@${SERVER}:${TARGETDIR} Code:
scp -rp /folder/subfolder1/subfolder2/ user123@server123:/folder/subfolder/ 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. |
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? |
worx for me:
Code:
[schneidz@hyper ~]$ i=/home/schneidz/temp-cc/ your profile indicates you are using windows... maybe there is something fubar with microsoft's version of ssh/scp ? |
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). 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` |
If you're using bash then use the following at the top of your script:
Code:
#!/bin/bash |
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 |
Maybe the usual problem of \r\n in the shell script? :scratch:
|
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'` |
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'
|
Quote:
|
All times are GMT -5. The time now is 06:51 PM. |