When you run the batch file, once you get to the prompt of the secondary shell, you have a process tree like this:

cmd.exe ## running the batch file perl.exe ## running -e ... system() cmd.exe ## 2

The second copy of cmd.exe (tagged ##2) is now in control of the console window hence the c:\tmp> prompt.

When you type ^C, all the processes in the tree receive the message. The action they take depends upon their nature.

  1. The second shell does nothing except re-prompt, just as you see in a normal cmd window.
  2. Perl.exe emits Terminating on signal SIGINT(2) and terminates.
  3. the first level shell prompts with the normal (for a shell running a batch file) message:
    Terminate batch job (Y/N)?

At this point, with the perl process having ended, the (grandparent/grandchild) relationship between the first and second shells has been broken and the (relevant) process "tree" has become:

cmd.exe ## running the batch file cmd.exe ## 2

In other words, both shells are still running, but as peers. More importantly, they are sharing the same console session. And that is the source of your confusion.

When you see the prompt Terminate batch job (Y/N)?, you respond to it. The problem is, as both shells are sharing the console session, and both are looking for input, when you respond, it is the other shell that reads the response. It tries to run your 'Y' as a command and you get:

'y' is not recognized as an internal or external command, operable program or batch file. c:\tmp>

So now you think you are talking to the second shell and probably type a command. Say dir, and you get:

c:\tmp>dir Terminate batch job (Y/N)?

Because this time, its the first shells turn to receive the input.

Think of it like talking to someone long distance on a call that has been routed via a satellite which introduces that 1/4 second delay into the conversation.

The way out of the impasse at the keyboard is to type exit in response to the Terminate batch job (Y/N)? message. The exit will actually be read by the secondary shell, and it will terminate.

Then hit enter and you will get the Terminate batch job (Y/N)? prompt again and this time your response will be seen by the "correct" (first) shell, as it is the only one still running:

Terminate batch job (Y/N)? exit Terminate batch job (Y/N)? y c:\test>

The solution to avoiding the creation of the problem would be for perl to ignore ^C until any active system call completed. Obviously, you can arrange this to happen yourself on a case by case basis using %SIG{INT}.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Interaction of Windows Batch files and Perl's system() function by BrowserUk
in thread Interaction of Windows Batch files and Perl's system() function by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.