2

I have a simple java program (think of it as Apple's Siri) that, when started from a terminal, waits for a user to type in a question and then it prints out a reply. I would like to make a bash script that starts the java program and then gives it a couple of questions (just as if a human was typing them in). This is what I tried:

#!/bin/bash
# change to project directory
cd C:/JavaProjects/VirtualButler

#compile the program
javac Alfred.java

#start the program
java Alfred

#Give it questions
echo Hey what time is it?\r
echo When is my next meeting?\r

#keep the terminal open so I can see the answer
PAUSE

However, when I run the bash script, the processing gets stuck at Java Alfred line (which is running on the open cmd, waiting for the user to type in questions and use the program. Only when I terminate the Java program, the processing moves on and gets to the questions that I want to automatically pass to the Java program. Is there a way to automatically pass these questions to the java program while it is running in the cmd?

2 Answers 2

3

Assuming bash 4.0 and up, you can use the coproc command.

# Starts Alfred in a background process with pipes in and out of it
coproc java Alfred
# Alfred's stdin is now referred to with ${COPROC[1]}
echo Hey what time is it?\r >&${COPROC[1]}
echo When is my next meeting?\r >&${COPROC[1]}
# Alfred's stdout is found with ${COPROC[0]}
cat <&${COPROC[0]}

Note that, from here, cat will hang as there is no EOL on the file descriptor. If Alfred quits running, then cat will stop. I'll leave it to you to figure out how to tell when Alfred is done and what to do then.

Sign up to request clarification or add additional context in comments.

4 Comments

This is great use of coproc +1
So, when I tried this approach earlier I simply typed in the commands on the terminal and everything works. However, when I put the code in a bash script and run the script I get an error message testScript.sh: 2: testScript.sh: coproc: not found. I have bash version 4.3.11 , but coproc seems to be unrecognizable through the bash script (even though it works perfectly when typed in on the terminal. I am probably making some syntactic mistake in the bash script, but I cannot figure out what. Can you please tell me what I might be doing wrong? Thanks!
the script that I would first like to get working is pretty much the answer that you gave me (then later I can add more to it): #!/bin/bash coproc java Alfred echo Hey what time is it? >&${COPROC[1]} echo When is my next meeting? >&${COPROC[1]} I don't need to use the cat line because the java program is logging the entire conversation. The name of the script file is testScript.sh and I run it as sh testScript.sh. Sorry about the 4 lines of code being crammed on the single line in the comment, I am not sure how to place them on separate lines in the comment :/
Well, there's your problem. If you run it with sh testScript.sh you are running it with sh, not bash. coproc is a bash builtin that you won't get in sh. Run it as bash testScript.sh or as simply testScript.sh.
2

This will redirect the input:

java Alfred < <(printf "Hey what time is it?\r When is my next meeting?\r")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.