Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
You may find this helpful: Automate input procedure to a binary file – steeldriver yesterday
    
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. – Lily Sharpton yesterday
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 yesterday
    
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. – Lily Sharpton yesterday

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

share|improve this answer
    
Thanks Patrick. Yes I have been going through this stuff and I guess that should be the solution. – Lily Sharpton yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.