先写一个读取交互式输入的脚本,然后用这个脚本自动化演示:

#!/bin/bash
#文件名:interactive.sh
read -p "enter number:" no;
read -p "enter name:" name
echo you have entered $no , $name
按照下面的方式向命令自动发送输入:
$echo -e "1\nhello\n" | sh ./interactive.sh
you have entered 1,hello
 
如果输入的内容比较多,我们可以用echo -e 来生成输入序列,那么可以用单独的输入文件结合重定向操作符来提供输入。
$ echo -e "1\nhello\n" > input.data
$ cat input.data
1
hello
 
制作输入文件后,我们可以不用echo命令:
$ sh ./interactive.sh < input.data
这种方法是从文件中导入交互式输入数据。