Download presentation
Presentation is loading. Please wait.
1
Implementation of a simple shell, xssh
Shell Project Implementation of a simple shell, xssh
2
What is a shell? A process that does command line interpretation
Reads a command from standard input (stdin) Executes command corresponding to input line In the simple case the shell: Reads a command Forks a child to execute the command Waits for child to complete before reading another command.
3
What is a shell? A real shell handles: Foreground process groups
Background process groups Signals Process pipelines Redirection
4
UNIX/Linux Shells A shell is a command line interpreter Some Shells
Translates commands typed at a terminal (or in a file) Input is from the terminal (stdin) or file Some Shells Bourne Shell (sh): What some use for shell scripts C-Shell, TC-Shell (csh, tcsh): Typical interactive shell Bash Shell (bash, “GNU Bourne-Again Shell”): Default Linux interactive shell
5
Shell Scripting Shell Script (Shell Program)
A file containing shell commands Used heavily in system administration Scripts in /etc/rc*.d/ are executed during system booting A Tutorial on Scripting:
6
Input and Output < File Use File as standard input (stdin)
Note: stdin is file descriptor (/dev/fd/0) > File Use File as standard output (stdout, /dev/fd/1) << Word Read from the current file until end of file or an input line containing Word alone >> File Append stdout to File Command1 | Command2 Create a pipeline Connect stdout of Command1 to stdin of Command2
7
Bash Parameter Expansion
${Parameters} indicates parameter (variable) expansion The result of expansion is the value of the parameter Braces are optional, but … X=foo echo $XX # output is null string echo ${X}X # output is fooX Parameters A Name: A sequence of letters, digits or underscores starting with a letter. Note: these parameters are called variables. A Number: $1 is the first shell or command line argument, $2 is the second, etc. (positional parameters). Special Parameters: # ? - $ ! 0 _
8
Essence of Bash Special Parameters
$* "$1 $2 ...“ "$1" "$2” ... $# Number of positional parameters $? Status of last executed foreground command $- Options supplied to shell on invocation or by set $$ Process id of this shell $! Process id of last background command $0 Name of shell or script $_ Last argument of previous command man bash for more information
9
What will our shell do? Execute built-in commands
Execute foreground processes Execute background processes Perform variable substitution (expansion) before executing command Perform redirection Ignore comments and blank lines Implement optional features?
10
Built-in commands echo W1 W2 … quit S wait P
W1, W2, ... are words (tokens separated by white space) Compress consecutive white spaces to a single space quit S Exit shell with exit status S, an integer Real shell allows bg processes to continue; xssh shouldn’t !!! wait P Wait for process P to terminate If P is omitted, wait for all background processes to terminate
11
Built-in commands (cont.)
set V Val Create shell variable V if it doesn’t exist Set the value of V to Val All occurrences of ${V} will be replaced by Val until V is changed or removed unset V Remove given variable export V Mark V for export. Details TBA. Special variables: $$ pid of current process $? status of most recent foreground command $! pid of most recent background command
12
Built-in commands (cont.)
chdir P (synonymous with cd P) Assume $HOME if P is omitted If P is a valid path for which the user has execute privileges, change PWD to P, a pathname
13
Commands that aren’t built-in
Built-in commands are executed without fork-exec If first word of command is not a built-in command And it is either An absolute pathname of an executable file, or An executable file in a directory listed in $PATH Execute with fork-exec
14
Strategy What are the requirements?
Are there assumptions? What is most critical? What are the risky parts of the project? What are the main data structures? i.e., Abstract data types (data and function members) What is the overall control flow? What system calls/library functions will be needed? Can you sketch the whole project on a sheet of paper? Assume simple implementations of each feature
15
Risky parts chdir P quit S Use chdir()
Not very risky after all quit S Need to cleanup and wait for background processes
16
Risky parts Non-builtin commands Variable substitution wait P
Need list/table of background processes Non-builtin commands fork-execvp-wait Variable substitution Should be separable code; i.e., inserted before command processing
17
Risky parts Environment Details TBA
18
Observations All system calls and error-prone library functions should be wrapped, e.g. Put “pid_t Fork(void);” in libxssh.h Put “pid_t Fork(void) {. . .}” in libxssh.c with a direct call to fork() and appropriate error handling No direct calls to fork() anywhere else
19
Basic Control Flow main: Initialize; while (get line not EOF) { Break line into words; if (builtin command) do_builtin; else do_nonbuiltin; } do_builtin: case command { // or directly call function ... chdir,echo,quit,wait,set ... if (incorrect #args) { Display msg; break; }
20
Basic Control Flow do_nonbuiltin:
if ((pid=Fork( )) error) ... Error ... if (pid == 0) { // child ... execvp code ... exit(0); } if (foreground) Wait for child to terminate;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.