1

I have two programs (Prog1.c and Prog2.c) written in C and each of them take one command line argument.

Prog1.c takes a file name as argument , reads content from file and outputs it on the STDOUT (standard output) screen. Prog2.c takes data as argument and does some operation. I want to redirect output of Prog1.c to Prog2.c as input.

I have tried following bash script which gives me error

#!/bin/bash
prog2 "`prog1 file.txt`"  

I have also tried without quotes and in both cases, it gives me following error.

Prog2:: argument list too long.
5
  • There seems to be a confusion for Prog2 between 'takes data as arguments' and what seems more correct to me 'reads data on STDIN', because you WILL run out of command line buffer space pretty fast if Prog1's output is large enough. Commented Dec 3, 2012 at 9:32
  • Visit [This Link][1] , It may help you. Same questions like yours. [1]: stackoverflow.com/questions/8593939/… Commented Dec 3, 2012 at 9:35
  • I have read that whole thread and it does not solve that problem. Hope you understood the difference between questions of two threads. Commented Dec 3, 2012 at 9:37
  • I have tried accepted solution of that thread, it does not work, that's why I posted another question. Commented Dec 3, 2012 at 9:38
  • 1
    and why -2? Whats wrong with the question? Commented Dec 3, 2012 at 9:41

3 Answers 3

3

To get the output of a command as a parameter for another command you can use backticks:

prog2 "`prog1 file.txt`"

or use $() (I believe this is a more preferred way for bash):

prog2 "$(prog1 file.txt)"

If you want to use the STDOUT of prog1 as STDIN for prog2 use the | (pipe) operator:

prog1 | prog2

Note: When you want to use pipes, you need to modify the code of prog2, as it needs to read from STDIN instead of the command arguments (argv of the main() function). See How to read a line from the console in C? for an example on how to do this.

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

Comments

2

PIPES!!!!!

On the command line (or in your script) do prog1 file.txt | prog2

Comments

1

You must use backticks in both places before and after prog1

prog2 "`prog1 file.txt`"

When you get "argument list too long", you have reached the system limit for the command line. You must then find another way to send the data to prog2. You can read from stdin instead, and pipe the output of prog1 to prog2

prog1 file.txt | prog2

When you pipe the data from prog1 to prog2, you must change prog2 and read from stdin instead of using argv.

Example reading line by line:

char buf[1024];
while (fgets(buf, 1024, stdin)) {
    /* process line in buf */
}

Example reading white space delimited words

char buf[1024];
while (scanf("%s", buf) != EOF) {
    /* process word in buf */
}

3 Comments

Thanks for pointing it out. I actually used `` in my script. It was typo only here.
Tried both ways. They dont work together. I have tested one by one and they work.
@Junaid Look at the modified answer.

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.