1
我正在使用expect来自动化一些bash脚本。一切顺利,直到我需要执行一个源脚本;然后我得到这个错误:需要使用expect产生源代码脚本
couldn't execute "source setNAME.sh": no such file or directory
while executing
"spawn "source setNAME.sh""
(file "./setNAME.exp" line 2)
我用期待的一个基本方式(卵/期望/发送/交互的);问题在于产卵。让我告诉一个很简单的例子:
脚本读取一个名称,回声的价值和出口环境的“姓名”:
/home/edu/scripts [] cat setNAME.sh
#!/bin/bash
echo "Your name?"
read name
export NAME=$name
echo "Provided NAME is $name"
如果你有“源”,然后输入值将执行作为环境变量导出。正是我需要的:
/home/edu/scripts [] source setNAME.sh
Your name?
Dennis
Provided NAME is Dennis
/home/edu/scripts [] echo $NAME
Dennis
让我们使用期望避免相互影响,设置“EDU”作为名称:
/home/edu/scripts [] cat setNAME.exp
#!/usr/bin/expect
spawn setNAME.sh
expect "Your name?"
send edu\r
interact
/home/edu/scripts [] setNAME.exp
spawn setNAME.sh
Your name?
edu
Provided NAME is edu
/home/edu/scripts [] echo $NAME
Dennis
但这个名字,很明显,仍然是丹尼斯:“EDU”显示在sh脚本输出中,但不能导出为NAME,因为expect脚本会生成setNAME.sh脚本的WITHOUT源(或脚本之前的点)。
问题是,我不能这样做,因为我得到的错误在开始评论。
任何想法解决/负担这种需求?
在我的情况下,脚本要复杂得多和其他的解决方案也是无效的,比如这样的事情:
source script.sh << EOF
<option1>
<option2>
...
EOF
这就是我试图用期待的理由...
非常感谢!
优秀的第一个问题,对不起,不是期望专家,但是你尝试过'spawn source setname.csh'吗?祝你好运。 – shellter
当然,这是我做的第一件事(抱歉,我没有解释清楚)。我不明白原因,但肯定有一个。我也试过用引号:产生“源码