Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

system call and newline interpretattion.

by jaimeGonz (Initiate)
on May 24, 2005 at 14:27 UTC ( #459988=perlquestion: print w/replies, xml ) Need Help??

jaimeGonz has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I need your esteemed wisdom.
I am trying to run an scp command on a ermote system which I am pretty sure is being split at the whitespace between the 2 arguments. I have tried every trick in my library to no avail. How do I avoid the command being split into 2 lines? The code is below:
$command = "scp $user\@$ip:$source/$_ $staging/$_";<br> print("$command\n");<br> system($command);<br>
and the output:
scp daylite_backup@192.168.0.53:/home/staging/dayliteLeads/courseBooki +ngs/1116932421807.xml <br> /dayliteImports/staging_courses/1116932421807.xml <br> usage: scp [-pqrvBC1246] [-F config] [-S program] [-P port]<br> [-c cipher] [-i identity] [-l limit] [-o option]<br> [[user@]host1:]file1 [...] [[user@]host2:]file2<br> sh: line 2: /dayliteImports/staging_courses/1116932421807.xml: No such + file or directory

Janitored by holli - added code tags

Replies are listed 'Best First'.
Re: system call and newline interpretattion.
by Fletch (Bishop) on May 24, 2005 at 14:35 UTC

    I'd check that $_ doesn't already have a newline on the end (see perldoc -f chomp).

Re: system call and newline interpretattion.
by davidrw (Prior) on May 24, 2005 at 14:50 UTC
    I think the consensus is clearly that $_ has a newline on it, but just wanted to add one other note -- system() can take the program and argument list as well:
    chomp; my @args = ("$user\@$ip:$source/$_", "staging/$_"); my $command = "scp"; system $command, @args;
    This still requires the chomp, but might make the code more legible or easier to debug and I believe (but not sure) that in some cases it helps with escaping issues.
      As a "bonus", using the system $command, @args form of system is safer since the arguments are not subject to interpretation by the shell.
Re: system call and newline interpretattion.
by davidrw (Prior) on May 24, 2005 at 14:52 UTC
    One other comment: Net::SCP might be an alternative for you.
Re: system call and newline interpretattion.
by robartes (Priest) on May 24, 2005 at 14:35 UTC

    Quite possibly your $_ comes from reading in a line in a file and contains a newline. Try chomp before you use $_.

    Then again, as you do not provide context, the problem might be a completely different one.

    CU
    Robartes-

Re: system call and newline interpretattion.
by dave_the_m (Monsignor) on May 24, 2005 at 14:37 UTC
    Looks like the filename in $_ has a newline in it. Try
    chomp
    to remove it first.

    Dave.

Re: system call and newline interpretattion.
by ikegami (Patriarch) on May 24, 2005 at 14:37 UTC

    Looks like like $_ ends with a newline. Try adding chomp before $command = ....

    Unrelated, try using <code> tags around your code and output when you post.

Re: system call and newline interpretattion.
by tlm (Prior) on May 24, 2005 at 14:37 UTC

    How are you getting $_ ? Are you sure it doesn't end with a newline?

    the lowliest monk

Re: system call and newline interpretattion.
by Fang (Pilgrim) on May 24, 2005 at 14:42 UTC

    Two thoughts:

    • check all the variables you're using ($user, $ip, $_, etc.) are correctly chomped, especially if some of them are coming from user input, which you didn't mention. From the ouput you provided, it looks to me like $_ has a trailing newline.
    • use the list syntax of the system command, i.e. system($command, $arg1, $arg2), it's always been considered better (as in safer) from what I've read.

    Update: ah, well, 5 replies and problem solved by the time I submitted this... nevermind me.

Re: system call and newline interpretattion.
by jaimeGonz (Initiate) on May 24, 2005 at 14:42 UTC
    Arrggghhh! That's the sound of me flattening my forehead with the palm of my hand. That's done it. I'd taken a listing from a ls command.

    chmoped away ... thank you all very much!!!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://459988]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2022-10-01 01:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?