automating install.sh script using an expect script

时间秒杀一切 提交于 2019-12-23 02:06:07

问题


I have a bit of a quandary that I can't seem to figure out, and i'm sure it's easy..

I have an external shell script, we will call it "install.sh" that is used to install a piece of software that our company develops. I want to automate that install.sh process using expect (with external arguments), since it's pretty easy to pattern match on the output strings and pass in a bunch of arguments, either as an array or arguments to the expect script.

Unfortunately, many of the examples online are for "scp" or "ftp" and none of these are interesting to me, since I'm trying to automate a simple install script, and not those interactive shells, but not a "script".

Here is an example of a simple expect script that I'm trying to create:

# !/usr/bin/expect
# passed arguments
set domPassw [lindex $argv 1]
set joinDomain [lindex $argv 2]
set cdcVersion [lindex $argv 3]
set domUser [lindex $argv 4]

# cd over
cd /Users/wqcoleman/Desktop/suite-mac10.6/;

# start install
spawn ./install.sh

# first match
expect "Install the 5.1.0 package? (Q|Y|N) [Y]:"
send "Y\r"

I'm sure I could get clever here and do this within a bash script, but this is harder as far as I'm concerned and I would rather have a simple iterative script that "matches" the output and sends an input, it seems simpler to me.

I have two basic questions.

  1. When running then above, it doesn't seem to "fire" the ./install.sh script. There is nothing? nothing to match? nothing is outputted.
  2. I need to run the ./install.sh script as root, and when I initially started this, I figure spawn would have some "elevated" start, it doesn't, so I have no idea how to do this here.

I was going to run something like this:

#!/bin/bash
# Ask for the administrator password upfront
sudo -v

# Keep-alive: update existing `sudo` time stamp until `.update` has finished
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &

# cd over
cd /Users/wqcoleman/Desktop/suite-mac10.6/;

# start install
spawn ./install.sh

and then roll into expect, but that didn't work AT ALL.. so I'm not sure where to start, write a bash script and call expect inside? or write an expect script and call the install.sh with elevated prompt inside that?

Any suggestions would be most helpful.


回答1:


#!/bin/bash
.......
.......
expect -c "
spawn ./install.sh
expect \"Install the 5.1.0 package\? \(Q\|Y\|N\) \[Y\]\: \"
send \"Y\r\"
interact "
.......
.......



回答2:


Rather than focus on the expect script, you might get better help showing what inputs the install script requires. The solution might be as simple as providing the responses to install.sh on its stdin:

install.sh << END_OF_RESPONSES
Y
answer1
answer2
answer3
END_OF_RESPONSES


来源:https://stackoverflow.com/questions/14329642/automating-install-sh-script-using-an-expect-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!