Shell中使用Expect Here Document

在使用Shell写程序时,有时不得不面对交互的问题——可惜shell往往无法自动完成交互。如果交互的内容很多,自然是直接应用Expect脚本比较方便;如果交互的内容很少,而且考虑到Shell的易用性,在Shell脚本中创建一个Expect 的Here Document更加方便灵活。

如下例所示,Expect Here Document可以直接运行并将运行结果赋给KSH变量rcs_stat。

#!/bin/ksh

autoload formatAPnum

# Set the lab name, COOLLAB
. $COOLXDIR/.netlabs
# Get the RCS cell list
set -A RCSs
typeset -i nx=0
cat $COOLXDIR/.coolcell2dcs | while read cell
do
	if [[ "$cell" = c* ]]
	then
		rcs=${cell%%$COOLLAB*}
		RCSs[$nx]=${rcs#c}
		nx=$nx+1
	fi
done

# Check RCS status
typeset -i loopCount=0
rcs_cnt=${#RCSs[*]}
B_server=$(formatAPnum $BserverAP)
while [ "${#RCSs[*]}" != 0 -a $loopCount -lt 720 ]  # wait at most 2 hours
do
	loopCount=${loopCount}+1

	nx=0
	while [ $nx -lt $rcs_cnt ]
	do
		rcs_stat="OOS"
		rcs_stat=$(
expect - <<!
log_user 0
set timeout 20
spawn $COOL_RSH ap$B_server TICLI
send "op:cell ${RCSs[$nx]}\r"
expect {
	timeout {puts "OOS\n"}
	"*DL(S) DOWN" {puts "OOS\n"}
	"*DL(S) UP" {puts "UP\n"}
}
!
		)

		if [ "$rcs_stat" = "UP" ]
		then
			coolprint - "RCS cell ${RCSs[$nx]} is up."
			unset RCSs[$nx]
		fi

		nx=${nx}+1
	done

	[ -n "${RCSs[*]}" ] && sleep 10 
done

coolprint - "All RCSs are up."
exit 0




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值