Find the answer to your Linux question:
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 ...
  1. #1
    Just 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:
    Code:
      char *arg[]={"-f","-c ls *.cpp",(char *)NULL};
      execv("/bin/csh",arg);
      perror("execv error");
    and also this code:
    Code:
    char *arg[]={"csh","-f","-c ls *.cpp",(char *)NULL};
      execvp(arg[0],arg);
      perror("execvp error");
    and none of them worked. both times the program exited without displaying anything.

    (the complete code also uses fork() before executing)
    what is wrong with the code?

  2. #2
    Just Joined!
    Join Date
    Feb 2006
    Posts
    13
    Can you try the same with system() call

  3. #3
    Just Joined!
    Join Date
    Nov 2009
    Posts
    4
    It's part of homework and I am not allowed to use system()

  4. #4
    Just 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.

  5. #5
    Just 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)

  6. #6
    Linux Guru Rubberman's Avatar
    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!

  7. #7
    Just 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

  8. #8
    Just Joined!
    Join Date
    Nov 2009
    Posts
    3

    execv

    Try
    Code:
    char *arg[]={ "/bin/csh","-f","-c","ls *.cpp",NULL };
    execv(*arg, arg);
    perror("execv error");
    Comments:
    - 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...