Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a.command, a script that waits for user entry (like typing something and then hitting return) It loops forever-ish. For example, it tells the user if their entry is a leap year, or its a minecraft server. I get it going as the user like so:

open a.command

Trying to write a script that will periodically act like a user typing entries to that script.

I am having no luck with something like this:

i=5;
while [ $i -gt 0 ]; do
  i=$(( $i - 1 )); sleep 10; # do this once every 10 seconds for i times
  {{send text to the script a.command in the newly opened window}}
done & open a.command > a.out; 

Doesn't have to output to a.out, I was just saving the results to that file.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Instead of doing the inter process communication inside the while loop, just print to standard out and pipe the whole loop into a.command.

So your script would look something like this:

i=5;
while [ $i -gt 0 ]; do
  i=$(( $i - 1 )); sleep 10; # do this once every 10 seconds for i times
  echo "Jan 1, 1970" # just echo or print your input here
done | a.command > a.out;

Note the use of the | character instead of the & in the last line.

share|improve this answer
    
This doesn't seem to work. The a.command script opens in another terminal window, but none of the echo data shows up there. The a.command script is a simple: –  Seattleda Mar 5 at 1:17
    
c=20; while [ $c -gt 0 ]; do c=$(( $c - 1 )); echo "Type the year that you want to check (4 digits), followed by [ENTER]:" read year if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )); then echo "$year is a leap year." else echo "This is not a leap year." fi done –  Seattleda Mar 5 at 1:19
    
Hmm, is a.command a shell script or something else? You might not need to use open if it's a shell script. –  8bittree Mar 5 at 2:15
    
Its something like that script there that figures whether 4 digits is a leap year. –  Seattleda Mar 6 at 2:37
    
The eventual application of this will be to feed Save All commands to my minecraft server, which is running in a terminal window and accepts user input, just like that script. I'll be launching it with java. –  Seattleda Mar 6 at 2:38

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.