Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirect stdout/stderr of batch using exec

185 views
Skip to first unread message

lamuzz...@gmail.com

unread,
Oct 22, 2022, 1:02:27 AM10/22/22
to
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

Rich

unread,
Oct 22, 2022, 9:00:35 AM10/22/22
to
lamuzz...@gmail.com <lamuzz...@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.

lamuzz...@gmail.com

unread,
Oct 22, 2022, 11:17:26 AM10/22/22
to
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.

Rich

unread,
Oct 22, 2022, 12:31:23 PM10/22/22
to
lamuzz...@gmail.com <lamuzz...@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
> (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.

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.

lamuzz...@gmail.com

unread,
Oct 23, 2022, 8:48:46 PM10/23/22
to
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
0 new messages