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.
No, you do that with
open()anddup()ordup2()(andclose()).No, you do that with
pipe(),dup()ordup2()and lots ofclose()calls.That’s the normal convention, yes.
Yes. In a normal shell, you can write something like:
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 usingposix_spawn()and its supporters.