CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
help with scp command


New User

Aug 19, 2011, 2:15 PM

Post #1 of 5 (2651 views)
help with scp command Can't Post


I need to modify an scp command that is executed in a perl script. The current scp command is:

`scp /broker/user/csp/myTestFile.txt pushme\`;

What I need to do is to add an date string to the file name. I wrote the following code:

$timeStamp = &TimeStamp;
$tranFileName = "myTestFile";

`scp /broker/user/csp/` .$tranFileName."_".$timeStamp.".txt". ` pushme\`;

sub TimeStamp {

my ($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$iddst) = localtime(time);
my $datestring;

$year += 1900;
$mon = insert0($mon);
$mday = insert0($mday);
$min = insert0($min);
$sec = insert0($sec);

$datestring = "$year$mon$mday";



sub insert0 {
my ($date) = shift;

if ($date < 10) {
return "0$date";

return $date;

I would like to file transmitted to be myFileName_20110819.txt. Can I break up the scp command like this? If not, how would it be written.



Veteran / Moderator

Aug 19, 2011, 4:38 PM

Post #2 of 5 (2643 views)
Re: [Mark1110] help with scp command [In reply to] Can't Post

First, add the warnings and strict pragmas and declare your vars, which is done by prefixing them with the 'my' keyword. And don't use CamelCase naming. Use an _ underscore to separate the words.

Next, get rid of those 2 subs. They are a very messy way of formatting the date string. Use the strftime function from the posix module.

Don't use the backticks operator in void context. The backticks are used to capture the output of an external command, which you're not doing. If you don't want to capture the output of the scp command, then use the list form of the system function or one of the IPC::Open modules.

use strict; 
use warnings;
use POSIX qw(strftime);

my $tran_filename = strftime("/broker/user/csp/myTestFile_%Y%m%d.txt", localtime);

my @cmd = qw($tran_filename;

system(@cmd) == 0 or die "system '@cmd' failed: $?;

In this case, I'd probably use the Net::SCP module instead of the system call.

(This post was edited by FishMonger on Aug 19, 2011, 4:43 PM)

Veteran / Moderator

Aug 20, 2011, 6:26 AM

Post #3 of 5 (2627 views)
Re: [FishMonger] help with scp command [In reply to] Can't Post

While going over some of my thread postings, I noticed that I left out an important part of the system call. I didn't specify the scp command, I just specified its arguments.

Here's the corrected cmd.

my @cmd = qw(scp $tran_filename;

New User

Aug 22, 2011, 9:39 AM

Post #4 of 5 (2583 views)
Re: [FishMonger] help with scp command [In reply to] Can't Post


I tried inserting your code into my perl module and I am getting an error message on the system(@cmd) == 0 or die "system '@cmd' failed: $? statement. The message I am getting is failed: -1. I then tried putting the scp command in front of the $tran_filename and I got failed: 256. Do you know what I am doing wrong?



New User

Aug 22, 2011, 10:42 AM

Post #5 of 5 (2581 views)
Re: [FishMonger] help with scp command [In reply to] Can't Post

What I did to get it to work was the following:

my @cmd = "scp ".$tran_filename." ".

What was happening was the qw function was deleting all the spaces so the @cmd was scp$

which was why I was getting an error. Thanks for all your help.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives