-1

EDIT: I'm re-writing this because the first time was a bit unclear.

Let's say I have a program (an executable) such that when I run it, it prompts me to enter an input.

For example, I execute ./myProgram

and the program prompts: Please enter your username:

Here, I would type in my username.

Now, how would I write a bash script so that after I start the above program, I can enter inputs to it?

Something along the lines of this:

#!/bin/bash

path/to/myProgram

# And here I would enter the commands, such as providing my username

Thanks

6
  • 1
    read -p 'Please enter your username: ' u Commented Dec 13, 2015 at 17:29
  • You need expect for that, well, that's the easy way to do it anyway Commented Dec 13, 2015 at 17:56
  • If your script starts an interactive program, that interactive program still has I/O access to the terminal where you started the script. You don't need to do anything. Commented Dec 13, 2015 at 19:34
  • 2
    Or do you mean this? stackoverflow.com/questions/23174849/… Commented Dec 13, 2015 at 19:35
  • 1
    Possible duplicate of Using Bash Script to feed input to command line Commented Oct 20, 2017 at 9:23

2 Answers 2

0

reading values interactively is rather uncommon in *nix scripts, and is frowned upon by those who want to do exactly what you're trying to do. The standard way of doing this would be changing myProgram to accept arguments. At that point it's trivial to do this.

If you really need to use this pattern you need to use some tool like expect, as pointed out by @EricRenouf.

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

Comments

0

If myProgram reads from standard input, you can use a here-document:

path/to/myProgram <<\END
username
more input if needed
END

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.