• Redirect stdout/stderr of batch using exec

    From lamuzzachiodi@gmail.com@21:1/5 to All on Fri Oct 21 22:02:14 2022
    I've have a program to send mails (command line)
    I want to use it - in a batch file - with exec, but i can't to get the output/error, just the batch command line itself.
    According Windows documentation i can use "2>&1" to capture stdout and stderr, so i try something like

    set argumentos [list $file "> $res" "2>&1"]
    exec mandamail.bat {*}$argumentos

    where $file is a parameter of the batch and $res the file used to save the result of sending this file.
    After running that, i get two files , the file "&1" empty and the file $res with the command line (c:\pathxxx1 c:\path_to_exe\xxx.exe. -param1 -param2 $file - etc.) instead of a $res file containig "Mail send succefully".
    How i can get the result of the program inside this batch ?
    Thanks,

    Alejandro

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to lamuzz...@gmail.com on Sat Oct 22 13:00:31 2022
    lamuzz...@gmail.com <lamuzzachiodi@gmail.com> wrote:
    I've have a program to send mails (command line)
    I want to use it - in a batch file - with exec, but i can't to get
    the output/error, just the batch command line itself. According
    Windows documentation i can use "2>&1" to capture stdout and stderr,
    so i try something like

    set argumentos [list $file "> $res" "2>&1"]
    exec mandamail.bat {*}$argumentos

    exec is a Tcl command, so you should be reading the Tcl documentation
    on exec redirection operators, not the windows documentation. The exec
    command interprets these, they never make it out for windows to see
    them.

    The Tcl 'redirect standard error to standard output' operator is 2>@1,
    and the 'exec' command may want > in its own parameter, so your set
    line should likely be:

    set argumentos [list $file > $res 2>@1]

    so that each item is a separate list element.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From lamuzzachiodi@gmail.com@21:1/5 to All on Sat Oct 22 08:17:23 2022
    El sábado, 22 de octubre de 2022 a la(s) 10:00:35 UTC-3, Rich escribió:

    I started with the simplest, passing the program and its arguments to the exec.
    However, I was getting an error as if argument string was truncated (does exec have any length restrictions?).
    This is not a program problem because when run as a batch it works ok.
    So I tried using exec with the batch file.
    But if I redirect the output of the batch file using the exec options (2>@1) what I get is the command line that is executed within the batch, not the output of the program.
    That's why I thought I'd send the windows option as a parameter.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to lamuzz...@gmail.com on Sat Oct 22 16:31:19 2022
    lamuzz...@gmail.com <lamuzzachiodi@gmail.com> wrote:
    El sábado, 22 de octubre de 2022 a la(s) 10:00:35 UTC-3, Rich escribió:

    I started with the simplest, passing the program and its arguments to
    the exec.
    However, I was getting an error as if argument string was truncated
    (does exec have any length restrictions?).

    exec the Tcl command may not, but the underlying OS most likely does. Presumably, given a "bat" file, you are on windows?

    This is not a program problem because when run as a batch it works ok.
    So I tried using exec with the batch file.
    But if I redirect the output of the batch file using the exec options
    @1) what I get is the command line that is executed within the
    batch, not the output of the program.
    That's why I thought I'd send the windows option as a parameter.

    For a batch file, on windows, to 'exec' it you likely also have to
    invoke the command interpreter. You may want to do this:

    exec {*}[auto_execok mycommand.bat] ...

    auto_execok is another Tcl command that will return the necessary
    'magic' to properly launch "mycommand.bat". Replace ... with the rest
    of the parameters you'd otherwise pass along.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From lamuzzachiodi@gmail.com@21:1/5 to All on Sun Oct 23 17:48:43 2022
    After much testing, I concluded that the program did not direct output to stdout on successful completion.
    Maybe, to stdin/con, I couldn't find the answer.
    However, the errors did go to stderr so I just focused on that.
    Finally, using Brad Lanam's code, answering a question on Stackoverflow (https://stackoverflow.com/questions/41705067/how-to-get-a-return-value-of-a-c-exe-file-in-tcl-windows), it looked like this :

    # file is the parameter of batch
    set returnvalue 1
    try {
    exec mandamail.bat $file 2> err_$file
    set returnvalue 0
    } trap {CHILDSTATUS} {resul retopts} {
    lassign [dict get $retopts -errorcode] class pid retcode
    set returnvalue $retcode
    }
    if {$returnvalue != 0} {
    set filerr err_$file
    set ferr [open $filerr r]
    puts [read $ferr]
    close $ferr
    } else {
    puts "Mail sent succefully !"
    }

    Saludos,

    Alejandro

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