0
votes

I've got tcl script with two ways of execution bash script:

#exec bash ./run.sh
open "|bash ./run.sh r"

The bash script is shown below:

#!/bin/bash
ls
if [ "$?" != "0" ]; then
    echo "ERROR: Status failed!" > status
else
    echo "Everything is OK!" > status
fi

I'm using tclsh for Windows with bash from git bash. When I use:

exec bash ./run.sh

I've got in status file:

Everything is OK!

otherwise:

open "|bash ./run.sh r"

got:

ERROR: Status failed!

Is there any possibility to correctly detect exit code when opened the tcl pipe?

1
Cannot reproduce. - glenn jackman
Shouldn't the open call look like so: open "|bash ./run.sh" r (r outside of the pipe argument)? - mrcalvin
Glenn, did you try this on Windows? mrcalvin, it does not change anything - same problem. - J. Doe

1 Answers

1
votes

You don't describe whether you get different results out of the ls part of the script. That matters; the ls command is most certainly capable of changing its behaviour according to the environment in which it is invoked. This matters because Tcl executes subprocesses (on Windows) directly using the CreateProcess() system call, rather than the various wrapped versions that Cygwin and git bash use. Other possibilities are that you're launching the script in a different directory and so on.

However, in general we'd expect a script to behave very similarly when launched via exec or via open |… r as they share a common core of functionality. The only differences are to do with how output and termination are waited for.


If you create a subprocess pipeline, by default you won't get to find out about errors from it until you close the pipeline. exec generates any errors “immediately” because it doesn't return control to you until the subprocess has terminated and all output has been read.