[英]Can expect spawn a bash function?
Here is a very simplified example: 这是一个非常简单的例子:
# expect -c "spawn socat -v -,raw,echo=0,nonblock /dev/ttyS0; interact"
expect
executes an in-line script, which spawns socat
in order to connect to a serial
device. expect
执行一个内联脚本,它生成socat
以连接到serial
设备。 But what if we have a bash
function called serial (which is quite handy): 但是如果我们有一个名为serial的
bash
函数(这非常方便)呢:
# serial(){ socat -v -,raw,echo=0,nonblock /dev/ttyS0;}
# expect -c "spawn serial; interact"
spawn serial
couldn't execute "serial": no such file or directory
while executing
"spawn serial"
Is it possible to force expect
to execute a function, without a wrapper script or binary? 是否有可能强制
expect
执行一个函数,没有包装脚本或二进制文件?
You can export
the shell function so a new spawn
ed shell can call it. 您可以
export
shell函数,以便新的spawn
ed shell可以调用它。 For example: 例如:
% cat foo.sh
the_func()
{
echo hello world
}
export -f the_func
expect -c "spawn bash -c the_func; interact"
% bash foo.sh
spawn bash -c the_func
hello world
%
To run a bash function or builtin inside spawn
, you have to make the spawn be of a bash subprocess that runs the function or builtin. 要在
spawn
运行bash函数或内置函数,必须使spawn成为运行函数或内置函数的bash子进程。 This is done via the -c
option to bash, which lets you give a little inline script for bash to run. 这是通过bash的
-c
选项完成的,它允许您为bash运行提供一些内联脚本。
spawn bash -c "serial"
Remember that the single argument after -c
is the inline script to run. 请记住,
-c
之后的单个参数是要运行的内联脚本。 Arguments after that get assigned to bash's positional parameters. 之后的参数被赋予bash的位置参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.