Linux之expect、tcl


expect是一个自动化交互软件,可用于linux终端自动交互(模拟人与终端的交互)。它是基于tcl(一种语言l实现的软件,命令行安装命令为sudo apt install expect(apt是abuntu中一个自带的安装包管理器)

推荐在常用的shell解释器下运行expect命令(expect <

#! /bin/sh
echo "begin"
expect <# send "ls\n"  网上别人的博客使用send可以调用linux命令,我却无法成功,只能用spawn。是因为它们开启了一个新进程,我却没有?
spawn sudo pwd
expect ": "
send "362143\r"
expect eof

# 另一种在sh解释器下调用的方法,有点煞笔,不建议使用。题外话∶bin/bash和bin/sh基本没啥区别。

# #!/bin/sh
# echo "begin"
# expect -c "
# send \"11\n\"
# spawn sudo pwd
# expect \": \"
# send \"362143\r\"
# expect eof
# "