6

I don't have much experience with bash scripting. What I am trying to do here might not be complicated but I just have some issues.

So I have a C script that I have to use to run on approximately 70,000 files. The script invoked certain inputs for me to enter one by one. e.g. it prompts me to enter (y/n) for yes or no 7 times one after the other. so for example, if I run the script on one file, it would prompt me to enter yes or no for something followed by multiple prompts asking (y/n). In this case there are 7 y/n prompts. It then goes on to prompt several other parameters that I enter once they are prompted e.g. c for continue, 100 for percentage etc.

All these paramters are the same for all the 70,000 files. I was wondering if someone can guide me on how to pass all these arguments to the bash script once they are prompted. Pretty basic but this is what I have so far:

var='testdir/*'
for i in var; do
    /script $i
done

the above bash commands invoke the script but prompt me to input all those parameters for each file. Note: I can not manipulate the underlying script.

4
  • You may find this helpful: Automate input procedure to a binary file Oct 26, 2016 at 2:54
  • Thanks steeldriver, I think that is useful when my program reads a standard output. In my case it reads the arguments from the terminal Anyhow I tried this thing but it only reads the first argument and then stops. Oct 26, 2016 at 3:15
  • 2
    @LilySharpton Are you sure your program is definitely reading from the terminal and not stdout (which just happens to be the terminal when a program is ran from inside a terminal, unless redirection is involved)? In other words, have you tried a simple printf 'yes\ny\ny\ny\ny\ny\ny\ny\n' | script $i or whatever, to see if your script definitely still prompts for input on the terminal instead of first accepting those first "yes" and "y" inputs? If you have definitely verified it uses the terminal it's attached to, then using expect, per the only current answer, is probably the easiest.
    – mtraceur
    Oct 26, 2016 at 5:14
  • Thanks mtraceur. Yes I have tried this but the program only takes input from terminal. A procedure like this starts the program but it keeps asking me for input even though I have it mentioned in the line. Oct 26, 2016 at 14:57

1 Answer 1

9

This is a job for expect.

Lets say your C program outputs something similar to the following:

question 1? (y/n):
question 2? (y/n):
enter some percent value:
bunch of stuff
confirm? (y/n):

You would write an expect script such as:

#!/usr/bin/expect

foreach file [glob /path/to/70k/files/*] {
  spawn "/path/to/c_prog" $file

  expect "question 1? (y/n): " { send "y\r" }
  expect "question 2? (y/n): " { send "n\r" }
  expect "enter some percent value: " { send "100\r" }
  expect "confirm? (y/n): " { send "y\r" }
  interact
}

Then either chmod +x the script and run it, or expect /path/to/script

2
  • Thanks Patrick. Yes I have been going through this stuff and I guess that should be the solution. Oct 26, 2016 at 4:15
  • I've never used expect - the man page is so forbidding. It's nice to have a simple example to refer to.
    – Joe
    Oct 28, 2016 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .