I couldn't test this 100% as-is but a similar version worked.#!/usr/bin/perl use strict; use warnings; use IPC::Open2; use Fcntl qw(F_SETFL F_GETFL O_NONBLOCK); my ($rh, $wh); my $pid = open2 ($rh, $wh, "sftp somehost") or die "can't sftp: $!"; my $flags = fcntl ($rh, F_GETFL, 0) or die "can't get flags: $!"; fcntl ($rh, F_SETFL, $flags|O_NONBLOCK) or die "can't set O_NONBLOCK: +$!"; print $wh "ls foo\n"; my @files; my $ln; while (! ($ln = <$rh>)) {} ## wait for valid output do { $ln=~/.+sftp>\s*//; ## eat sftp> prompts chomp $ln; push @files, $ln; } while ($ln = <$rh>); for (@files) { my $qf = quotemeta; print $wh "get foo/$qf\n"; print $wh "mv foo/$qf bar/$qf\n"; } print $wh "bye\n"; waitpid $pid, 0;
Edited (twice) to ignore sftp> prompts.
In reply to Re: Short example using IPC::Open2 or IPC::Open3
by gamache
in thread Short example using IPC::Open2 or IPC::Open3
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |