0

I'm trying to write a shell that will eventually take advantage of concurrency. Right now I've got a working shell parser but I'm having trouble figuring out how to execute commands. I've looked a bit at exec (execvp etc.) and it looks promising but I have some questions.

Can exec handle file input/output redirection? Can I set up pipes using exec?

I'm also wondering about subshells. What should subshells return; the exit status of its last statement? Can subshells be a part of a pipe?

These might seem like really dumb questions but please bear with my inexperience.

1
  • As a reference book for these sorts of basic Unix programming questions, I recommend Michael Kerrisk's The Linux Programming Interface. In fact, sections 24.2.1 and 27.4 of that book (pages 517-520, 575-578) answer your question about standard I/O redirection. Commented Jan 23, 2013 at 6:42

2 Answers 2

1

Can exec handle file input/output redirection?

No, you do that with open() and dup() or dup2() (and close()).

Can I set up pipes using exec?

No, you do that with pipe(), dup() or dup2() and lots of close() calls.

I'm also wondering about subshells. What should subshells return, the exit status of its last statement?

That's the normal convention, yes.

Can subshells be a part of a pipe?

Yes. In a normal shell, you can write something like:

(cd /some/where; find . -name '*.png') | sed 's/xyz/prq/' > mapped.namelist

If you want to get scared, you could investigate posix_spawn() and its support functions. Search for 'spawn' at the POSIX 2008 site, and be prepared to be scared. I think it is actually easier to do the mapping work ad hoc than to codify it using posix_spawn() and its supporters.

Sign up to request clarification or add additional context in comments.

3 Comments

Why does it seem like there are so many ways to do the same thing in C hahah. So if a subshell appears to the right of a pipe, does that mean the output of the command on the left becomes the input of the first command in the subshell? Just make a blind guess.
Oh and for some reason I can do this.. ls > file | cat and yet there's no output. I don't even know what that means, I expected a syntax error. Sorry to bug you with a bunch of dumb questions I don't really know where else to ask hahah.
ls sends its output to the file; there is nothing appearing on the pipe for cat to write.
1

The standard technique for a shell is to use fork-exec. In this model, to execute an application the shell uses fork to create a new process that is a copy of itself, and then uses one of the exec variants to replace its own code, data, etc. with the information specified by an executable file on disk.

The nice thing about this model is that the shell can do a little extra work with file descriptors (which are not thrown away by exec) before it changes out its address space. So to implement redirection, it changes out the file descriptors 0, 1, and 2 (stdin, stdout, and stderr, respectively) to point to another open file, instead of console I/O. You use dup2 to change out the meaning of one of these file descriptors, and then exec to start the new process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.