Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 01-23-2008, 10:16 AM   #1
LQ Newbie
Registered: Dec 2005
Posts: 2

Rep: Reputation: 0
Piping directory listing to sftp questiong

I am posting this on several forums (whether they're computer forums or not) in hopes of getting a answer soon. I'm not a strong Linux /bin/bash script writer. I do know Perl fairly well, but the Perl script is executing a Linux command in a subshell.

We have a Perl script that author just now noticed hasn't been working for 2 months. They added the "-1" option on the ls command below to make it a single column listing. I claim (and some minor testing supports it) that the "echo ls -1 download/$user" is simply going to produce the string 'ls -1 download/<WHATEVER>' and pass that as STDIN to the sftp command, not a list of files in the download/<WHATEVER> directory.

my $cmd = "echo ls -1 download/$user | sftp -oIdentityFile=$identityFile " . "-oPort=444 " . "-oPasswordAuthentication=no ". "-oPubkeyAuthentication=yes ". "$user\@$destAddress 2>&1";

my $output = `$cmd`;

I believe that the echo should be removed from the $cmd for this to work as intended. I personally would have globbed the directory and passed individual file names down to sftp in a loop. Or done the equivalent loop in the Linux command line.

Any help is greatly appreciated.
Old 01-24-2008, 04:35 AM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

There's a number of things wrong here.
1) No need for the -1 - ls always outputs in single column mode when it's not outputting to a tty
2) Yes, you're right. The echo will just echo 'ls -1 download/$user' into sftp.
3) Just echoing a bunch of file names into sftp won't do anything. You'll need a 'put' or a 'get' command in front.

If I were doing this, I'd do:
ls | sed 's/^/\"/' | sed 's/$/\"/' | sed 's/^/put /' | sftp ......
The first two sed invocations wrap each file name in quotes, in case there's spaces in the filenames. The third sed add 'put ' to the start of the line.



linux, perl, script, scripts, shell

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 07:03 AM
apache directory listing doralsoral Linux - Software 2 06-15-2005 08:37 AM
directory listing script linuxzouk Programming 2 06-04-2004 01:07 PM
Directory Listing mattbeekler Linux - Software 8 07-22-2003 03:27 PM
Java Directory Listing markdw Programming 1 04-16-2002 07:06 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:59 PM.

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