• Another question on arg expansion in TCL/Expect

    From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.tcl on Thu Jul 9 11:01:00 2026
    From Newsgroup: comp.lang.tcl

    This is on Linux, with exp_version 5.45.4 and tclversion 8.6.

    I have an Expect script containing the line:

    spawn sh -c "$argv 2> $FileForStderr"

    where $argv is the command line args passed to this Expect script and $FileForStderr is a variable previously calculated in the script.

    I use the shell primarily to do the stderr redirection , but also to expand $argv. Now, this all works fine as long as $argv is "normal" - no funny
    shell metas or args with spaces - and for my purposes, that's fine.

    But suppose I wanted to run the above script like this:

    myscript printf "%s\n" 1 2 3

    I could not get this to work - that to produce the expected output of:

    1
    2
    3

    with newlines but with no funny (TCL-inserted) {}s in the output.

    Note that I usually use some form of: eval spawn $argv
    but here, that is not necessary (since the shell does the expansion for you) and it doesn't change the results. Also, I could not get the {*} (list expansion operator) to work. When I do something like:

    spawn sh -c "{*}$argv 2> $FileForStderr"

    The shell sees literally: {*}The args as passed

    So, this question, while mostly academic, is: Is there a way to get what I
    want - that is, for the above command line (with printf) to work as
    expected?

    Similarly, for something like:

    myscript printf "This is a test: %s\n" 1 2 3

    Note: I know that TCL's "exec" command has direct support for redirections,
    and I've used it before, but here, because we are using Expect, we have to
    use spawn and (AFAICT) "spawn" doesn't support redirections. I know there
    is a way to use exec to "simulate" spawn - and I've used this method on
    other projects - but here it is not convenient to do that.
    --
    They say that Trump is a stupid man's idea of a clever man, a poor man's
    idea of a rich man, and a weak man's idea of a strong man.

    Well, Melania is an unclassy man's idea of classy.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Harald Oehlmann@wortkarg3@yahoo.com to comp.lang.tcl on Thu Jul 9 13:35:28 2026
    From Newsgroup: comp.lang.tcl

    Am 09.07.2026 um 13:01 schrieb Kenny McCormack:
    This is on Linux, with exp_version 5.45.4 and tclversion 8.6.

    I have an Expect script containing the line:

    spawn sh -c "$argv 2> $FileForStderr"

    where $argv is the command line args passed to this Expect script and $FileForStderr is a variable previously calculated in the script.

    I use the shell primarily to do the stderr redirection , but also to expand $argv. Now, this all works fine as long as $argv is "normal" - no funny shell metas or args with spaces - and for my purposes, that's fine.

    But suppose I wanted to run the above script like this:

    myscript printf "%s\n" 1 2 3

    I could not get this to work - that to produce the expected output of:

    1
    2
    3

    with newlines but with no funny (TCL-inserted) {}s in the output.

    Note that I usually use some form of: eval spawn $argv
    but here, that is not necessary (since the shell does the expansion for you) and it doesn't change the results. Also, I could not get the {*} (list expansion operator) to work. When I do something like:

    spawn sh -c "{*}$argv 2> $FileForStderr"

    The shell sees literally: {*}The args as passed

    So, this question, while mostly academic, is: Is there a way to get what I want - that is, for the above command line (with printf) to work as
    expected?

    Similarly, for something like:

    myscript printf "This is a test: %s\n" 1 2 3

    Note: I know that TCL's "exec" command has direct support for redirections, and I've used it before, but here, because we are using Expect, we have to use spawn and (AFAICT) "spawn" doesn't support redirections. I know there
    is a way to use exec to "simulate" spawn - and I've used this method on
    other projects - but here it is not convenient to do that.



    On the tcl level, argv is a list.
    To have this list for example space separated elements, use
    [join $argv " "]

    Then, all the funny characters are gone...

    Disclaimer: I don't know expect nor Linux command line, but tcl syntax

    Harald
    --- Synchronet 3.22a-Linux NewsLink 1.2