Question

I'm trying to automate running of a shell script that would take some user inputs at various points of its execution. The basic logic that I've in my mind is copied below, but this is only for one input. I wanna run it recursively until the shell prompt is received after the original script completes its execution. I said recursively because, the question that prompts for an input and the input itself will be the same all the time.

#!/usr/bin/expect
spawn new.sh $1
expect  "Please enter input:"
send "my_input"

Sharing any short-cut/simple method to achieve this will be highly appreciated.

Was it helpful?

Solution

You don't need expect to do this - read can read from a pipe as well as from user input, so you can pass the input through a pipe to your script. Example script:

#!/bin/bash
read -p "Please enter input: " input
echo "Input: $input"

Running the script prompts for input as normal, but if you pipe to it:

$ echo "Hello" | sh my_script.sh
Input: Hello

You said that your input is always the same - if so, then you can use yes (which just prints a given string over and over) to pass your script the input repeatedly:

yes "My input" | sh my_script.sh

This would run my_script.sh, any read commands within the script will read "My input".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top