Bash script to pass arguments to a script

argumentsbash

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.

Best Answer

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

Related Question