Question

I have looked at some of the related questions and answers in here, but i'm still stumped :-(

I wish to pass a multi-word command from a bash script to an expect script. Inside the expect script, the multi-word command will be executed by a send statement.

So, the multi-word command is:

get dump perf 0

It is stored in the bash script in a variable called usrcmd.

The expect script is called like this:

./userspecifiedcmd.exp root $password $server $usrcmd

In the expect script, the multi-word command is extracted as follows:

set usercommand [lrange $argv 3 3]

And the multi-word command is executed as follows: send "$usercommand\r"

RESULTS...

The string actually sent by the send statement is: get

I have tried putting quotes around the $usrcmd variable when I launch the expect script, like this:

./userspecifiedcmd.exp root $password $server "$usrcmd"

When I do that, the string sent by the send statement is: {get dump perf 0}

My gut tells me there is a simple fix. Is my gut correct?

Thx!

Was it helpful?

Solution

The expect script should be called like this:

./userspecifiedcmd.exp root "$password" "$server" "$usrcmd"

Always quote your shell variables unless you have a compelling to omit them.

In expect, map the args to variables like this:

lassign $argv user password server usercommand

The lrange command returns a list, and when you handle a list like a string, you get the {} artifacts. Be aware of your Tcl data types: handle lists as lists, strings as strings. When you have a list, use join to "convert" it into a string cleanly.

The Tcl manual pages are helpful: http://tcl.tk/man/tcl8.5/TclCmd/contents.htm
As is the Tcl tutorial: http://tcl.tk/man/tcl8.5/tutorial/tcltutorial.html

OTHER TIPS

I'm not sure it's the best solution, but you can do something like this:

ls -1 < "echo $(ls a*)"

so we are listing all a* things as part of all the files.

In your case,

./userspecifiedcmd.exp root $password $server < "echo $usrcmd"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top