How to feed input to an interactive bash script from a text file

I have a script (from a provider .. for masking passwords) that goes through a series of user inputs and generates some output based on input. I would like to be able to wrap another script around it that transfers the input from a text file and then captures the output for later use. Does anyone have examples of this?

UPDATE: I did something, and it turns out that the shell script starts a Java process that asks for user input. xargs and <,>, | don't seem to work for this.

+5
source share
3 answers

myscript < input_file > output_file(from the command line) will read the line input_fileby line, as if it were entered by the user, and then write the output to output_file. However, be careful if it output_filealready exists, it will be completely overwritten without warning.

+5
source

You can try the program expectavailable as a package for most Linux distributions. It uses a simple script language to feed interactive programs. An example script could be like this:

#!/usr/bin/expect
spawn passwordmanager
expect "Enter password for testuser:"
send "verysecret123"

This script would say to expectstart the program passwordmanager, and then wait for the invitation Enter password for testuser:and respond to it with verysecret123, etc.

+1
source

function script_plus ()
{
   if [ $# != 2 ]; then echo "usage: ..."; exit; fi
   src=$1;
   tgt=$2;
   if [ ! -f $src ]; then echo "usage: ..."; exit; fi
   cat $src | xargs script > $tgt
}

, script - script. ( ). , script_plus script.

-1

All Articles