bash to c

Can someone help me with this.

How would I create this bash script in c?

cat file.txt | grep bla > out.txt
I've done a simple cat /etc/passwd but how do I know if this is working ok, because it doesn't print anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(){
        int fds1[2];
        pid_t pid;
        pipe(fds1);
        if((pid = fork())== 0){
                dup2(fds1[1],1);
                close(fds1[0]);
                close(fds1[1]);
                execl("/bin/cat","cat","/etc/passwd",NULL);
        }
}
How would I create this bash script in c?
No need to use cat. grep bla /etc/passwd > out.txt will work. Check out the man page: man grep.

As for a c program: http://www.unix.com/programming/53220-execl-redirecting-output-text-files.html
Post #3 has a solution and a good explanation.

Edit: had - "http://lmgtfy.com/?q=execl+output+to+file - Top result" Rankings can change.
Last edited on
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 ...
If you must use the shell to execute some commands, then why don't you just use the shell? Why C?
You can create a pipe using the POSIX API (which is what shell is based around...).
it has to be done like i tried in my second post
@ 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.

Step 1: Open the file. This is done with fopen(): http://www.cplusplus.com/reference/cstdio/fopen/

Step 2: Read from the file. Use fscanf(): http://www.cplusplus.com/reference/cstdio/fscanf/

Step 3: Compare the contents of the file to a string. That would be strcmp(): http://www.cplusplus.com/reference/cstring/strcmp/

Step 4: Output something. fprintf() is your solution: http://www.cplusplus.com/reference/cstdio/fprintf/

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.

I don't know if you saw this thread but I feel like I have to draw more attention to it: http://www.cplusplus.com/forum/lounge/137087/3/#msg729235 it's just too horrible to ignore.
Last edited on
Topic archived. No new replies allowed.