« Back to Index

[Bash script with dynamic values for inputs using Expect]

View original Gist on GitHub

Tags: #bash #shell #expect #dynamic #script #input #password

Bash script with dynamic values for inputs using Expect.md

A bash script that will expect input from the user (e.g. we are going to be the user in this example):

#!/bin/bash
 
echo "Hello, who are you?"
 
read $REPLY
 
echo "Can I ask you some questions?"
 
read $REPLY
 
echo "What is your favorite topic?"
 
read $REPLY

We can write our own script (e.g. chmod +x ./answerbot) to use expect to respond with pre-canned responses:

#!/usr/bin/expect -f
 
set timeout -1 # disable the timeout
 
spawn ./questions # start our script using spawn command
 
expect "Hello, who are you?\r"
 
send -- "Im Adam\r"
 
expect "Can I ask you some questions?\r"
 
send -- "Sure\r"
 
expect "What is your favorite topic?\r"
 
send -- "Technology\r"
 
expect eof

Note: discovered autoexpect ./some_script.sh which will look at some_script.sh and will dynamically generate an ‘expect’ script for you