可以期望生成一个 bash 函数吗?
Can expect spawn a bash function?
这是一个非常简单的例子:
# expect -c "spawn socat -v -,raw,echo=0,nonblock /dev/ttyS0; interact"
expect
执行内联脚本,生成 socat
以连接到 serial
设备。但是,如果我们有一个名为 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"
是否可以在没有包装脚本或二进制文件的情况下强制 expect
执行函数?
您可以 export
shell 函数,以便新 spawn
ed shell 可以调用它。例如:
% 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
%
要 运行 一个 bash 函数或 spawn
中的内置函数,您必须使 spawn 成为 bash 子进程的 运行 子进程功能或内置。这是通过 bash 的 -c
选项完成的,它允许您为 bash 到 运行.
提供一些内联脚本
spawn bash -c "serial"
请记住 -c
之后的 单个 参数是 运行 的内联脚本。之后的参数被分配给 bash 的位置参数。
这是一个非常简单的例子:
# expect -c "spawn socat -v -,raw,echo=0,nonblock /dev/ttyS0; interact"
expect
执行内联脚本,生成 socat
以连接到 serial
设备。但是,如果我们有一个名为 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"
是否可以在没有包装脚本或二进制文件的情况下强制 expect
执行函数?
您可以 export
shell 函数,以便新 spawn
ed shell 可以调用它。例如:
% 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
%
要 运行 一个 bash 函数或 spawn
中的内置函数,您必须使 spawn 成为 bash 子进程的 运行 子进程功能或内置。这是通过 bash 的 -c
选项完成的,它允许您为 bash 到 运行.
spawn bash -c "serial"
请记住 -c
之后的 单个 参数是 运行 的内联脚本。之后的参数被分配给 bash 的位置参数。