3 ms·
One step at a time. Some of the things people expect of a shell: - can search $PATH for things to run (traversing $PATH), - ?must check 'executable' bit? - ?
by Someone 10y ago
One step at a time. Some of the things people expect of a shell:
- can search $PATH for things to run (traversing $PATH),
- ?must check 'executable' bit?
- ?must discriminate scripts from binaries?
- must exec things to be run
- must intercept stdin, stdout, and stderr of things being run
- pass arguments to programs
- expand wild card characters (for UNIX shells. A in some sense more reasonable alternative is to put wildcard expansion in a separate library and let executables do wildcard expansion. That would prevent problems with expanded command lines becoming too long)
- allow for back tick substitutions
- evaluate environment variables when interpreting arguments (again, this _could_ be something left to programs to do. For this, I don't see a benefit, though)
- allow multiple commands on a line
- allow redirection from and to files
- build pipelines and execute them
- has some builtins (mostly optional, to speed up things)
- reading and interpreting .shellrc
- control structures (if, for, etc.)
You can implement them in whatever order you want.
I would probably start like this:
- a program that starts a single executable.
- starting an executable and intercepting its output.
- also sending it input
- accept the name of the command to run on the command line, search for it, and execute it.
- allow sending arguments to the program to run
- loop infinitely, running the executables whose names the user types in (this is where your program becomes shell-like)
You will likely encounter many things I let out above (do you need to interact with the terminal? How to handle Control-C, for example to nicely kill all commands in a pipeline) or do not even know about, but I would tackle them when I met them.