I don't see a whole lot of purpose to a program that just starts another program. There are definitely more simpler ways to accomplish this than busting out a low-level system programming language compiler.
How would I create this bash script in c?
Do what, exactly?
1. "Create a script file"? Write the text into a file so that the result is:
#!/bin/sh
grep bla file.txt > out.txt
However, writing a C program in order to write a text file that you could write with any editor (or with plain echo) seems overkill. However, if the C program will do other things too and will spit out not just one script file, but many (for example to be submitted to a batch queue in a cluster), it might make sense.
2. "Run other programs"? That has been discussed already; it seems a pointless complication with the current example.
3. "Perform the same operations as the script does"? This would make sense (although not in such a simple case).
open a file "file.txt" for reading
open a file "out.txt" for writing
WHILE you can read a line from file.txt
IF line contains "bla", write it to out.txt
This doesn't help my problem. I MUST create a pipe that will execute the same as bash command. So at the end I MUST call execl. The problem is only how do I get to execl. I don't need a c program that uses open, read ...
@ OP: I'm a Windows guy so I'm sorry if I misunderstand but you're searching a file for text right? Why tie your self down to a POSIX shell at all? Everything you need is in stdio.h, which you've already included, and string.h.
The only reason I can think of for you to call execl() is to run the command in the security context of the files owner. That is something I can't help you with. Everything else is pretty straight forward though. EDIT: Does this have to be C by the way? It's just easier to work with streams and text in C++.
@ NoXzema: Regarding this comment:
I don't see a whole lot of purpose to a program that just starts another program.