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

Hello Team ,

I am using system command in my script which is running on windows using CMD . The command which I am running is mentioned below .

system("simple_pin_$thost.pl $thost  | findstr \n "2016 TTL timed"  >> ping_$thost.txt" );

when run though the script it gives below error message .

Number found where operator expected at C:\Windows\Scripts\Run_ping.pl + line 16, near ""simple_pin_$thost.pl $thost | findstr \n "2016" (Missing operator before 2016?) Bareword found where operator expected at C:\Windows\Scripts\Run_ping. +pl line 16, near "2016 TTL" (Missing operator before TTL?) syntax error at C:\Windows\Scripts\Run_ping.pl line 16, near ""simple_ +pin_$thost.pl $thost | findstr \n "2016" Execution of C:\Windows\Scripts\Run_ping.pl aborted due to compilation + errors.

-Ajay

Replies are listed 'Best First'.
Re: Using system command not able to run though the script
by haukex (Archbishop) on Sep 07, 2016 at 06:41 UTC

    Hi chopraa,

    Please use <code> tags to format your post, see e.g. How do I post a question effectively?

    When you write:

    system("simple_pin_$thost.pl $thost | findstr \n "2016 TTL timed" >> + ping_$thost.txt" );

    The quotes after findstr are ending the Perl string, and Perl expects to see an operator there. Escape them with \, or use a different quoting construct:

    system("simple_pin_$thost.pl $thost | findstr \n \"2016 TTL timed\" +>> ping_$thost.txt" ); system(qq{simple_pin_$thost.pl $thost | findstr \n "2016 TTL timed" +>> ping_$thost.txt});

    See also Quote Like Operators.

    Hope this helps,
    -- Hauke D

      Using command in scripts still getting the error message . system(qq{simple_pin_$thost.pl $thost | findstr \n "2016 TTL timed" >> ping_$thost.txt}); #system("simple_pin_$thost.pl $thost | findstr \n \"2016 TTL timed\" >> ping_$thost.txt" ); ERROR Message : FINDSTR: Bad command line -Ajay

        Hi chopraa,

        When you write "... \n ...", that is interpolated and produces a newline character in the string. If you want to send the literal string \n to the shell, then in your Perl string you have to escape the backslash and write "... \\n ..." or qq{... \\n ...}. However, NetWallah has a good point, perhaps you meant /n?

        Hope this helps,
        -- Hauke D

Re: Using system command not able to run though the script
by NetWallah (Canon) on Sep 08, 2016 at 03:06 UTC
    You probably want "...| findstr /n ...", not "...|findstr \n ...".

    The first option tells 'findstr' to produce line numbers. The second "\n" will get interpreted by perl to be a newline, and probably not achieve what you intended.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall