-   Linux - General (
-   -   Piping directory listing to sftp questiong (

scoghill 01-23-2008 10:16 AM

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.

ilikejam 01-24-2008 04:35 AM


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.


All times are GMT -5. The time now is 07:53 PM.