5

I have a script (from a vendor.. for masking passwords) that walks 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 which feeds the input from a text file, and then captures the output for later use. Anyone have any examples of this?

UPDATE: I've done some digging, and it turns out the shell script is kicking off a java process which is what is requesting user input. xargs and <,>,| don't seem to work for this.

etsauer
  • 347
  • 2
  • 6
  • 12

3 Answers3

6

myscript < input_file > output_file (from the command line) will read input_file line by line as if it were user input, and then write the output to output_file. Be careful though, if output_file already exists, it will be completely overwritten without any warning.

Lorkenpeist
  • 1,475
  • 2
  • 13
  • 26
  • 1
    `<`, `>`, and `|` are all good tools to know. `script1 | script2` will take the output of `script1` and feed it into `script2` as input. – Lorkenpeist Mar 15 '13 at 20:44
  • `>>` is another useful tool. `myscript >> output_file` will append the output from `myscript` to the end of `output_file`. – Lorkenpeist Mar 15 '13 at 20:47
  • thanks for the response. see Updates above. It's actually a java process being kicked off by a shell script. no luck with these suggestions so far. – etsauer Mar 21 '13 at 21:00
5

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

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

This script would tell expect to launch the program passwordmanager, then wait for the prompt Enter password for testuser: and answer it with verysecret123 and so on.

dronus
  • 10,774
  • 8
  • 54
  • 80
-1

How about a shell function

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
}

Assumes 'script' is your vendor script. Does no error checking (for illustration only). If not a shell function, the body of the above script_plus could be the content of a shell script file.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 1
    You have some code problems -- for example, you have `src` where you need `"$src"` -- but more fundamentally, I don't think that `script_plus FOO BAR` is at all superior to `script < FOO > BAR`, and I don't think this is an approach worth recommending. – ruakh Mar 15 '13 at 20:29
  • Thanks, fixed with xargs. Of course 'worth recommending' changes when one needs to augment the script with error/arg checking. Input exists , for example. – GoZoner Mar 15 '13 at 20:31
  • `xargs` is definitely not what the OP needs. The existing script doesn't take these things as command-line parameters; rather, it "walks through a series of user inputs". – ruakh Mar 15 '13 at 20:37