Thread: basic shell command execution

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    basic shell command execution

    In the code on the following page:
    http://www.cs.cf.ac.uk/Dave/C/section2_22_22.html
    a basic UNIX shell is represented. Why does commands such as rm, mv, mkdir, rmdir, ls, etc can be executed but many other ones such as cd, or & (background processing) have to be implemented in the code? i.e. the code doesn't define rm or mv but the system understands them when they're typed in this shell, but that's not the case with cd.

    Please advise.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    execvp replaces the current process image with that of a different program.

    rm, mv, mkdir, rmdir, ls, etc are all programs. You can find them in /usr/bin
    cd and background processing are not programs, they are shell builtins.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Thanks. So if execvp is replaced by execv, then this case will no longer be valid? i.e. those commands won't be recognized and need to be implemented inside the code?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The difference between execv and execvp is just that execvp uses your $PATH variable for file lookup, and evecv does not.

    So while you can do execvp("ls", NULL);
    You have to do execv("\usr\bin\ls", NULL); to get the same effect.

    None of the exec functions will perform cd unless you write a cd program (I think a script will work). If you want background processing, you have to implement it yourself.

    Think of it this way.... this program you've posted is it's own shell. You can't expect it to use bash builtins any more than you would expect csh to use bash builtins.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    thanks. very useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shell script basic question
    By PedroTuga in forum Linux Programming
    Replies: 1
    Last Post: 09-09-2006, 03:24 AM
  2. A basic shell question
    By AngKar in forum C# Programming
    Replies: 1
    Last Post: 06-20-2006, 05:18 PM
  3. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  4. Basic Shell Script-Very Frustrating
    By kwigibo in forum Linux Programming
    Replies: 8
    Last Post: 05-19-2002, 02:43 AM