Reading In and Exec


 
Thread Tools Search this Thread
Top Forums Programming Reading In and Exec
# 1  
Old 03-13-2008
Reading In and Exec

(using C) I'm getting really frustrated because I feel what I'm trying to do is easy as pie but I can't think of it. I'm reading in a file that contains a list of commands (ie- wc -cl ls, etc), one per line, and I need to execute those commands on the cwd. I'm used to C++ so I'm having problems reading in the commands properly. And the exec that I'm using I'm pretty sure isn't the right one. I've tried other exec's but they give me errors.

int count;
int fin;
char test[10];

if ( (fin = open ( argv[1], O_RDONLY)) == -1) {
printf("\nError in opening file:\n");
perror ("Error:");
close(fin);
return 1;
}

while ((count=read(fin, test, 256)) > 0 ) {
printf ("%s\n", test); //used this to show something was read in
}
exec (test, cwd) > out.txt //exec the commands read on cwd, send
//output to out.txt
# 2  
Old 03-14-2008
Quick example using a file with tab separated command and single argument. untested, but should get across the basic idea.

Code:
int main(int argc, char **argv<) {
pid_t chld;
char line[256];
char cmd[256], arg[256];
FILE *fd;

                    if (argc != 2) {printf("Error: You must provide a filename for this program.\n"); return -1;}
                    
                   if ( (fd = fopen(argv[1],"r")) == NULL) {return -1;}
                   while (fgets(line,256,fd) != NULL) {
                           if (sscanf(line,"%s\t%s",cmd,arg) != 2) {continue;}
                           if ( (chld = fork()) == 0) {
                             execl(basename(cmd),cmd,arg,NULL);
                           } else if (chld > 0) {
                              wait(NULL);
                           }
                           chld = 0;
                    }
                   fclose(fd);
                   return 0;
}

# 3  
Old 03-14-2008
Simpler version would be to use the system() call.
If it makes senese to do this, and the file you are reading has the execute attribute set
you could
Code:
system("/path/to/filename");

# 4  
Old 03-14-2008
Yes. System performs the gyrations in my example and more transparently. Good catch Jim.
# 5  
Old 03-14-2008
Thanks guys. I think I was using too few arguments in my exec statement and I think I swapped some arguments in the fgets. you guys cleared up a lot of confusion, Thanks a lot!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

2. UNIX for Advanced & Expert Users

Using -exec with and without -name

Hi, I need to delete the last N days file using find. I am trying to use find . -mtime -10 -print which lists down required files. but when i use find . -mtime -10 -exec ls -lrt {} \; it gives me all files in the directory including the required files but the required files... (7 Replies)
Discussion started by: v_m1986
7 Replies

3. UNIX for Dummies Questions & Answers

exec

Hi, i don't understand this part of one script exec >> $Log_File 2>&1 (1 Reply)
Discussion started by: messi777
1 Replies

4. UNIX for Advanced & Expert Users

-exec cp

Hi, on AIX 6.L I want to copy the result of grep -v to test directory then : `hostname`@oracle$ls -l | grep -v RINT -exec cp {} test grep: can't open -exec grep: can't open cp grep: can't open {} test:°`. Can you help me ? Thank you. (3 Replies)
Discussion started by: big123456
3 Replies

5. Shell Programming and Scripting

exec command help

All, I am using below shell script to output the content to outputfile.txt. What I am looking for is in addition to outputfile.txt, I want the output to be on standard output too. exec > outputfile.txt echo "Starting " echo "ending" (5 Replies)
Discussion started by: basic_shell
5 Replies

6. Shell Programming and Scripting

Help with use of `` vs exec

Hi all, I had an issue regarding use of `` or exec in perl . `` are considered to be unsafe. Why? In my case an user would be giving some parameters as input and I will form an command of it and execute it using ``. It is important to capture output as i have to parse the output. As well as I need... (0 Replies)
Discussion started by: bharadiaam
0 Replies

7. Shell Programming and Scripting

using -exec in a script

I am using a third party job management program called Autosys. the command to load a jil into the autosys database is jil < somefilename.jil I have a directory and it in are a lot of jils. rather than type jil < somefilename.jil for every file I would like to script something do do it. if cd... (2 Replies)
Discussion started by: jayjabour
2 Replies

8. UNIX for Advanced & Expert Users

exec

I have read that exec "replaces the current process with a new one". So I did $ exec ls and after this executed, my shell disappeared. I am assuming that my shell had PID xyz, and when I did exec ls, this ls got pid xyz, and when it terminated, there was no more shell process running, and... (5 Replies)
Discussion started by: JamesByars
5 Replies

9. Programming

reading reading data from webpage

hi iam reading data from web page using request socket and curl socket. now my problem is some the web page containg data as a image so how can i read the data from a image. thank,inadvance. sree (3 Replies)
Discussion started by: phani_sree
3 Replies

10. Shell Programming and Scripting

exec

In exec function say when i would like to remove the files exec rm{}\; Why is this "\" needed immediately after {} and what if i dont give it? TIA, Nisha (1 Reply)
Discussion started by: Nisha
1 Replies
Login or Register to Ask a Question