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

run perl script with cmd line in shell

by Anonymous Monk
on Apr 16, 2012 at 11:26 UTC ( [id://965291]=perlquestion: print w/replies, xml ) Need Help??

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

how to run a perl script in shell with cmd line paramters to perl I tried something like below that wont work out
#!usr/binn/csh ssh user@server perl < script.pl $cmdlinArgtoscrpt
Can anyone suggest a solution, I cant able to use packages here

Replies are listed 'Best First'.
Re: run perl script with cmd line in shell
by MidLifeXis (Monsignor) on Apr 16, 2012 at 12:41 UTC

    #!usr/binn/csh ssh user@server perl < script.pl $cmdlinArgtoscrpt
    It appears that you are asking to have a perl command run the local script script.pl on server with the parameters $cmdlinArgtoscrpt. Is this correct?

    Update: "Hello. My name is MidLifeXis, and I am a complexaholic." See Re^2: run perl script with cmd line in shell for a better solution.

    I see various solutions to this, with different difficulties:

    • Share the script to the remote machine and execute it from that shared location.
    • Copy the script.pl to the remote machine and run it from there. There are potential race conditions between the copy / execute and the execute / cleanup.
    • Embed the command line arguments into the top of your script.pl, perhaps as you are sending it across the wire to perl. This would require some pre-processing of the script and some assumptions of where you can embed the code - if I were to do it this way, I would probably have a token of some sort in the script that I would replace with my parameters.
    • Run the command remotely using the -e or -E flag to perl, and embedding the script on the remote command invocation. This would be my last choice for something that is any more complex than dead simple unless I could ensure that quotes were properly escaped, newlines handled, length of the command line accounted for, and so on.

    Update: I do not mean to imply that these are the only solutions -- there may well be something much more elegant, robust, and maintainable than any idea proposed above.

    --MidLifeXis

      Is there a reason why something like ssh user@server perl - $cmdlinArgtoscrpt < script.pl would not work?

      use Data::Dumper; print Dumper(\@ARGV); # testing: $ ssh someserver perl - aoeu snth < localscript.pl $VAR1 = [ 'aoeu', 'snth' ];

        Just wanted to add that due to the way ssh executes commands, you may need to shell-quote your command line arguments twice if they have _any_ odd characters:

        % ssh someserver perl - "foo bar" < localscript.pl $VAR1 = [ 'foo', 'bar' ]; % ssh someserver perl - "foo (bar)" < localscript.pl bash: -c: line 0: syntax error near unexpected token `(' bash: -c: line 0: `perl - foo (bar)' % ssh someserver perl - '"foo (bar)"' < localscript.pl $VAR1 = [ 'foo (bar)' ];
        -->>ssh user@server perl - $cmdlinArgtoscrpt < script.pl Worked well, thanks
      Hi MidLifeXis, Thanks.Correct Exactely.

      1)Share the script to the remote machine and execute it from that shared location.

      --> I can't do that. may be the script given as a patch,that can't put into server that is dynamic

      2)Copy the script.pl to the remote machine and run it from there. There are potential race conditions between the copy / execute and the execute /

      --> Same above said reason, i cant do that

      3)Embed the command line arguments into the top of your script.pl, perhaps as you are sending it across the wire to perl. This would require some pre-processing of the script and some assumptions of where you can embed the code - if I were to do it this way, I would probably have a token of some sort in the script that I would replace with my parameters

      --> The cmd line argumetns are also dynamic in nature..but i dont understand this entire statment correctely. can you give an example ?

      4)Run the command remotely using the -e or -E flag to perl, and embedding the script on the remote command invocation. This would be my last choice for something that is any more complex than dead simple unless I could ensure that quotes were properly escaped, newlines handled, length of the command line accounted for, and so on. --> I think this will work out may be..i will give it a try and also i am trying with sed cmd as well

        3)Embed the command line arguments into the top of your script.pl, perhaps as you are sending it across the wire to perl. This would require some pre-processing of the script and some assumptions of where you can embed the code - if I were to do it this way, I would probably have a token of some sort in the script that I would replace with my parameters --> The cmd line argumetns are also dynamic in nature..but i dont understand this entire statment correctely. can you give an example ?

        In your script you could have something like:

        # Earlier stuff in the script... # Fill in the arguments if running remotely @ARGV ||= qw(%%REPLACEDTOKEN%%); # ... and back it out if @ARGV should really be empty @ARGV = () if $ARGV[0] eq "\%\%REPLACEDTOKEN\%\%"; # Rest of your script
        At this point, you would use something locally to replace '%%REPLACEDTOKEN%%' with your parameters, and pipe that to a perl call on the remote machine.

        sed -e .... < script.pl | ssh user@remote perl

        I still have concerns that this is the most robust solution to your problem.

        Do your scripts use any temporary files? If so, I would seriously reconsider your answer to number 2. If you can write temp files, you can also write a temporary perl script.

        --MidLifeXis

Re: run perl script with cmd line in shell
by i5513 (Pilgrim) on Apr 16, 2012 at 22:43 UTC

    Hello,

    Take a look at Net::OpenSSH or other cpan ssh modules.

    It is not exactly what do you want to do (perl will running in this case in your computer and not in remote), but I hope it helps.

    It can be useful in many environments, like:

  • You don't have the same perl version in all your hosts
  • You want to parse some remote file or some remote command output
  • You want to write remote files with some info
  • See this example which is like a first attemp to solve your problem (cat script.pl | perl thisexample user@host parameters):

    #!/usr/bin/perl -w use strict; use Net::OpenSSH; my $ssh = Net::OpenSSH->new($ARGV[0]); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; shift; my (@args)=@_; shift while (@ARGV!=0); my $command=""; while (<>) { $command.=$_; } $ssh->system("perl - <<EOS @args $command; EOS ");

    For solve this specific issue, I prefer the response that Anonymous monk give you before (Re^2: run perl script with cmd line in shell), it is clearest.

    Regards,
Re: run perl script with cmd line in shell
by Sandy (Curate) on Apr 16, 2012 at 12:28 UTC
    I am truly confused as what it is that you are asking.

    Are you asking to pass parameters which are outputs from a perl script to another perl script?

    Use the unix backticks to execute your perl program on the command line first...

     perl -e 'print "@ARGV\n"' `perl -e '@x=(1..5);print "@x\n"'`

    or, pipe the output from one perl script into stdin of 2nd perl script

    perl -e '@x=(1..5);print "@x"' | perl -e 'while (<>) {print};print"\n +"'
    Like I said, I am unsure if this is what you are asking.

    Sandy

Re: run perl script with cmd line in shell
by JavaFan (Canon) on Apr 16, 2012 at 14:28 UTC
    If you want to execute perl script.pl (with some additional parameters) on the remove server, you have to get script.pl there (just as you have to get perl there, if it isn't there already). When it's there, you can do:
    $ ssh user@server "perl /path/to/script.pl $cmdlinArgtoscrpt"
    Where I assume that $cmdlinArgtoscrpt contains the arguments to the script.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found