0

Hi have an easy C program that does the following when I run it with ./a.out

Please enter your name:

What I want to do, is I want to automatically pass the name without having to wait for the prompt. I tried ./a.out Sam, but it would still prompt for a name. I'm trying to write a bash script that would do that.

Any help would be appreciated. Note: I don't want to modify the C source. All I want to do is be able to pass the first argument without having to wait for the prompt.

1
  • In the future, if you want your program to take an argument from the command line, use the argc/argv parameters in int main( int argc, char **argv ). argc is the total number of arguments on the command line (including the command that invokes the program), argv[0] is the command you used to invoke the program, and argv[1] through argv[N-1] are the additional arguments you passed on the command line (represented as text strings). Commented Sep 8, 2015 at 16:05

1 Answer 1

4

The program reads from stdin. You can pipe to stdin:

echo "Sam" | ./a.out
Sign up to request clarification or add additional context in comments.

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.