Results 1 to 8 of 8
I am trying to execute the shell command:
csh -f -c ls *.cpp
and I tried the follwong code:
Code:
char *arg[]={"-f","-c ls *.cpp",(char *)NULL};
execv("/bin/csh",arg);
perror("execv error");
and also ...
- 11-26-2009 #1Just Joined!
- Join Date
- Nov 2009
- Posts
- 4
execv and csh in c++
I am trying to execute the shell command:
csh -f -c ls *.cpp
and I tried the follwong code:
and also this code:Code:char *arg[]={"-f","-c ls *.cpp",(char *)NULL}; execv("/bin/csh",arg); perror("execv error");
and none of them worked. both times the program exited without displaying anything.Code:char *arg[]={"csh","-f","-c ls *.cpp",(char *)NULL}; execvp(arg[0],arg); perror("execvp error");
(the complete code also uses fork() before executing)
what is wrong with the code?
- 11-27-2009 #2Just Joined!
- Join Date
- Feb 2006
- Posts
- 13
Can you try the same with system() call
- 11-27-2009 #3Just Joined!
- Join Date
- Nov 2009
- Posts
- 4
It's part of homework and I am not allowed to use system()
- 11-27-2009 #4Just Joined!
- Join Date
- Nov 2009
- Posts
- 53
execvp question...
Hi,
Please read the man execvp() carefully.
The arg list should perhaps be "-f" "-c" "ls ...." and null terminated.
Not sure what csh would make of a single arg "-c ls..." as opposed to two args "-c" (exec the following command) "ls..." (list some things).
My experience with c shell is that it is mainly an exercise in bug-avoidance.
execvp "/bin/ls" "*.cpp" "" should also do the trick.
- 11-27-2009 #5Just Joined!
- Join Date
- Nov 2009
- Posts
- 4
-c "ls *.cpp" doesn't work as well. cant use execvp "/bin/ls" "*.cpp" ""
(ls was just an example... it's not only for ls)
- 11-27-2009 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The exec functions terminate the current process and start the new one. If you want perror() to run, you need to first fork() your program.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-27-2009 #7Just Joined!
- Join Date
- Nov 2009
- Posts
- 4
if exec fails then it returns and perror will run. as I said the actual program uses fork(). this is not the problem. exec runs successfully but does not display anything
- 11-30-2009 #8Just Joined!
- Join Date
- Nov 2009
- Posts
- 3
execv
Try
Comments:Code:char *arg[]={ "/bin/csh","-f","-c","ls *.cpp",NULL }; execv(*arg, arg); perror("execv error");
- C and C++ programs expect the 1st arg to be the command name, thus the "bin/csh" in the args list
- execv passes the arguments as a list, thus "-c" is an arg by itself (when you enter a shell command, the shell does the work of splitting the arguments and then call the program with an arg list)
- "ls *.cpp" is also one arg, as you would provide it to a shell. 'csh' will interpret the argument and expand the "*.cpp".
Make sure you have /bin/csh, and that you have at least one ".cpp" file in your current dir to see something


Reply With Quote