通过脚本实现linux上安装的自动交互

来源:互联网 发布:结构力学软件 编辑:程序博客网 时间:2024/05/17 07:23


需要在服务器上安装若干软件,一个个的去安装浪费时间,通过脚本安装时,安装过程中的[y/N]类似的询问,通过 -y --force-yes 又莫名奇妙的报错,没有完成全部安装。只能从自动响应交互来切入了,查了下看可以通过expect来实现。主要代码如下,运行环境为ubuntu,需要安装expect(apt-get install expect即可)



apt-get install expect   #安装expect   #!/bin/bashecho $1cat $1 | while read linedoif [ "$line" != "******" ];thenecho ">>> Now install " $line/usr/bin/expect <<-EOF  #expect的路径spawn apt-get install $lineexpect "y/N" send "y\n"  if [ $?  -eq 0 ];thencontinueelseecho 'install is  ERR'breakfiexpect eof EOFelseecho ">>> Now Remove " $line/usr/bin/expect <<-EOFspawn apt-get remove $lineexpect "y/N" send "y\n"if [ $?  -eq 0 ];thencontinueelseecho 'Remove is ERR'break    fiexpect eof            EOFfi   done

如有问题欢迎指正!


原创粉丝点击