1 / 22

Unix Programming Environment Part 3-1 Basic Commands and An Introduction to B-Shell

Unix Programming Environment Part 3-1 Basic Commands and An Introduction to B-Shell 2002/09/25 Draft Version 2002/10/01 Rev1.0. Agenda. 1. The Basics – getting started ( 1.1 ) 2. Overview of the system architecture - concepts 3. using TTY – Unix terminal

curry
Télécharger la présentation

Unix Programming Environment Part 3-1 Basic Commands and An Introduction to B-Shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unix Programming Environment Part 3-1 Basic Commands and An Introduction to B-Shell 2002/09/25 Draft Version 2002/10/01 Rev1.0

  2. Agenda • 1. The Basics – getting started ( 1.1 ) • 2. Overview of the system architecture - concepts • 3. using TTY – Unix terminal • 4. An introduction to B-Shell ( 1.4 ) • Overview • Filename Expansion • Standard I/O – redirect I/O • Pipe • Customizing our personal operating environment • 5. X-Windows • demo

  3. 1. The Basics – getting started( 1 ) • Reading the book( 1.1 ) • 1. logging in • 2. some basic commands • 3. interrupt the running programs • 4. logout • 5. mail & write • 6. online manual – man • Winhelp • man man • man –k “command” • xman, gnome-help-broswer, web-based manual

  4. 1. The Basics – getting started( 2 ) • Sections: • 1. user commands; (1M. administrator commands ) • 2. system calls • 3. library functions( standard C, networking, threads, etc ) • 4. File formats • 5. Standards, Environments and Macros • 6. Demos • 7. Device and Network Interfaces • 9. DDI and DKI

  5. 2. Overview of the system architecture

  6. 3. Using Unix terminals – TTY ( 1 ) • 1. Scenarios • H/W: host + serial ports(Hyerterm, minicom), host + NIC( telnet, rsh ), PC( CRT+ Adaptor + KB ) • X-based App, Screen-based App, line-based App • Job control & signals • 2. the internal structure: hierarchical model • General TTY data structure, • Line Disciplines, • TTY Drivers( pty, serial ) • 3. TTY’s Line Discipline • “echo”; • Mode: canonical, raw, cbreak • Special Control Characters • The “stty” command: Demo Linux & Solaris

  7. 3. Using Unix terminals – TTY ( 2 ) • 4. POSIX Programming Interfaces • See AUPE – Chapter 11, 19 • 5. other applications using terminal I/O • Printer, vi( ncurse ), PPP, PPPOE • 6. Further Readings: • <AUPE> - C11( POSIX API ), C19( PTY ) • < Linux Source > - tty.c/h, n_tty.c, serial.c • < Design of UNIX System >, Bach • < ncurse lib >

  8. 4.1 An Introductionof Unix Shells (1) • 1. What is a shell? • A shell is simply a macro processor that executes commands. A Unix shell is both a command interpreter and a programming language. • a command interpreter :providing the user interface to the rich set of Unix utilities. • a programming language: allowing these utilitites to be combined. Files containing commands can be created, and become commands themselves. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions. • Shells may be used interactively or non-interactively: they accept input typed from the keyboard or from a file. • A shell allows execution of Unix commands, both synchronously and asynchronously. • The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. • The redirection constructs permit fine-grained control of the input and output of those commands, and the shell allows control over the contents of their environment. • Unix shells also provide a small set of built-in commands (builtins) implementing functionality impossible (e.g., cd, break, continue, and exec), or inconvenient (history, getopts, kill, or pwd, for example) to obtain via separate utilities.

  9. 4.1 Overview of Unix Shells (2) • 2. Shell Operation • Reads its input from a file, from a string supplied as an argument to the `-c' invocation option, or from the user's terminal. • Breaks the input into words and operators, obeying the quoting rules. These tokens are separated by metacharacters. Alias expansion is performed by this step. • Parses the tokens into simple and compound commands. • Performs the various shell expansions, breaking the expanded tokens into lists of filenames (“Filename Expansion”) and commands and arguments. • Performs any necessary redirections and removes the redirection operators and their operands from the argument list. • Executes the command. • Optionally waits for the command to complete and collects its exit status.

  10. 4.1 Overview of Unix Shells (3) • 3. Variants of Unix Shells • The Bourne Shell( /bin/sh ): • Developed by Steve Bourne at Bell Labs. Its control flow constructs are reminiscent of Algol 68. • C-Shell: • Done by Bill Joy at Berkeley. It was build on the 5th Edition shell( not the Bourne shell). Its control flow looks more like the C language, and supports additional features – job control, a history mechanism, and command-line editing. • The Korn Shell( /bin/ksh ): • A successor to the Bourne shell and being provided in SVR4. • Developed by David Kern at Bell Labs. It is upward-compatible from the Bourne shell and included those features that mode the C shell popular. • Bash: • the GNU Shell, used in GNU O.S. like Linux, FreeBSD and Cygwin. • To get details about Bash, please read the supplementary reading.

  11. 4.2 B-Shell: Filename Expansion (1) • 1. Pattern Matching • The special pattern characters have the following meanings: • * : Matches any string, including the null string. • ? : Matches any single character. • [...] : Matches any one of the enclosed characters. • The special pattern characters MUST be quoted if they are to be matched literally. • 2. Filename Expansion • After word splitting, sh scans each word for the characters `*', `?', `(', and `['. • If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern. • If no matching file names are found, the word is left unchanged. • Options in Bash: left unchanged, or removed • The character `.' at the start of a filename or immediately following a slash must be matched explicitly. When matching a file name, the slash character must always be matched explicitly.

  12. 4.2 B-Shell: Filename Expansion (2) • Examples from the textbook( p20, 21 ) • Pathname • ‘*’, ‘?’ can not be used to create files and directories • Quoting: • Escape Character • Example: $ ls \? • Single Quotes • Example: $ ls ‘?’

  13. 4.5 B-Shell: Process ( 1 ) • 1. Introduction • A “process” is a virtual machine which provides the runtime environment for executable programs. • Process Context: ( proc struct, u-area ), PID • The process-local file descriptor table: FS • Memory Mapping table: memory • The hierarchical structure: the parent process( ppid ) and its context • We will further understand “Process” using O.S. designers’ eyes in Part-6 for these basic concepts are critical for our programming.

  14. 4.5 B-Shell: Process ( 2 ) • 2. examples from the textbook: • date; who • wc ch* > wc.out & • pr ch* | lpr & • wait, kill pid, nice • ps • nohup • at time, batch • 3. exit status • Every command returns an exit status (sometimes referred to as a return status ). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually may be interpreted as an error code.The exit status must be a decimal number in the 0 - 255 range. • $? gives the exit status of the last command executed in the function. • See waitpid() to get more information on how to encode the return status.

  15. 4.5 B-Shell: Process ( 3 ) • 4. Job Control • Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. • A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and a shell supporting job control. • The system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as SIGINT. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a SIGTTIN (SIGTTOU) signal by the terminal driver, which, unless caught, suspends the process. • “^Z”, jobs, fg, bg • We will model these concepts in Part-6, and discuss programming interfaces.

  16. 4.5 B-Shell: Process ( 4 )

  17. 4.3 B-Shell: I/O Redirection (1) • 1. Introduction • For “processes”, there are always three default "files" open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen), which are inherited from their parent processes. • Before a command is executed, its process’s input and output may be redirected using a special notation interpreted by the shell. • Redirection may also be used to open and close files for the current shell execution environment.

  18. 4.3 B-Shell: I/O Redirection (2) • 2. Redirecting Input: [n]<word • 3. Redirecting Output: [n]>[|]word • 4. Appending Redirected Output: [n]>>word • 5. Redirecting Standard Output and Standard Error • &>word, >&word, semantically equivalent to “>word 2>&1” • 6. Here Documents • 7. Duplicating File Descriptors • 8. Opening File Descriptors for Reading and Writing • We discuss the features(6, 7, 8) in the Chapter 3.

  19. 4.4 B-Shell: Pipelines • 1. Introduction • “Pipelines” should be the ancestor of the middleware tech. • The format: [time [-p]] [!] command1 [| command2 ...] • If the pipeline is not executed asynchronously, the shell waits for all commands in the pipeline to complete. • Each command in a pipeline is executed in its own subshell. The exit status of a pipeline is the exit status of the last command in the pipeline. If the reserved word `!' precedes the pipeline, the exit status is the logical negation of the exit status of the last command. • How does the UNIX kernel support “pipelines”? • How does the shell implement “pipelines”? • who | grep mary | wc -l

  20. 4.6 B-Shell: Customizing Environment(1) • 1. Shell Startup files • the POSIX standard for startup files: • `/etc/profile‘: systemwide defaults • `~/.profile‘: user-specific default settings • For Bash: • /etc/profile: systemwide defaults, mostly setting the environment (all Bourne-type shells, not just Bash) • /etc/bashrc: systemwide functions and aliases for Bash • $HOME/.bash_profile: user-specific Bash environmental default settings, found in each user's home directory (the local counterpart to /etc/profile) • $HOME/.bashrc: user-specific Bash init file, found in each user's home directory (the local counterpart to /etc/bashrc). Only interactive shells and user scripts read this file. For example, Bash is being run by the remote shell daemon, usually rshd.

  21. 4.6 B-Shell: Customizing Environment(2) • 2. An Introduction to Variables • carefully distinguish between the name of a variable and its value. • If variable1 is the name of a variable, then $variable1 is a reference to its value, the data item it contains. • $variable actually a simplified alternate form of ${variable}. • tmp=/tmp/ps; ps a > ${tmp}a # ps a > /tmp/psa • Unlike many other programming languages, b-shell does not segregate its variables by "type". Essentially, b-shell variables are only strings-valued. • But, depending on context, Bash permits integer operations and comparisons on variables. • An uninitialized variable has a "null" value - no assigned value at all (not zero!). • the assignment operator (no space before & after) • Note that = can be either an assignment or a test operator, depending on context.

  22. 4.6 B-Shell: Customizing Environment(3) • local variables: variables visible only within a code block or function • environmental variables: • variables that affect the behavior of the shell, the user interface and the child processes • “export” • Special variables: • The command “set”: display all defined variables • HOME, PATH, PS1, PS2, IFS, TERM

More Related