Jon
Jon

Reputation: 1705

Unable to accept multiple commandline arguments into one variable within expect

Using cygwin 32bit, expect v5.45 (what comes with the latest cygwin, it seems).

COMMAND="$WIN_BUILD_ROOT\\scripts\\signBinaries.bat $BUS $NET_DRIVE $WIN_SIGNING_ROOT"
$CLIENT_BUILD_ROOT/scripts/runCommand.sh $arg1 $arg2 $arg3 $arg4 $COMMAND

runCommand.sh:

#!C:\cygwin\bin\expect.exe -f
set timeout 9

set arg1 [lindex $argv 0]
set arg2 [lindex $argv 1]
set arg3 [lindex $argv 2]
set arg4 [lindex $argv 3]
set COMMAND [lrange $argv 4 end]

send -- "$COMMAND\r"

Gives me:

{s:\git\builds\scripts\signBinaries.bat} 64 s {s:\git\builds\}
The filename, directory name, or volume label syntax is incorrect.

The scenario is, the first four arguments are fixed. There could then be a variable number of commands following those, which I want to have as one command to be executed by expect. If I just use [lindex argv 4], I only get the signBinaries script name, whether or not it's enclosed in quotes. Using lrange (found via googling) instead, it's enclosing the string arguments in braces, as shown. Why would it be modifying my arguments in this fashion and how do I fix it so that $COMMAND contains the command as I intended it to be?

Upvotes: 0

Views: 31

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

In Tcl, which expect extends, you have to be aware of the datatype of your variables: is it a string or a list? When a list gets stringified, it may quote some of its elements if they contain "metacharacters" (like backslash and braces).

In most cases when you want to use the contents of a list as a single string, it's best to stringify it yourself:

set COMMAND [list "s:\\git\\builds\\scripts\\signBinaries.bat" 64 s "s:\\git\\builds\\"]

puts $COMMAND
# => {s:\git\builds\scripts\signBinaries.bat} 64 s s:\\git\\builds\\

puts [join $COMMAND " "]
# => s:\git\builds\scripts\signBinaries.bat 64 s s:\git\builds\

Upvotes: 1

Jon
Jon

Reputation: 1705

Aha! Figured it out:

set COMMAND [join [lrange $argv 4 end]]

Gives me:

s:\git\builds\scripts\signBinaries.bat 64 s s:\git\builds
"BUS: 64"
"DRIVE: s"
"WORKSPACE: s:\git\builds"

Upvotes: 0

Related Questions