1

I have a shell script that requires input during runtime. Is it possible to echo input into the shell script during the exec()?

IE in a normal shell: $ ./script.sh

Input: Whats your favorite color: [Type your answer] Red

Output: Your favorite color is Red.

3
  • you really want to do that with a shell script? Commented Jul 12, 2012 at 20:50
  • 1
    The Shell scripts have already been developed and are used to test external hardware and requires a keypress to move to the next test. I think I can find a way around doing it this way, however, I was just curious if it was possible. I will probably end up splitting each test into its own script and just output the result. Commented Jul 12, 2012 at 20:54
  • so nothing to do wit my favourite colour then - sigh Commented Jul 12, 2012 at 20:55

1 Answer 1

3

I'm not sure exactly which side you are asking for, but I will answer both anyway.

From PHP, you can read user input from the shell. It can be done using the readline() function or simply using streams. Depending on the PHP version you are using and the environment, it can be either php://input or php://stdin. You can simply fopen() on those and read as usual.

If you want to interact with a shell script you are calling, you will need to use proc_open() rather than exec. It is a little more trouble, but there are no ways around. The function will provide you with separate streams for input, output and error, you can read them as needed to do your task.

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

2 Comments

proc_open sounds like what I am looking for. I basically want to be able to run a shell script, view the output, enter dynamic input, repeat.
Yes, this is what you need. It would be better to use arguments to the shell function when possible as it is more stable, but there are cases where it is just not an option.

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.