• Puzzling "exec" behavior in Windows 10

    From Michael Soyka@21:1/5 to All on Tue May 31 13:08:56 2022
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see
    the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of
    statements such as:

    set someDir C:[file nativename /Users]
    exec -- cmd.exe /C cd $someDir && dir

    which responds: "The system cannot find the path specified."

    If I disable command extensions:

    exec -- cmd.exe /E:OFF /C cd $someDir && dir

    I don't get an error but I don't get the expected list of files either.

    However, this command works as expected:

    exec -- cmd.exe /C dir $someDir

    so I'm thinking it has something to do with the &&, hence my question.

    One more thing. Such commands did work a few months ago so something
    has changed.

    I see this on Windows 10 using both Tcl 8.6.9 and 8.6.12.

    Thanks in advance for any help/insights.

    -mike

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to Michael Soyka on Tue May 31 15:07:31 2022
    On 05/31/2022 1:08 PM, Michael Soyka wrote:
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see
    the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of
    statements such as:

        set someDir C:[file nativename /Users]
        exec -- cmd.exe /C cd $someDir && dir

    which responds: "The system cannot find the path specified."

    However, this alternative does work:

    exec -- cmd.exe /C "cd $someDir && dir"

    but I don't understand why.


    If I disable command extensions:

        exec -- cmd.exe /E:OFF /C cd $someDir && dir

    I don't get an error but I don't get the expected list of files either.

    However, this command works as expected:

        exec -- cmd.exe /C dir $someDir

    so I'm thinking it has something to do with the &&, hence my question.

    One more thing.  Such commands did work a few months ago so something
    has changed.

    I see this on Windows 10 using both Tcl 8.6.9 and 8.6.12.

    Thanks in advance for any help/insights.

    -mike

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Tue May 31 21:31:20 2022
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see
    the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of
    statements such as:

    set someDir C:[file nativename /Users]
    exec -- cmd.exe /C cd $someDir && dir
    Hello Mike,
    I think you need quotes: Try
    exec -- cmd.exe /C "cd $someDir" && "dir"
    or
    exec -- cmd.exe /C "cd $someDir" && dir
    both worked on my Windows 10, 64 bit system.
    Note, that when 'exec' returns I was still in my original directory,
    but I could of course 'cd' from within Tcl.
    I don't know what you want to do but did you have a look at 'glob'?

    HTH
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to [email protected] on Tue May 31 14:50:35 2022
    At Tue, 31 May 2022 15:07:31 -0400 Michael Soyka <[email protected]> wrote:


    On 05/31/2022 1:08 PM, Michael Soyka wrote:
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see
    the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of statements such as:

        set someDir C:[file nativename /Users]
        exec -- cmd.exe /C cd $someDir && dir

    which responds: "The system cannot find the path specified."

    However, this alternative does work:

    exec -- cmd.exe /C "cd $someDir && dir"

    but I don't understand why.

    I'm guessing that "cmd.exe /C <mumble>" is MS-Windows's way of doing "sh -c <mumble>". My guess, like sh -c <mumble>, there are some gotchas relating to how you need to quote things. You want to create a subshell and run something like a pipeline there. You need to make sure the whole pipeline is passed to the subshell, so you need to make the pipeline *one* argument ("cd $someDir && dir"), not 4 arguments (cd, $someDir, &&, dir). If you don't, you end up passing just cd to cmd.exe /C, and then you have the three options "$someDir", "&&", and "dir" leftover.



    If I disable command extensions:

        exec -- cmd.exe /E:OFF /C cd $someDir && dir

    I don't get an error but I don't get the expected list of files either.

    However, this command works as expected:

        exec -- cmd.exe /C dir $someDir

    so I'm thinking it has something to do with the &&, hence my question.

    One more thing.  Such commands did work a few months ago so something has changed.

    I see this on Windows 10 using both Tcl 8.6.9 and 8.6.12.

    Thanks in advance for any help/insights.

    -mike




    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    [email protected] -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to Helmut Giese on Tue May 31 16:07:19 2022
    On 05/31/2022 3:31 PM, Helmut Giese wrote:
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see
    the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of
    statements such as:

    set someDir C:[file nativename /Users]
    exec -- cmd.exe /C cd $someDir && dir
    Hello Mike,
    I think you need quotes: Try
    exec -- cmd.exe /C "cd $someDir" && "dir"
    or
    exec -- cmd.exe /C "cd $someDir" && dir
    both worked on my Windows 10, 64 bit system.
    Note, that when 'exec' returns I was still in my original directory,
    but I could of course 'cd' from within Tcl.
    I don't know what you want to do but did you have a look at 'glob'?

    Thanks Helmut for the suggestion but my example is misleading.

    My goal is to use a single shell to execute a sequence of commands. I
    crafted the above example to demonstrate the problem with as simple an
    example as I could.


    HTH
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to Helmut Giese on Tue May 31 16:12:54 2022
    On 05/31/2022 3:38 PM, Helmut Giese wrote:

    However, this alternative does work:

    exec -- cmd.exe /C "cd $someDir && dir"

    but I don't understand why.

    Hello Mike,
    I don't know how good your German is but here goes:
    Hinweis: Mehrere Befehle, die durch das Befehlstrennzeichen "&&"
    getrennt sind, werden als Zeichenfolge akzeptiert, wenn diese von Anführungsstrichen umgeben sind.
    My translation: Note: Several commands separated by "&&" are accepted
    when they are quoted. (short form)

    Sorry but my German is extremely limited!
    Nonetheless, I'm somewhat confused by what "help cmd" displays.

    However, the following does work in a cmd window without the double quotes:

    cmd.exe /C cd $someDir && dir

    HTH
    Helmut

    PS: I got this info by typing 'help cmd' in a console window.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Tue May 31 21:38:24 2022

    However, this alternative does work:

    exec -- cmd.exe /C "cd $someDir && dir"

    but I don't understand why.

    Hello Mike,
    I don't know how good your German is but here goes:
    Hinweis: Mehrere Befehle, die durch das Befehlstrennzeichen "&&"
    getrennt sind, werden als Zeichenfolge akzeptiert, wenn diese von Anf�hrungsstrichen umgeben sind.
    My translation: Note: Several commands separated by "&&" are accepted
    when they are quoted. (short form)
    HTH
    Helmut

    PS: I got this info by typing 'help cmd' in a console window.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to Robert Heller on Tue May 31 16:29:09 2022
    On 05/31/2022 3:50 PM, Robert Heller wrote:
    At Tue, 31 May 2022 15:07:31 -0400 Michael Soyka <[email protected]> wrote:


    On 05/31/2022 1:08 PM, Michael Soyka wrote:
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see
    the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of
    statements such as:

        set someDir C:[file nativename /Users]
        exec -- cmd.exe /C cd $someDir && dir

    which responds: "The system cannot find the path specified."

    However, this alternative does work:

    exec -- cmd.exe /C "cd $someDir && dir"

    but I don't understand why.

    I'm guessing that "cmd.exe /C <mumble>" is MS-Windows's way of doing "sh -c <mumble>". My guess, like sh -c <mumble>, there are some gotchas relating to how you need to quote things. You want to create a subshell and run something like a pipeline there. You need to make sure the whole pipeline is passed to the subshell, so you need to make the pipeline *one* argument ("cd $someDir &&
    dir"), not 4 arguments (cd, $someDir, &&, dir). If you don't, you end up passing just cd to cmd.exe /C, and then you have the three options "$someDir",
    "&&", and "dir" leftover.

    I agree that that appears to be true. However, the "exec" man page says
    (to me) that each "exec" argument represents a subprocess. Under that assumption I don't understand why "cmd.exe", "/C" and the rest should be formatted as 3 Tcl words (neither 1 nor 2 work).

    I'm reviewing what I think is the relevant Tcl source code
    (tclWinPipe.c) but, so far, it's been tough sledding!




    If I disable command extensions:

        exec -- cmd.exe /E:OFF /C cd $someDir && dir >>>
    I don't get an error but I don't get the expected list of files either.

    However, this command works as expected:

        exec -- cmd.exe /C dir $someDir

    so I'm thinking it has something to do with the &&, hence my question.

    One more thing.  Such commands did work a few months ago so something
    has changed.

    I see this on Windows 10 using both Tcl 8.6.9 and 8.6.12.

    Thanks in advance for any help/insights.

    -mike





    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to [email protected] on Tue May 31 16:19:24 2022
    At Tue, 31 May 2022 16:29:09 -0400 Michael Soyka <[email protected]> wrote:


    On 05/31/2022 3:50 PM, Robert Heller wrote:
    At Tue, 31 May 2022 15:07:31 -0400 Michael Soyka <[email protected]> wrote:


    On 05/31/2022 1:08 PM, Michael Soyka wrote:
    Hello,

    If Tcl's "exec" command is used to execute a command, how does one see >>> the exact command that is issued to the OS?

    I'm asking this question because I'm investigating the failure of
    statements such as:

        set someDir C:[file nativename /Users]
        exec -- cmd.exe /C cd $someDir && dir

    which responds: "The system cannot find the path specified."

    However, this alternative does work:

    exec -- cmd.exe /C "cd $someDir && dir"

    but I don't understand why.

    I'm guessing that "cmd.exe /C <mumble>" is MS-Windows's way of doing "sh -c <mumble>". My guess, like sh -c <mumble>, there are some gotchas relating to
    how you need to quote things. You want to create a subshell and run something
    like a pipeline there. You need to make sure the whole pipeline is passed to
    the subshell, so you need to make the pipeline *one* argument ("cd $someDir &&
    dir"), not 4 arguments (cd, $someDir, &&, dir). If you don't, you end up passing just cd to cmd.exe /C, and then you have the three options "$someDir",
    "&&", and "dir" leftover.

    I agree that that appears to be true. However, the "exec" man page says
    (to me) that each "exec" argument represents a subprocess. Under that assumption I don't understand why "cmd.exe", "/C" and the rest should be formatted as 3 Tcl words (neither 1 nor 2 work).

    The various "words" after /C are one logic operand to cmd.exe, just like the "words" after the -c are in the case of Unix's sh. In both cases, all of the "words" need to be collected as a single "word". This is done with quotes.

    You are also misreading the exec man page. The exec command creates 1 subprocess for its first word and passes the remaining words as arguments, possibly with special considerations for things that look like command separaters (&&, |, ;). I guessing that since you are being upfront with cmd.exe, you actually want something different, esp. since the cd command is a built-in and never forks (and it makes no sense to fork it). What you want here is a "subshell" (hense the use of "cmd.exe /C"), to handle all of your commands. So, you need to bundle them up (with quote marks).

    Basically, using "cmd.exe /C" (or "sh -c") is a special case requiring special handling. It may or may not make sense to create a .bat (or .sh) file instead, depending on the complexity. Then you would just run the .bat (or .sh) file:

    set fp [open "temp.bat" w]
    puts $fp "cd $someDir"
    puts $fp "dir"
    close $fp
    exec cmd.exe temp.bat
    file delete -force temp.bat



    I'm reviewing what I think is the relevant Tcl source code
    (tclWinPipe.c) but, so far, it's been tough sledding!




    If I disable command extensions:

        exec -- cmd.exe /E:OFF /C cd $someDir && dir

    I don't get an error but I don't get the expected list of files either. >>>
    However, this command works as expected:

        exec -- cmd.exe /C dir $someDir

    so I'm thinking it has something to do with the &&, hence my question. >>>
    One more thing.  Such commands did work a few months ago so something
    has changed.

    I see this on Windows 10 using both Tcl 8.6.9 and 8.6.12.

    Thanks in advance for any help/insights.

    -mike








    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    [email protected] -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)