G'day trmn8tr,

Welcome to the Monastery.

"use warnings; use strict; my $arg1 = $ARGV1; my $arg1 = $ARGV2; my $arg1 = $ARGV3; system('plink.exe -ssh me@myserver -pw password . ./scripts/myscript.sh $arg1 $arg2 $arg3');"

Beyond the issue with code tags that stevieb pointed out (i.e. all three [n] parts are rendered as links — described in "What shortcuts can I use for linking to other information?"), you have an additional problem which is probably caused by typing your code in by hand. What you've ended up with is three declarations for $arg1. Had you tried to run this code, it would have failed to compile and aborted something like this:

$ perl -E 'my $arg1 = $ARGV[1]; my $arg1 = $ARGV[2]; my $arg1 = $ARGV[ +3]; say $arg2; say $arg3' "my" variable $arg1 masks earlier declaration in same scope at -e line + 1. "my" variable $arg1 masks earlier declaration in same scope at -e line + 1. Global symbol "$arg2" requires explicit package name (did you forget t +o declare "my $arg2"?) at -e line 1. Global symbol "$arg3" requires explicit package name (did you forget t +o declare "my $arg3"?) at -e line 1. Execution of -e aborted due to compilation errors.

[Note: I have $PERL5OPT set to '-Mstrict -Mwarnings -Mautodie' (see perlrun)]

If you copy and paste your code, you'll avoid these sorts of typos; not to mention saving yourself a lot of unnecessary work.

Array indices are zero-based: the first element of @ARGV is $ARGV[0]. Did you really intend to set $arg1 to the 2nd argument ($ARGV[1]), $arg2 to the 3rd argument and $arg3 to the 4th argument? If so, these variables are extremely poorly named and your code is highly error-prone!

You might also consider using an array slice (e.g. @ARGV[0..2]). If you're unfamiliar with this construct, see "perlintro: Perl variable types".

See the system function documentation. Note that this function takes a list: so use one. There is no benefit in attempting to construct a string in which some parts aren't interpolated (e.g. @myserver) and some parts are (e.g. $arg1). You probably could have removed all three declarations and assignments and just written:

system qw{plink.exe -ssh me@myserver -pw password . ./scripts/myscript +.sh}, @ARGV[0..2];

That's obviously untested; this isn't:

$ perl -E 'system qw{ls -l}, @ARGV[0..2]' x y z -rw-r--r-- 1 ken staff 0 15 Sep 15:02 x -rw-r--r-- 1 ken staff 0 15 Sep 15:02 y -rw-r--r-- 1 ken staff 0 15 Sep 15:02 z

— Ken


In reply to Re: Using system to run exe on Windows and pass it variables by kcott
in thread Using system to run exe on Windows and pass it variables by trmn8tr

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.