system("/usr/bin/ssh $server \"/usr/sbin/useradd $login\"");

Do you really use the single-argument form of system? This usually involves a shell, and forces you to guess what shell is invoked and what type of quoting it needs. Typically, you will get lots of problems as soon as you include white space, special characters, or variables. You have all of these.

Use the list form of system:

system('/usr/bin/ssh',$server,"/usr/sbin/useradd $login");

And I'm very sure that this call will fail with a "command not found" error. You don't want to invoke the program "/usr/sbin/useradd joe average user" when $login is "joe average user". You want to invoke the program "/usr/sbin/useradd" and pass whatever $login contains as first argument. ssh does not invoke the shell on the remote system.

So:

system('/usr/bin/ssh',$server,'/usr/sbin/useradd',$login);

What's the big difference? No quoting mess, all arguments end exactly where you want them, even if they contain usual characters.

Why?

Unix systems pass an array of arguments to each program (that's where C's argv argument to main() comes from, and perl copies that information into $^X, $0, and @ARGV). Only the shell splits the entered string into an array of arguments, typically on $ENV{'IFS'}. It also expands variables, * and ?, and several other constructs. The first element of the array is also taken as the name of the program to be started.

DOS and Windows, on the other hand, pass a single string to the program invoked. command.com trims the input and splits it at the first white space into executable name and argument string, expanding variables, but not * and ?. That is left to the invoked executable, and the executable also has to split the argument string into an array. The latter typically happens in the C runtime library and it depends on the implementation, * and ? are typically passed unmodified to the program code.

Perl tries to hide the evil differences between differrent operating systems from you, so even on Windows, the multiple-argument form of system() works on Windows, automatically quoting arguments where needed (or at least, it should do so). There are several special tricks for system() on different platforms, see http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlport.pod#system.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: getting multiple return values from system() by afoken
in thread getting multiple return values from system() by handarbyte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.