Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Using system to run exe on Windows and pass it variables

by kcott (Archbishop)
on Sep 15, 2016 at 06:22 UTC ( [id://1171815]=note: print w/replies, xml ) Need Help??


in reply to Using system to run exe on Windows and pass it variables

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

Replies are listed 'Best First'.
Re^2: Using system to run exe on Windows and pass it variables
by trmn8tr (Initiate) on Sep 15, 2016 at 19:56 UTC
    Putting this into an array slice seems to have worked! Here is my new code I'm using to call out specific variables:
    use warnings; use strict; system qw{D:\\SiteScope\\tools\\plink.exe -ssh user@myserver -pw Passw +ord . ./scripts/myscript.sh}, @ARGV[1,6,7];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found