期望脚本支持两个预期的字符

我想通过期望在Linux和Solaris机器上运行sshd restart(我期望在我的ksh脚本中运行)

我创build期望的脚本,所以当期望看到提示符“#”然后他运行sshd重启

但问题是,我也运行这个期望也solars和提示有“>”,所以如何创build一个支持“#”和“>”提示行

在Linux上

expect # {send "/etc/init.d/sshd restart\r"} 

solaris

  expect > {send "/etc/init.d/sshd restart\r"} 

使用全局模式: expect {[#>]}

或正则expression式: expect -re {#|>} – 正则expression式模式可以得到更详细的说明。 我build议你将锚点提示匹配到行尾。 通常提示以空格结束,所以您可以:

 expect -re {[#>] ?$} 

您可以通过检查uname -s的输出,或者通过检查以下输出来将其放入if语句中:

猫/ etc / release

猫/ etc / redhat-release

猫/ etc / lsb-release

并使用像这样的东西:

 if [[ $(uname -s) == "Linux" ]];then expect # {send "/etc/init.d/sshd restart\r"} else expect > {send "/etc/init.d/sshd restart\r"} fi 

如果语法错误,我还没有使用ksh多less年了!