3 ms·
Having written a number of shells, I can confidentally say that the actual heart of one is the ability to execute a series of commands. Doing this in a very hi
by DougMerritt 10y ago
Having written a number of shells, I can confidentally say that the actual heart of one is the ability to execute a series of commands.
Doing this in a very high level language like Ruby is trivial; it does all the work for you.
Doing this in C, as Bash does, requires learning some systems programming. Under Unix/Linux that means using fork() to create a child process and then having that child call exec() to run some specified command.
If not run "in the background", then the parent process needs to call some variant of wait() to idle until the child is done.
If it is desired to have input and/or output different than the parent, say for redirection of stdin/stdout/stderr, then one has to fiddle with variants of dup() to arrange this.
In some sense, all of the features mentioned in the other comments are optional, and the number of features in a sophisticated shell like Bash is mindboggling, so it would be a good idea to figure out a "minimum viable product" so that you can get your minimal desired feature set working before the end of time.
- tedmiston 10y agoWhen you say trivial, do you mean (roughly)?: - get input from the user - pipe it to a subprocess [1] -- ex. subprocess.call("somecmd", shell=True) - show output to the user - run this all inside a while true loop Is it "cheating" to do it this way? Should the shell be responsible for parsing a language, generating an underlying representation like an abstract syntax tree, etc? I'm speaking from the Python perspective but of course this idea is about the same in any very high level language. [1]: https://docs.python.org/2/library/subprocess.html#using-the-subprocess-module https://docs.python.org/2/library/subprocess.html#using-the-...
- DougMerritt 10y agoYes, that is precisely what I mean by trivial; the things needed are builtin to the Ruby/Python/Perl languages + standard class library, which makes it very easy. > Is it "cheating" to do it this way? Should the shell be responsible for ... Depends on your goals. There is no absolute answer. If you just want to get things done fast, use Ruby. If you want to understand what Bash does, and learn more about systems programming, then do it the long way.