需求:一键安装程序,需要用到expect自动交互。
#!/bin/bash
basedir=`cd $(dirname $0); pwd -P`
cd $basedir
function YumDokcer(){
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker.service
systemctl enable docker.service
docker pull golang:alpine
docker pull alpine
}
rpm -qa |grep docker &>/dev/null
if [ $? -ne 0 ];then
YumDokcer
fi
if [ $? -ne 0 ];then
echo 'Dcoekr build error !!!'
exit 1
fi
docker image ls|awk -F " " '{ print $1 }'|grep golang &>/dev/null
if [ $? -ne 0 ];then
docker pull golang:alpine
fi
if [ $? -ne 0 ] ;then
echo 'docker pull golang:alpine error !!!'
exit 1
fi
install_file=`ls -l bcb-node_*.tar.gz| grep ^[^d] | awk '{print $9}'`
if [ ! -f "$install_file" ];then
echo 'The installation file does not exist !!!'
exit 1
else
tar xf $install_file
fi
rpm -q expect &> /dev/null
if [ $? -ne 0 ] ;then
yum install -y expect
fi
if [ $? -ne 0 ] ;then
echo 'Install expect error !!!'
exit 1
fi
#expect install.expect
/usr/bin/expect << EOF
set timeout -1
spawn /bin/bash setup.sh
expect {
"#?" {send "2\r";exp_continue}
"node or FOLLOWER to follow:" {send "earth.testchain.io \r";exp_continue}
"#?" {send "2\r";exp_continue}
"nodes or FOLLOWERs to follow:" {send "earth.testchain.io \r"}
}
expect eof
EOF