execl() + redirecting output to text files


 
Thread Tools Search this Thread
Top Forums Programming execl() + redirecting output to text files
# 1  
Old 02-13-2008
execl() + redirecting output to text files

Im currently using execl() to run the ls command and redirect the output to a text file. Unfortunately, when I check the text file, no information has been written to it.

I am calling execl() with the ls command like this

Code:
execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 );

What are some possible causes of my above problem ? cheers
# 2  
Old 02-13-2008
Is that the right syntax for execl?

Quote:
Originally Posted by man execl
execl(<shell path>, arg0, file, arg1, ..., (char *)0);

where <shell path> is an unspecified pathname for the sh utility, file is the process image file, and for execvp(), where arg0, arg1, and so on correspond to the values passed to execvp() in argv[0], argv[1], and so on.

The arguments represented by arg0,... are pointers to null-terminated character strings. These strings shall constitute the argument list available to the new process image. The list is terminated by a null pointer. The argument arg0 should point to a filename that is associated with the process being started by one of the exec functions.

The argument argv is an array of character pointers to null-terminated strings. The application shall ensure that the last member of this array is a null pointer. These strings shall constitute the argument list available to the new process image. The value in argv[0] should point to a filename that is associated with the process being started by one of the exec functions.

The argument envp is an array of character pointers to null-terminated strings. These strings shall constitute the environment for the new process image. The envp array is terminated by a null pointer.

For those forms not containing an envp pointer ( execl(), execv(), execlp(), and execvp()), the environment for the new process image shall be taken from the external variable environ in the calling process.

The number of bytes available for the new process' combined argument and environment lists is {ARG_MAX}. It is implementation-defined whether null terminators, pointers, and/or any alignment bytes are included in this total.


# 3  
Old 02-16-2008
Quote:
Originally Posted by JamesGoh
What are some possible causes of my above problem ? cheers
The problem is: you are giving ls something that actually is the shell's responsability, and that thing is output redirection. Since there is no shell involved in your program, you should perform the redirection of the output by yourself, that is, your program should do the shells's work, first by openning a new file and them redirecting its standard output and standard error to the new file descriptor using the dup2(2) function.


PHP Code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(int argcchar *argv)
{
int fd/*file descriptor to the file we will redirect ls's output*/

if((fd open("dirlist.txt"O_RDWR O_CREAT))==-1){ /*open the file */
  
perror("open");
  return 
1;
}

dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it more  */

/*execl ls */
execl"/bin/ls" "ls" "-la" , (char *) );

return 
0;

Hope you got the point Smilie
# 4  
Old 02-17-2008
Hi guys

I ended up using system() instead and it resolved my probs (although I had to use a lot of strcmp() calls to create the command Smilie ) of redirecting shell output to a file. Another reason why I chose to reject execl() in favour of system() was because I didn't really understand the manual description of it too well (apart from its purpose of course)

pflynn, thanks for your suggestion. Just a question (which may be a bit stupid so bear with me). The standard output is the place where all output of any c program is directed to (i.e. the place where all printf() calls have the text displayed ) ?
# 5  
Old 02-17-2008
Quote:
Originally Posted by JamesGoh
The standard output is the place where all output of any c program is directed to (i.e. the place where all printf() calls have the text displayed ) ?
Yes, you are right!

Quote:
Standard output (stdout)

Standard output is the stream where a program writes its output data. The program requests data transfer with the write operation. Not all programs generate output. For example the file rename command (variously called mv, move, ren) is silent on success.

Unless redirected, standard output is the text terminal which initiated the program.

The file descriptor for standard output is 1 (one); the corresponding <stdio.h> variable is FILE* stdout; similarly, the <iostream> variable is std::cout.
Pay attention to this part:

Quote:
Unless redirected, standard output is the text terminal which initiated the program.
which is exactly what you want to do: you want to open a file so the ls program would write its output to this file instead of writing it to the terminal that's originally associated to the standard output.

More information on the standard streams:

Standard streams
Standard streams - Wikipedia, the free encyclopedia
# 6  
Old 02-18-2008
thanks heaps pflynn !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting the results to different output files

Hi All, I am trying a shell script and need your help on taking the results to different output files. I have tried the below code: nawk ' {CNF = (length()-10)/7 printf "%9s", substr ($0, 1, 9) for (i=0; i<=CNF; i++) T = substr ($0, 10+i*7, 7) TMP = 100 - (T + T + T + T + T + T + T + T... (24 Replies)
Discussion started by: am24
24 Replies

2. Shell Programming and Scripting

Awk: Print Error While Redirecting output in multiple Files

Hi, I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") { array1++ array2++ array3++ } else if (substr($1,26,2)=="03" &&... (4 Replies)
Discussion started by: siramitsharma
4 Replies

3. Post Here to Contact Site Administrators and Moderators

Redirecting grep output to multiple files

Hi All, I am trying to redirect the grep output to multiple files, can you please help with that. Below is the command im using to match my pattern grep \<proxyType\>$PxyType $DIR/EndureFiles.json > File_Name*.json Note : $DIR and $PxyType is already defined in my script Im able... (0 Replies)
Discussion started by: Deena1984
0 Replies

4. Shell Programming and Scripting

Redirecting the output

For example, if we run the below command, symcfg list -thin -pool , results in an output most of the times and if the out is generated i'm able to redirect the output to a file. but sometimes it doesnt result any output and even though the output is being redirected, i can see "No Thin Pools "... (2 Replies)
Discussion started by: web2moha
2 Replies

5. UNIX for Dummies Questions & Answers

redirecting script output

Hello, I am interested in taking the output from a script i wrote and using it as input to a different script i wrote. So for example i want to take the output from program2 and use it as a parameter for program1. I didnt think i could use the >> symbols because i think that is just for .txt... (4 Replies)
Discussion started by: GmGeubt
4 Replies

6. Red Hat

write execl output to file

hi, how to write the resullt of execl() into another file. i used write(fd,execl()); (1 Reply)
Discussion started by: Mahendravarma
1 Replies

7. Shell Programming and Scripting

Redirecting to different output files with awk.

Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting. So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls. #!/bin/bash #some variables... (2 Replies)
Discussion started by: mikesimone
2 Replies

8. Shell Programming and Scripting

Redirecting echo output to 2 flat files

Hello all, I have the following 2 questions.. 1) I would like to capture the output of an echo command to 2 different files at the same time. This does not seem to work. Any ideas? echo ==== started on `date` ==== >> res1.log res2.log 2) Would it be possible to just get the 5th... (2 Replies)
Discussion started by: luft
2 Replies

9. Shell Programming and Scripting

Redirecting OUTPUT

Hi, I want to move the output of a command/script to a file as well as to to be displayed on stdout. Can anybody help me in this. Thanks in advace .. -Chanakya M (1 Reply)
Discussion started by: Chanakya.m
1 Replies

10. UNIX for Dummies Questions & Answers

Redirecting output to multiple log files?

If I wanted to redirect output to multiple log files, what would be the best way to do that? echo "Unix is awesome" >>unixgod.log >>unixgod.log (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question