I want to create a mini shell for UNIX just to know the ins and outs of everything. I am having some confusions understanding things that I used to get for granted. This is kinda philosophical question. When I creating a "shell", I assume I have a UNIX with no shell, so what would be the std in and std out in this case? doesnt functions like system() and exec() use the shell to execute programs, so if I am creating a shell in the first place. How do these functions work?
相关问题
- How to get the return code of a shell script in lu
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
- “command not found” errors in expect script execut
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- Generate disk usage graphs/charts with CLI only to
There are several functions in the
exec
family:execve(2)
,execl(3)
,execle(3)
,execlp(3)
,execv(3)
,execvp(3)
. The first one,execve(2)
is provided by the operating system kernel as a system call. (Well, okay, the function that programs call is provided by the system C library, but it is a simple wrapper around the system call.) The other functions provide slightly different semantics and are implemented in terms of theexecve(2)
function.The shells could use
execvp(3)
orexeclp(3)
to provide thePATH
search for executables, but at leastbash(1)
hashes the full pathname of executables to provide a performance benefit. (Seebash(1)
built-inhash
for details.)system(3)
is implemented via/bin/sh -c
, as you've surmised.The standard input and output is set up by whichever program spawned the shell. If a user logs in on the console directly, it'll be handled by
agetty(8)
ormgetty(8)
or whichevergetty
-alike program handles direct logins. If a user logs in viasshd(8)
, thensshd(8)
is in charge of creating thepty
and delegating the terminal slave to the shell. If a user creates their shells viaxterm(1)
or other terminal emulators, then those processes will be responsible for hooking up the standard input, output, and error for the shell.system(3)
does indeed use (possibly directly or indirectly via exec) a shell to do its work.exec(3)
and friends, however, do not use a shell, but rather execute the specified program image directly. You can see this simply by reading their respectiveman
pages.One difference is that with system(), you will see sugar like wildcards being expanded, whereas if you pass
*
as an argument to your program using exec(), your program will see the literal asterisk (and probably not know what to do).A shell can be implemented using exec() among other things. It gets its stdin and stdout from something called the TTY (teletype, or old-school terminal) or PTY (pseudo-terminal, as in modern systems). See
posix_openpt(2)
.