Bash 如果预期字符串未出现,如何继续?

Bash 如果预期字符串未出现,如何继续?,bash,expect,Bash,Expect,我正在使用expect脚本来安装一些软件包。在这样的安装过程中,有时它会请求权限(比如Y/n)来安装它们,有时则不会 我这里有两个问题: 1)如何处理这两种情况 #!/usr/bin/expect -- # This is for boto libraries installation spawn apt-get install python-pip expect { "Do you want to continue" { send "Y\r\n" ## HE

我正在使用
expect
脚本来安装一些软件包。在这样的安装过程中,有时它会请求权限(比如
Y/n
)来安装它们,有时则不会

我这里有两个问题:

1)如何处理这两种情况

#!/usr/bin/expect --
# This is for boto libraries installation
spawn apt-get install python-pip 
expect { 
    "Do you want to continue" 
        {   send "Y\r\n"  ## HERE SOMETIMES THIS STRING MAY NOT APPEAR
        }
}
interact
spawn pip install filechunkio 
interact
spawn pip install -U boto 
interact
当expect字符串未出现时,它将抛出错误

spawn_id: spawn id exp6 not open
    while executing
"interact"
    (file "./botoInstall.exp" line 10)
第10行是第一个交互


2)这里的
spawn\u id:spawn id exp6 not open
是什么意思?

您必须使用
exp\u continue
中有一个可选字符串,以等待它。上述脚本可以根据需要进行修改

spawn apt-get install python-pip 

expect { 
    "Do you want to continue" {   send "Y\r\n"; exp_continue }
    #some other expect string along with 'exp_continue'
    timeout { puts "timeout happened" }
    eof { #some other action here# } 
}
如果
expect
看到这些单词,它将发送
y\r\n
,否则它将继续检查其他字符串

请记住在
exp\u continue
用法中有一些退出条件。否则,如果在时间限制内看不到它们,显然会发生超时


关于您对
spawn id exp6 not open
的查询,请查看和。

-y
传递给
apt get
是否会造成混乱?对于
apt get
步骤,请至少查看。对于1)请参阅Dinesh的答案。对于2)这是因为预期的字符串没有出现,所以
expect
命令只会在
timeout
eof
时返回。由于
apt get
命令通常会很快完成(我不确定
exp\u continue
是否是此处的相关细节。我认为
超时
是。尽管您需要
exp\u continue
如果在同一块中有其他字符串要匹配。