How to execute a command line in Perlscript

Discussion in 'Perl Misc' started by Eric SALGON, Feb 5, 2004.

  1. Eric SALGON

    Eric SALGON Guest

    Hi,

    Is there someone who knows how to execute a command line (such a simple echo
    cmd) in PerlScript.

    I hve tryed '' or system() but the result is the same: no result but no error
    !

    This syntax works fine in a perl program but not in PerlScript

    An idea ?

    Eric
     
    Eric SALGON, Feb 5, 2004
    #1
    1. Advertising

  2. Eric SALGON

    Chris Guest

    Eric SALGON wrote:
    > Hi,
    >
    > Is there someone who knows how to execute a command line (such a simple echo
    > cmd) in PerlScript.
    >
    > I hve tryed '' or system() but the result is the same: no result but no error
    > !
    >
    > This syntax works fine in a perl program but not in PerlScript
    >
    > An idea ?


    Well, to answer your question successfully, I'll have to make some
    assumptions. Some people, when they say "PerlScript," they really mean
    "Perl script." But since there IS a product called "PerlScript," I'll
    answer in that context. And even within THAT, there are two ways this
    could be viewed: PerlScript under ASP and PerlScript under Windows
    Scripting Host. I'll assume the later, but answer both.

    PerlScript ASP cannot use 'print' or 'echo' to write to the end client
    browser. You have to use $Response->Write(). Assuming you know that
    already...

    ....the answer when running PerlScript in a WSH context is:

    $WScript->Echo( "This is how to print in PerlScript\n" );

    A tip: I personally like Ruby's use of a call named puts() which, named
    in C-style convention, out Puts a string. I've adopted THAT as my
    standard "outputing call" and I write wrappers in each environment I'm
    in to handle it the right way (so I don't have to remember any more how
    to write a string; it gets ridiculously out of hand esp. in the Windows
    world where Microsoft can't get it together and each side of their house
    invents a new standard for something so simple -- in ASP it's
    Response.Write in WSH it's WScript.Echo, in Access it's print.debug or
    whatever...)

    So... I recommend this approach and then you never have to remember any
    more:

    ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-vermen-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Feb 5, 2004
    #2
    1. Advertising

  3. Eric SALGON

    Chris Guest

    Eric SALGON wrote:
    > Hi,
    >
    > Is there someone who knows how to execute a command line (such a simple echo
    > cmd) in PerlScript.
    >
    > I hve tryed '' or system() but the result is the same: no result but no error
    > !
    >
    > This syntax works fine in a perl program but not in PerlScript
    >
    > An idea ?
    >


    I just realized I didn't answer your question in my last response.

    First of all, as a general rule, and one that is outlined in the FAQ
    (and asked about a lot here is) system() does NOT provide output. But
    most especially in the Windows world where Microsoft writes their own
    rules, this is also not the case in either ASP or WSH (still assuming
    you are talking about "PerlScript" and not "Perl script.")

    Using output wrappers in the ASP and WSH worlds for outputing a string,
    the following code will work in both worlds for outputing a "system"
    command:

    sub shell {

    my @output;
    for (@_) { push( @output, `$_` ) }
    chomp( @output );
    return wantarray ? @output : \@output;

    }

    puts( shell( 'dir /l/on/' ) );

    or you can:

    puts( shell(
    'dir',
    'type c:\\autoexec.bat',
    'dir c:\\winnt\\system32',
    'command4',
    'command5',
    ));

    If:

    ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

    Incidentially using PerlScript in Windows allows you this convenience.
    The "official" Microsoft "interface" (read "complicated and
    convolluted") for executing a command in a scripting environment (both
    ASP and WSH) is WshShell.Exec() (which takes two calls to set up.) You
    can do this in PerlScript if you want, but the regular Perl way of
    executing a system command works much smoother.

    HTH,
    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-vermen-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Feb 5, 2004
    #3
  4. Eric SALGON

    Eric SALGON Guest

    Thx Chris for your answer,

    My question was for "PerlScript" under ASP, and the 'Echo' command was only a
    sample.

    In fact, my goal is to run the 'dnscmd' command (which is used to manage dns
    on w2000/w2003 server) and analyse the result.

    Eric


    In article <u1vUb.19065$>,
    says...
    >
    >Eric SALGON wrote:
    >> Hi,
    >>
    >> Is there someone who knows how to execute a command line (such a simple

    echo
    >> cmd) in PerlScript.
    >>
    >> I hve tryed '' or system() but the result is the same: no result but no

    error
    >> !
    >>
    >> This syntax works fine in a perl program but not in PerlScript
    >>
    >> An idea ?

    >
    >Well, to answer your question successfully, I'll have to make some
    >assumptions. Some people, when they say "PerlScript," they really mean
    >"Perl script." But since there IS a product called "PerlScript," I'll
    >answer in that context. And even within THAT, there are two ways this
    >could be viewed: PerlScript under ASP and PerlScript under Windows
    >Scripting Host. I'll assume the later, but answer both.
    >
    >PerlScript ASP cannot use 'print' or 'echo' to write to the end client
    >browser. You have to use $Response->Write(). Assuming you know that
    >already...
    >
    >...the answer when running PerlScript in a WSH context is:
    >
    >$WScript->Echo( "This is how to print in PerlScript\n" );
    >
    >A tip: I personally like Ruby's use of a call named puts() which, named
    >in C-style convention, out Puts a string. I've adopted THAT as my
    >standard "outputing call" and I write wrappers in each environment I'm
    >in to handle it the right way (so I don't have to remember any more how
    >to write a string; it gets ridiculously out of hand esp. in the Windows
    >world where Microsoft can't get it together and each side of their house
    >invents a new standard for something so simple -- in ASP it's
    >Response.Write in WSH it's WScript.Echo, in Access it's print.debug or
    >whatever...)
    >
    >So... I recommend this approach and then you never have to remember any
    >more:
    >
    >ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
    >
    >Chris
    >-----
    >Chris Olive
    >chris -at- --spammers-are-vermen-- technologEase -dot- com
    >http://www.technologEase.com
    >(pronounced "technologies")
    >
     
    Eric SALGON, Feb 6, 2004
    #4
  5. Eric SALGON

    Eric SALGON Guest

    Unfortunatly, I have implemented "shell" and "puts" functions in my ASP code
    but `$_` return no result in @output array with a simple 'dir' command.

    I'm afraid it's a problem of right on my IIS server.

    Eric

    In article <elvUb.19067$>,
    says...
    >
    >Eric SALGON wrote:
    >> Hi,
    >>
    >> Is there someone who knows how to execute a command line (such a simple

    echo
    >> cmd) in PerlScript.
    >>
    >> I hve tryed '' or system() but the result is the same: no result but no

    error
    >> !
    >>
    >> This syntax works fine in a perl program but not in PerlScript
    >>
    >> An idea ?
    >>

    >
    >I just realized I didn't answer your question in my last response.
    >
    >First of all, as a general rule, and one that is outlined in the FAQ
    >(and asked about a lot here is) system() does NOT provide output. But
    >most especially in the Windows world where Microsoft writes their own
    >rules, this is also not the case in either ASP or WSH (still assuming
    >you are talking about "PerlScript" and not "Perl script.")
    >
    >Using output wrappers in the ASP and WSH worlds for outputing a string,
    >the following code will work in both worlds for outputing a "system"
    >command:
    >
    >sub shell {
    >
    > my @output;
    > for (@_) { push( @output, `$_` ) }
    > chomp( @output );
    > return wantarray ? @output : \@output;
    >
    >}
    >
    >puts( shell( 'dir /l/on/' ) );
    >
    >or you can:
    >
    >puts( shell(
    > 'dir',
    > 'type c:\\autoexec.bat',
    > 'dir c:\\winnt\\system32',
    > 'command4',
    > 'command5',
    >));
    >
    >If:
    >
    >ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
    >
    >Incidentially using PerlScript in Windows allows you this convenience.
    >The "official" Microsoft "interface" (read "complicated and
    >convolluted") for executing a command in a scripting environment (both
    >ASP and WSH) is WshShell.Exec() (which takes two calls to set up.) You
    >can do this in PerlScript if you want, but the regular Perl way of
    >executing a system command works much smoother.
    >
    >HTH,
    >Chris
    >-----
    >Chris Olive
    >chris -at- --spammers-are-vermen-- technologEase -dot- com
    >http://www.technologEase.com
    >(pronounced "technologies")
    >
    >
     
    Eric SALGON, Feb 6, 2004
    #5
  6. Eric SALGON

    Chris Guest

    Eric SALGON wrote:
    > In article <u1vUb.19065$>,
    > says...
    >
    >>Eric SALGON wrote:
    >>
    >>>Hi,
    >>>
    >>>Is there someone who knows how to execute a command line (such a simple

    >
    > echo
    >
    >>>cmd) in PerlScript.
    >>>
    >>>I hve tryed '' or system() but the result is the same: no result but no

    >
    > error
    >
    >>>!
    >>>
    >>>This syntax works fine in a perl program but not in PerlScript
    >>>
    >>>An idea ?

    >>
    >>Well, to answer your question successfully, I'll have to make some
    >>assumptions. Some people, when they say "PerlScript," they really mean
    >>"Perl script." But since there IS a product called "PerlScript," I'll
    >>answer in that context. And even within THAT, there are two ways this
    >>could be viewed: PerlScript under ASP and PerlScript under Windows
    >>Scripting Host. I'll assume the later, but answer both.
    >>
    >>PerlScript ASP cannot use 'print' or 'echo' to write to the end client
    >>browser. You have to use $Response->Write(). Assuming you know that
    >>already...
    >>
    >>...the answer when running PerlScript in a WSH context is:
    >>
    >>$WScript->Echo( "This is how to print in PerlScript\n" );
    >>
    >>A tip: I personally like Ruby's use of a call named puts() which, named
    >>in C-style convention, out Puts a string. I've adopted THAT as my
    >>standard "outputing call" and I write wrappers in each environment I'm
    >>in to handle it the right way (so I don't have to remember any more how
    >>to write a string; it gets ridiculously out of hand esp. in the Windows
    >>world where Microsoft can't get it together and each side of their house
    >>invents a new standard for something so simple -- in ASP it's
    >>Response.Write in WSH it's WScript.Echo, in Access it's print.debug or
    >>whatever...)
    >>
    >>So... I recommend this approach and then you never have to remember any
    >>more:
    >>
    >>ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >>WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

    >


    >> [Top Posting Fixed]


    > Thx Chris for your answer,
    >
    > My question was for "PerlScript" under ASP, and the 'Echo' command was only a
    > sample.
    >
    > In fact, my goal is to run the 'dnscmd' command (which is used to manage dns
    > on w2000/w2003 server) and analyse the result.
    >


    Well, hopefully you read my other answer where I was reading and
    comprehending instead of reading and jumping the gun...

    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-vermin-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Feb 6, 2004
    #6
  7. [TOFU snipped]

    Eric SALGON wrote:
    > Unfortunatly, I have implemented "shell" and "puts" functions in my
    > ASP code but `$_` return no result in @output array with a simple
    > 'dir' command.
    >
    > I'm afraid it's a problem of right on my IIS server.


    But PerlScript is executed client side.
    The web server (be it IIS or Apache or whatever) has nothing to do with it.

    jue
     
    Jürgen Exner, Feb 7, 2004
    #7
  8. Eric SALGON

    ko Guest

    Jürgen Exner wrote:
    > [TOFU snipped]
    >
    > Eric SALGON wrote:
    >
    >>Unfortunatly, I have implemented "shell" and "puts" functions in my
    >>ASP code but `$_` return no result in @output array with a simple
    >>'dir' command.
    >>
    >>I'm afraid it's a problem of right on my IIS server.

    >
    >
    > But PerlScript is executed client side.
    > The web server (be it IIS or Apache or whatever) has nothing to do with it.
    >
    > jue


    Actually, ActiveState Perl allows you to do something like this with
    PerlScript and IIS:

    <% $Response->write( $_ . '<br>') foreach qx(dir) %>

    To the OP - have you looked at the HTML documentation that comes with AS
    Perl? The IIS/PerlScript stuff is pretty much right under the Table of
    Contents...

    keith
     
    ko, Feb 7, 2004
    #8
  9. Eric SALGON

    Chris Guest

    Jürgen Exner wrote:
    > [TOFU snipped]
    >
    > Eric SALGON wrote:
    >
    >>Unfortunatly, I have implemented "shell" and "puts" functions in my
    >>ASP code but `$_` return no result in @output array with a simple
    >>'dir' command.
    >>
    >>I'm afraid it's a problem of right on my IIS server.

    >
    >
    > But PerlScript is executed client side.
    > The web server (be it IIS or Apache or whatever) has nothing to do with it.
    >
    > jue


    Jurgen...! We've gone through this before once a year or so ago. :cool:
    PERLSCRIPT IS NOT JUST CLIENT-SIDE!!!! In fact, it's run FAR more often
    SERVER-SIDE than it is CLIENT-SIDE. Nobody uses it client-side. It's
    useless. If you install ActiveState Perl on a server running IIS, it
    gives you a chance to install the PerlScript ASP engine that runs JUST
    LIKE VBScript and JScript on an IIS server... :cool: (It also allows you
    to use it as a WSH language as well.)

    I say all that as kindly as possible since it's so easy to misconstrue
    things in print. But you keep saying that PerlScript is "client-side",
    which is CAN BE, but it's FAR more prevalent (and far more useful)
    server-side.

    So the question and my answer has EVERYTHING to do with IIS...

    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-vermin-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Feb 7, 2004
    #9
  10. Eric SALGON

    Chris Guest

    Eric SALGON wrote:
    > In article <elvUb.19067$>,
    > says...
    >
    >>Eric SALGON wrote:
    >>
    >>>Hi,
    >>>
    >>>Is there someone who knows how to execute a command line (such a simple

    >
    > echo
    >
    >>>cmd) in PerlScript.
    >>>
    >>>I hve tryed '' or system() but the result is the same: no result but no

    >
    > error
    >
    >>>!
    >>>
    >>>This syntax works fine in a perl program but not in PerlScript
    >>>
    >>>An idea ?
    >>>

    >>
    >>I just realized I didn't answer your question in my last response.
    >>
    >>First of all, as a general rule, and one that is outlined in the FAQ
    >>(and asked about a lot here is) system() does NOT provide output. But
    >>most especially in the Windows world where Microsoft writes their own
    >>rules, this is also not the case in either ASP or WSH (still assuming
    >>you are talking about "PerlScript" and not "Perl script.")
    >>
    >>Using output wrappers in the ASP and WSH worlds for outputing a string,
    >>the following code will work in both worlds for outputing a "system"
    >>command:
    >>
    >>sub shell {
    >>
    >> my @output;
    >> for (@_) { push( @output, `$_` ) }
    >> chomp( @output );
    >> return wantarray ? @output : \@output;
    >>
    >>}
    >>
    >>puts( shell( 'dir /l/on/' ) );
    >>
    >>or you can:
    >>
    >>puts( shell(
    >> 'dir',
    >> 'type c:\\autoexec.bat',
    >> 'dir c:\\winnt\\system32',
    >> 'command4',
    >> 'command5',
    >>));
    >>
    >>If:
    >>
    >>ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >>WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
    >>
    >>Incidentially using PerlScript in Windows allows you this convenience.
    >>The "official" Microsoft "interface" (read "complicated and
    >>convolluted") for executing a command in a scripting environment (both
    >>ASP and WSH) is WshShell.Exec() (which takes two calls to set up.) You
    >>can do this in PerlScript if you want, but the regular Perl way of
    >>executing a system command works much smoother.
    >>

    > [Top Posting Fixed]
    >
    > Unfortunatly, I have implemented "shell" and "puts" functions in my ASP code
    > but `$_` return no result in @output array with a simple 'dir' command.
    >
    > I'm afraid it's a problem of right on my IIS server.
    >


    If your tick marks above are an accurate portrayal of what you used in
    your shell() code, then, no you will not get output. (You would only
    get an echo back of the command(s) you wanted to run that way.) The
    single tick marks in my shell() routine above are backward ticks. Use
    qx() if this is confusing:

    sub shell {

    my @output;
    for (@_) { push( @output, qx($_) } }
    chomp( @output );
    return wantarray ? @output : \@output;

    }

    I would be very surprised that you can't execute 'dir' under IIS. The
    username running IIS -- usually the default IUSR_machinename account --
    should have enough privilege to do a 'dir' (and under most circumstances
    will give you a directory listing of C:\WINNT\SYSTEM32).

    I'm wondering if it's not just a matter of the backwards tick, which the
    function written using qx() above should solve.

    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-vermin-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Feb 7, 2004
    #10
  11. Eric SALGON

    Chris Guest

    Chris wrote:
    > Eric SALGON wrote:
    >
    >> In article <elvUb.19067$>,
    >> says...
    >>
    >>> Eric SALGON wrote:
    >>>
    >>>> Hi,
    >>>>
    >>>> Is there someone who knows how to execute a command line (such a simple

    >>
    >>
    >> echo
    >>
    >>>> cmd) in PerlScript.
    >>>>
    >>>> I hve tryed '' or system() but the result is the same: no result but no

    >>
    >>
    >> error
    >>
    >>>> !
    >>>>
    >>>> This syntax works fine in a perl program but not in PerlScript
    >>>>
    >>>> An idea ?
    >>>>
    >>>
    >>> I just realized I didn't answer your question in my last response.
    >>>
    >>> First of all, as a general rule, and one that is outlined in the FAQ
    >>> (and asked about a lot here is) system() does NOT provide output.
    >>> But most especially in the Windows world where Microsoft writes their
    >>> own rules, this is also not the case in either ASP or WSH (still
    >>> assuming you are talking about "PerlScript" and not "Perl script.")
    >>>
    >>> Using output wrappers in the ASP and WSH worlds for outputing a
    >>> string, the following code will work in both worlds for outputing a
    >>> "system" command:
    >>>
    >>> sub shell {
    >>>
    >>> my @output;
    >>> for (@_) { push( @output, `$_` ) }
    >>> chomp( @output );
    >>> return wantarray ? @output : \@output;
    >>>
    >>> }
    >>>
    >>> puts( shell( 'dir /l/on/' ) );
    >>>
    >>> or you can:
    >>>
    >>> puts( shell(
    >>> 'dir',
    >>> 'type c:\\autoexec.bat',
    >>> 'dir c:\\winnt\\system32',
    >>> 'command4',
    >>> 'command5',
    >>> ));
    >>>
    >>> If:
    >>>
    >>> ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >>> WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
    >>>
    >>> Incidentially using PerlScript in Windows allows you this
    >>> convenience. The "official" Microsoft "interface" (read "complicated
    >>> and convolluted") for executing a command in a scripting environment
    >>> (both ASP and WSH) is WshShell.Exec() (which takes two calls to set
    >>> up.) You can do this in PerlScript if you want, but the regular Perl
    >>> way of executing a system command works much smoother.
    >>>

    > > [Top Posting Fixed]
    > >

    >
    >> Unfortunatly, I have implemented "shell" and "puts" functions in my
    >> ASP code but `$_` return no result in @output array with a simple
    >> 'dir' command.
    >>
    >> I'm afraid it's a problem of right on my IIS server.
    >>

    >
    > If your tick marks above are an accurate portrayal of what you used in
    > your shell() code, then, no you will not get output. (You would only
    > get an echo back of the command(s) you wanted to run that way.) The
    > single tick marks in my shell() routine above are backward ticks. Use
    > qx() if this is confusing:
    >
    > sub shell {
    >
    > my @output;
    > for (@_) { push( @output, qx($_) } }
    > chomp( @output );
    > return wantarray ? @output : \@output;
    >
    > }
    >


    The extra "}" at the end of the line "for (@_) { push..." in the shell()
    routine should be removed. Should have been:

    for (@_) { push( @output, qx($_) ) }

    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-vermin-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")

    "Uh? Yes... well... You don't know *everything*..."
    --Puddleglum, _The Silver Chair_
     
    Chris, Feb 7, 2004
    #11
  12. Eric SALGON

    Eric SALGON Guest

    Chris,

    It was not a problem of backward ticks, no chance.
    The result is unfortunatly the same ...

    another idea ?

    Thx

    Eric

    In article <A4bVb.19477$>,
    says...
    >
    >Chris wrote:
    >> Eric SALGON wrote:
    >>
    >>> In article <elvUb.19067$>,
    >>> says...
    >>>
    >>>> Eric SALGON wrote:
    >>>>
    >>>>> Hi,
    >>>>>
    >>>>> Is there someone who knows how to execute a command line (such a simple
    >>>
    >>>
    >>> echo
    >>>
    >>>>> cmd) in PerlScript.
    >>>>>
    >>>>> I hve tryed '' or system() but the result is the same: no result but no
    >>>
    >>>
    >>> error
    >>>
    >>>>> !
    >>>>>
    >>>>> This syntax works fine in a perl program but not in PerlScript
    >>>>>
    >>>>> An idea ?
    >>>>>
    >>>>
    >>>> I just realized I didn't answer your question in my last response.
    >>>>
    >>>> First of all, as a general rule, and one that is outlined in the FAQ
    >>>> (and asked about a lot here is) system() does NOT provide output.
    >>>> But most especially in the Windows world where Microsoft writes their
    >>>> own rules, this is also not the case in either ASP or WSH (still
    >>>> assuming you are talking about "PerlScript" and not "Perl script.")
    >>>>
    >>>> Using output wrappers in the ASP and WSH worlds for outputing a
    >>>> string, the following code will work in both worlds for outputing a
    >>>> "system" command:
    >>>>
    >>>> sub shell {
    >>>>
    >>>> my @output;
    >>>> for (@_) { push( @output, `$_` ) }
    >>>> chomp( @output );
    >>>> return wantarray ? @output : \@output;
    >>>>
    >>>> }
    >>>>
    >>>> puts( shell( 'dir /l/on/' ) );
    >>>>
    >>>> or you can:
    >>>>
    >>>> puts( shell(
    >>>> 'dir',
    >>>> 'type c:\\autoexec.bat',
    >>>> 'dir c:\\winnt\\system32',
    >>>> 'command4',
    >>>> 'command5',
    >>>> ));
    >>>>
    >>>> If:
    >>>>
    >>>> ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >>>> WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
    >>>>
    >>>> Incidentially using PerlScript in Windows allows you this
    >>>> convenience. The "official" Microsoft "interface" (read "complicated
    >>>> and convolluted") for executing a command in a scripting environment
    >>>> (both ASP and WSH) is WshShell.Exec() (which takes two calls to set
    >>>> up.) You can do this in PerlScript if you want, but the regular Perl
    >>>> way of executing a system command works much smoother.
    >>>>
    >> > [Top Posting Fixed]
    >> >

    >>
    >>> Unfortunatly, I have implemented "shell" and "puts" functions in my
    >>> ASP code but `$_` return no result in @output array with a simple
    >>> 'dir' command.
    >>>
    >>> I'm afraid it's a problem of right on my IIS server.
    >>>

    >>
    >> If your tick marks above are an accurate portrayal of what you used in
    >> your shell() code, then, no you will not get output. (You would only
    >> get an echo back of the command(s) you wanted to run that way.) The
    >> single tick marks in my shell() routine above are backward ticks. Use
    >> qx() if this is confusing:
    >>
    >> sub shell {
    >>
    >> my @output;
    >> for (@_) { push( @output, qx($_) } }
    >> chomp( @output );
    >> return wantarray ? @output : \@output;
    >>
    >> }
    >>

    >
    >The extra "}" at the end of the line "for (@_) { push..." in the shell()
    >routine should be removed. Should have been:
    >
    >for (@_) { push( @output, qx($_) ) }
    >
    >Chris
    >-----
    >Chris Olive
    >chris -at- --spammers-are-vermin-- technologEase -dot- com
    >http://www.technologEase.com
    >(pronounced "technologies")
    >
    >"Uh? Yes... well... You don't know *everything*..."
    >--Puddleglum, _The Silver Chair_
     
    Eric SALGON, Feb 10, 2004
    #12
  13. Eric SALGON

    Chris Guest

    Eric SALGON wrote:
    > In article <A4bVb.19477$>,
    > says...
    >
    >>Chris wrote:
    >>
    >>>Eric SALGON wrote:
    >>>
    >>>
    >>>>In article <elvUb.19067$>,
    >>>> says...
    >>>>
    >>>>
    >>>>>Eric SALGON wrote:
    >>>>>
    >>>>>
    >>>>>>Hi,
    >>>>>>
    >>>>>>Is there someone who knows how to execute a command line (such a simple
    >>>>
    >>>>
    >>>>echo
    >>>>
    >>>>
    >>>>>>cmd) in PerlScript.
    >>>>>>
    >>>>>>I hve tryed '' or system() but the result is the same: no result but no
    >>>>
    >>>>
    >>>>error
    >>>>
    >>>>
    >>>>>>!
    >>>>>>
    >>>>>>This syntax works fine in a perl program but not in PerlScript
    >>>>>>
    >>>>>>An idea ?
    >>>>>>
    >>>>>
    >>>>>I just realized I didn't answer your question in my last response.
    >>>>>
    >>>>>First of all, as a general rule, and one that is outlined in the FAQ
    >>>>>(and asked about a lot here is) system() does NOT provide output.
    >>>>>But most especially in the Windows world where Microsoft writes their
    >>>>>own rules, this is also not the case in either ASP or WSH (still
    >>>>>assuming you are talking about "PerlScript" and not "Perl script.")
    >>>>>
    >>>>>Using output wrappers in the ASP and WSH worlds for outputing a
    >>>>>string, the following code will work in both worlds for outputing a
    >>>>>"system" command:
    >>>>>
    >>>>>sub shell {
    >>>>>
    >>>>> my @output;
    >>>>> for (@_) { push( @output, `$_` ) }
    >>>>> chomp( @output );
    >>>>> return wantarray ? @output : \@output;
    >>>>>
    >>>>>}
    >>>>>
    >>>>>puts( shell( 'dir /l/on/' ) );
    >>>>>
    >>>>>or you can:
    >>>>>
    >>>>>puts( shell(
    >>>>> 'dir',
    >>>>> 'type c:\\autoexec.bat',
    >>>>> 'dir c:\\winnt\\system32',
    >>>>> 'command4',
    >>>>> 'command5',
    >>>>>));
    >>>>>
    >>>>>If:
    >>>>>
    >>>>>ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
    >>>>>WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
    >>>>>
    >>>>>Incidentially using PerlScript in Windows allows you this
    >>>>>convenience. The "official" Microsoft "interface" (read "complicated
    >>>>>and convolluted") for executing a command in a scripting environment
    >>>>>(both ASP and WSH) is WshShell.Exec() (which takes two calls to set
    >>>>>up.) You can do this in PerlScript if you want, but the regular Perl
    >>>>>way of executing a system command works much smoother.
    >>>>>
    >>>
    >>> > [Top Posting Fixed]
    >>> >
    >>>
    >>>>Unfortunatly, I have implemented "shell" and "puts" functions in my
    >>>>ASP code but `$_` return no result in @output array with a simple
    >>>>'dir' command.
    >>>>
    >>>>I'm afraid it's a problem of right on my IIS server.
    >>>>
    >>>
    >>>If your tick marks above are an accurate portrayal of what you used in
    >>>your shell() code, then, no you will not get output. (You would only
    >>>get an echo back of the command(s) you wanted to run that way.) The
    >>>single tick marks in my shell() routine above are backward ticks. Use
    >>>qx() if this is confusing:
    >>>
    >>>sub shell {
    >>>
    >>> my @output;
    >>> for (@_) { push( @output, qx($_) } }
    >>> chomp( @output );
    >>> return wantarray ? @output : \@output;
    >>>
    >>>}
    >>>

    >>
    >>The extra "}" at the end of the line "for (@_) { push..." in the shell()
    >>routine should be removed. Should have been:
    >>
    >>for (@_) { push( @output, qx($_) ) }
    >>

    > Chris,
    >
    > It was not a problem of backward ticks, no chance.
    > The result is unfortunatly the same ...
    >
    > another idea ?
    >


    Really weird. I suppose the "on topic" question is, what is in the
    array that is returned by 'shell()' when you call it? Nothing? If you
    are trying to do a directory listing or something like that, redirect
    errors to the CONsole so you can see what the issue is:

    puts( shell( 'dir 2>&1' ) );

    Another thing to try would be to issue a command that leaves results
    that can be otherwise inspected after the command is done. Try to
    delete some file you know you should be able to delete and after the
    script runs, go see if it's still there. Something like that. Perhaps
    it *is* a permissions issue. Sounds like IIS might be setup in some
    weird way after all.

    Redirect all output to the CONsole no matter what as a trouble-shooting
    measure (you may have already thought of that). If it's a permissions
    issue, you should get SOMETHING back in the return array of 'shell()'.

    Chris
    -----
    Chris Olive
    chris -at- --spammers-are-__________-- technologEase -dot- com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Feb 10, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Cowald
    Replies:
    4
    Views:
    1,102
    Tohid
    Oct 23, 2003
  2. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    262
    Genich
    Oct 22, 2003
  3. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page.

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    225
    Oliver
    Oct 22, 2003
  4. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page.

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    191
    Lucas Cowald
    Oct 22, 2003
  5. Jack
    Replies:
    1
    Views:
    124
    Jürgen Exner
    Sep 13, 2008
Loading...

Share This Page