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.
To get the output of a command as a parameter for another command you can use backticks:
or use
$()(I believe this is a more preferred way for bash):If you want to use the STDOUT of prog1 as STDIN for prog2 use the | (pipe) operator:
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 (
argvof themain()function). See How to read a line from the console in C? for an example on how to do this.