DOC PREVIEW
Rose-Hulman CSSE 332 - Shell Project

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Implementation of a simple shell, xssh(Section 1 version)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 simple case the shell: Reads a command Forks a child to execute the command Wait for child to complete before reading another command.2What is a shell? A real shell handles: Foreground process groups Background process groups Signals Process pipelines Redirection3UNIX/Linux Shells A shell is a command line interpreter Translates commands typed at a terminal (or in a file) Input is from the terminal (stdin) or file User’s /etc/passwd entry indicates the interactive shell 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 shell4Shell 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: www.ooblick.com/text/sh5Input 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 Command26Bash 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 _7Essence 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 information8What 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?9Built-in commands echo W1 W2 … 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 terminate10Built-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 command11Built-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 pathname12Commands 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-exec13Strategy 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 feature14Risky parts chdir P Use chdir() Not very risky after all quit S Need to cleanup and wait for background processes15Risky parts 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 processing16Risky parts Environment Details TBA17Observations 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.cwith a direct call to fork() and appropriate error handling No direct calls to fork() anywhere else18Basic Control Flowmain: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; }}19Basic Control Flowdo_nonbuiltin:if ((pid=Fork( )) error) ... Error ...if (pid == 0) { // child... execvp code ...exit(0);}if (foreground) Wait for child to


View Full Document

Rose-Hulman CSSE 332 - Shell Project

Download Shell Project
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Shell Project and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shell Project 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?