How to use a variable in expect body which embedded in a shell script



  • 1. Using Control-a to select all text in a text widget
    I'm having a difficult time binding Control-a so it selects all the text in a widget just like the default Control-/ binding does. I've tried: .t bind <Control-a> [.t bind <Control-/>] But [.t bind <Control-/>] returns nothing. If I do: .t bind <Control-/> {puts "Selecting all"} .t bind <Control-a> [.t bind <Control-/>] When I hit Control-/ I get at the prompt "Selecting all" and all the text is selected. But when I hit Control-a all I get is the "Selecting all" at the prompt and no text is selected. I've also tried: .t bind <Control-a> {.t tag add sel 1.0 end} And that doesn't work either. In fact, with this code if you do Control-/ and then Control-a it unselects everything. And dumping the tags for the entire region show the tagon/tagoff for sel are gone. Does anyone know the semantics of Control-/? There's got to be more to it. - Ian
  • 2. Installing Komodo-IDE-4.1.0-278996-linux
    sudo python -E $dname/support/ "$@" Enter directory in which to install Komodo. Leave blank and press 'Enter' to use the default [/opt/Komodo-IDE-4.1]. Install directory: install: Installing ActiveState Komodo to '/opt/Komodo-IDE-4.1'... Traceback (most recent call last): File "./support/", line 641, in <module> sys.exit( main(sys.argv) ) File "./support/", line 628, in main interactiveInstall(suppressShortcut) File "./support/", line 535, in interactiveInstall install(installDir, suppressShortcut) File "./support/", line 543, in install return _install(installDir, tempDir, suppressShortcut) File "./support/", line 432, in _install import activestate ImportError: No module named activestate ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Need some help on this. I'm linux newbie and I'm using Ubuntu.
  • 3. Fonts on X11
    I having trouble understanding how to apply different fonts on a X11 system. I am familiar with the various font commands, my ignorance is with how fonts become available to Tk (those listed by font families). The fonts available with a standard TclTk 8.4 install are limited and generally don't look very good. Does Tk have a set library of fonts of its own, or does it look to the system. How do I add fonts, particularly something that may be anti-alias. Thanks

Re: How to use a variable in expect body which embedded in a shell script

Postby Andreas Leitgeb » Fri, 23 Jan 2009 19:02:19 GMT

> Thanks a lot for your prompt response. Does expect -c take things>
> between ' and ' as the code of expect?
Yes, and expect doesn't see the single quotes themselves.
> The code outside ' and ' will be taken as shell,
The shell, which also interprets the single-quotes according
to it's syntax (namely: "take everything literally, up to the
next single-quote") will also parse the "$hostname" and finally
create a single string that has all the parts concatenated, and
this string is then passed to expect, which takes it as if it
it had read it from a script-file.
> so "$hostname" was actually a shell style variable.
> So you can just omit those two ", like>
> expect -c '>
> set timeout 15>
> spawn telnet '$hostname'

One principially can, but that has an effect on what the shell
does with the contents of the variable.  For sh-like shells, 
(unlike tclsh!), there *is* a difference between $var and "$var",
but this difference is only relevant for values of var that
contain certain shell-meta-characters such as e.g. whitespace.

Similar Threads:

1.How to use a variable in expect body which embedded in a shell script

Hi all,

My question is how to use expect in a shell script. For example, here
is a little program, it just telnet to some host, and login root


echo "This is to test how to call expect in a shell"


expect -c '
set timeout 15
spawn telnet
expect "login:" { send "root\r" }
expect "assword:" { send "abcd1234\r" }

Can I use variable in the body of expect? Such as the is a
variation passed to the shell scripts, like the following, actually
this is wrong, expect can't handle $hostname


echo "This is to test how to call expect in a shell"


expect -c '
set timeout 15
spawn telnet $hostname
expect "login:" { send "root\r" }
expect "assword:" { send "abcd1234\r" }

it will issue the following error.
[tina@kgardenia test]$ ./test.expect
This is to test how to call expect in a shell
can't read "hostname": no such variable
    while executing
"spawn telnet $hostname"

Does anybody know how to use a variable in the body of the expect
code, which is embedded in a bash?

Many thanks to you all :D

2.expect script with shell script

I am using expect script in shell script.
I want one value from expect script.
how can i get the value from expect script and use it in shell script?

3.Expect : Interact returns to shell instead of script

I'm new to tcl and expect, and am trying to do something very simple,
run a few commands by script, then interact, then run some more
commands.  When i'm interacting, andI enter the '+' key expecting to
return to my script, I get bounced out of the script and get a new
shell prompt.  "Hello World 2" never gets printed.

I've wondered if something in my shell set up is getting in the way,
and to that end I removed my .profile so I get a vanilla shell
environment.  (I use vi command line editing with set -o vi and
thought that was a problem, but ....)

expect version 5.42.0
Running on MacOS  v 10.4.11  inside "Terminal" application/ bash shell

#!/usr/bin/expect -f

set timeout -1

spawn $env(SHELL)
match_max 100000

send  "echo Hello World 1\r"
expect  "*$"

interact "+" return

send "echo Hello World 2\r"
expect "*$" command as argument to expect script

Here is the scenario which is not working for me.

1. I have a shell script in BASH, which constructs a shell variable
containing some command e.g.
var=cd /usr/local/home/user/server/config; grep -i propname

2. I called an expect script from this shell script. The purpose of
this expect script is to SSH to remote server and execute the command
mentioned in the variable $var.

3. In the Bash Script, I call the expect script in following way

expect.exp $host_name $user $password $var

4. In the Expect script, I read these command line arguments in
following way:

set host_name [lrange $argv 0 0]
set user [lrange $argv 1 1]
set password [lrange $argv 2 2]
set var [lrange $argv 3 end]

5. I spawn the ssh in expect in the following way:

spawn ssh $user@$host_name $var

Observations: It looks like that while interpreting the variable $var
(which contains the shell commands), it's appending the curly braces
and hence bash is not able to understand it. Here is the error message
I am getting:

spawn ssh  XXXX@XXXXX.COM  {cd /usr/local/site/
pvr/config/; grep -i jdbc}
bash: {cd: command not found
grep:}: No such file or directory
expect: spawn id exp7 not open
    while executing
"expect "*""
    (file "./ssh_prop.exp" line 36)

NOTE: Please see it's appending the curly braches while interpreating
the variable $var. Due to this bash is not able to understand and
throwing error.

Please help in this regard.

5.Problem with get exit status of shell script/command within expect

Hi, all
I'm caught in the problem with get exit status of shell script/command,
with ssh. For example:

% ssh hostname command
% echo $?

This will tell me the exit status of `command'.

The problem is that when using ssh, I need to do some interactive
Here I begin to do it with expect, like the following:
#! /bin/bash


expect_cmd=`cat << EOF
spawn ssh $host $command
set timeout -1
expect {
        "*password:" { send "xxxxxxxx\r" }
        "* (yes/no)?" { send "yes\r"; exp_continue }
        eof           exit

expect -c "$expect_cmd"
echo $?
But here I will get the exit status of `expect', not `command'.
So, how I can solve this problem?

Any help is appreciated.
Thanks in advance.

6. How to run the expect script using TCL oe expect 5.21

7. Importing variables from Main Expect Body to Procedures

8. Passing shell variable as command line argument to expect

Return to tcl


Who is online

Users browsing this forum: No registered users and 9 guest