开发者

exec not working in expect script

开发者 https://www.devze.com 2023-02-26 20:36 出处:网络
I\'m writing my first expect script, so I may be missing something obvious, but I find the following quite puzzling:

I'm writing my first expect script, so I may be missing something obvious, but I find the following quite puzzling:

$ expect
expec开发者_如何学编程t1.1> exec echo a
a
expect1.2>
$ cat > test.exp
exec echo a
$ expect test.exp
$

If I run expect and make it exec something manually, it works. If I put the same command in a script, it doesn't work. What's going on?


Note that expect is essentially an extension of Tcl. Tcl's exec executes the external command, captures the output and returns it as a value -- it does not send it to stdout unless you instruct it to. However, an interactive Tcl (or expect) session prints the output to stdout as a convenience.

Your script should be:

puts [exec echo a]
# or
set output [exec echo a]
puts $output

I'm assuming "echo a" is a placeholder for a more complicated external command. If you just want to print something:

puts "this is something"  ;# the Tcl way
send_user "this is too\n" ;# the expect way

If you're new to Tcl as well as expect then you should learn Tcl first.

0

精彩评论

暂无评论...
验证码 换一张
取 消