LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-12-2014, 10:51 AM   #1
Sorcha11
LQ Newbie
 
Registered: Mar 2014
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 03-12-2014, 11:11 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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?
 
Old 03-12-2014, 11:14 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,028

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
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 ?
 
Old 03-12-2014, 11:31 AM   #4
Sorcha11
LQ Newbie
 
Registered: Mar 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
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`
 
Old 03-12-2014, 11:34 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
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.
 
Old 03-12-2014, 11:38 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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
 
Old 03-12-2014, 11:40 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Maybe the usual problem of \r\n in the shell script?
 
1 members found this post helpful.
Old 03-12-2014, 12:05 PM   #8
Sorcha11
LQ Newbie
 
Registered: Mar 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
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'`
 
Old 03-12-2014, 12:32 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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'
 
Old 03-12-2014, 02:36 PM   #10
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by Sorcha11 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to use scp in a shell script Thaidog Programming 4 04-04-2011 04:10 PM
how to give password in shell script after scp kushkothari Linux - Networking 2 10-14-2008 01:13 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM
Shell Script with ssh & scp ErikJohnson Linux - Software 3 03-11-2004 05:53 PM
Shell Script with ssh & scp ErikJohnson Linux - Networking 1 03-11-2004 05:14 PM


All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration