develooper Front page | perl.perl6.users | Postings from September 2017

A pm6 to share: RunNoShell

September 9, 2017 07:38
A pm6 to share: RunNoShell
Message ID:
Hi All,

I have been using the heck out of this pm6, so
I though other wold like it too.

Here is the most miserable code I have run at it so far
(this guy has to be run from his own directory):

    my $CimTrakVersion;
    my $ReturnCode;
    ( $CimTrakVersion, $ReturnCode ) = RunNoShell(
        'bash -c "cd /opt/Cimcor/CimTrak/CimTrakServer; 
./CimTrakServer.exe --version 2>&1 | grep -i Build"' );

#!/usr/bin/env perl6
# RunNoShell.pm6

sub RunNoShell ( $RunString ) is export {

    #`{ run a system command without a shell.
        Enter the command as a sting.  Use quotes around items that
        are meant to be grouped together ("Program Files", etc.)

        Results are returned as a dual pair:
            a string with the results of the command, and
            the return code (note: bash 0=success)

    # place each value into a cell in an array.  Keep quoted
    # values together
    my @RunArray  = $RunString ~~ m:global/ [ '"' <-[ " ]> * '"' | \S+ ] /;

    # shells remove the quote, so you have to here as well
    for @RunArray.kv -> $index, $value { @RunArray[$index] ~~ s:g/\"//; };
    # for @RunArray.kv -> $index, $value { say "\$RunArray[$index] = 
<$value>"; }; print "\n";

    my $proc      = run( @RunArray, :out );
    my $ReturnStr = $proc.out.slurp-rest;
    my $RtnCode   = $proc.status;

    return ( $ReturnStr, $RtnCode );
</code> Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About