MiniShell Project
OBJECT
- Acknownledge of process.
- Acknownledge of process piping
Func
-
echo
- Printing string in terminal
- with
-n
-
export
- It is not linux program, only work in shell.
- Export variable only work own process or child process, so cant connect & set variables in parent.
Pipe
Process has own dependency memories, so cant connect memory through processes. Pipe make connection with two process, possible communicating each processes.
#include <unistd.h>
int pipe(int fd[2]);
Pipe gets file descriptors(fd[2]
). fd[0]
is exit of pipe, fd[1]
is entrance of pipe
Must check pipe in shell program below.
- Child process get only stdout in parent process, so redirection is not duplicated
- pipe is one side communitate default, so one side should close.
Multi piping line
int piping(char *cmds, char *envp[])
{
int fd[2];
int fd_in = 0;
int pid;
while (*cmds)
{
pipe(fd);
pid = fork();
if (pid == 0)
{
dup2(fd_in, 0);
if (*(cmds + 1))
dup2(fd[1], 1);
close(fd[0]);
execve(*cmds, NULL, envp);
exit(0);
}
else
{
wait(NULL);
close(fd[1]);
//input piping sequencly
fd_in = fd[0];
cmds++;
}
}
}
Redirection
Redirection changes process output from stdin/stdout to other file descriptor. Must remember it is not arguments, only in/out process.
Must check redirection in shell program below.
- Redirection is preferred to Pipe. So redirection is worked after piping descriptor setted.
Quotes
Quote doesn't use for distinguishing string or number. '123'
, "123"
, 123
are same string. Quote is used for below.
- Make one argv with space char.
- Use enter in argv.
- Emphasize string.
Special Charactor
$
: Get variable.-
!
: history operand
We use \
, escape this contents;
No quote
When no quote, all charactor are escaped like below.
$ echo \a\b\c\d
$ adcd
$ echo \\ta ## \is work \t => tab
$ a
Double quotes(") / Single quotes(')
Special character is interpreted in Double quotes, but single is not, ever no escapce.