CS 497C - Introduction to UNIX Lecture 20: - The Shell Chin-Chih Chang chang@cs.twsu.edu The Shell * The shell is the agency between the user and the UNIX system. * The shell is a command processor; it processes the instructions you issue to the machine. * The Bourne Shell named after its founder Steve Bourne is the earliest shell that came with UNIX system. * Use echo $SHELL to know your shell. The Shell as Command Processor * When you log on to a UNIX machine, you see a prompt. A UNIX command is running once you log in. This command is the shell. It withers away when you log out. * The shell accepts and interprets users requests. It examines and rebuilds the command lien and then leaves the execution work to the kernel. * The kernel handles the hardware on behalf of these commands and all processes. The Shell as Command Processor * The shell is generally sleeping. It wakes up when input is keyed in at the prompt. * Sleeping, waiting, and waking are the cycle of the shell. * The following activities are performed by the shell: - It issues the prompt and sleeps till you enter a command. The Shell as Command Processor - After a command is entered, the shell scans the command line for some special characters (metacharacters) for expansion. - It then passes the command to the kernel in a form which the kernel can understand. - After the job is complete, the prompt reappears and the shell returns to its sleeping role to start the next cycle. * You can use metacharacters to devise a generalized pattern or model that can often match a group of similar filenames. Pattern Matching - The Wild Cards * The * matches any number of characters including none. * Use ls *.lst to list all files with extension .lst. * ? matches a single character. * Use cp ???.c progs to copy files whose filename has 3 characters and the .c extension. Pattern Matching - The Wild Cards * [cut] matches a single character – either a c, u, or t. * [!cut] matches a single character that is not a, c, or t. * Use cat *.[!o] to display all files having one extension character except object files. * [x-z] matches a single character from x to z. * Use rm note[01][0-9] to remove files note00, note01, …., note18, note19. Pattern Matching - The Wild Cards * [!q-z] matches a single character that is not within q to z. * These metacharacters lose their meaning when placed in the wrong place. * The * doesn’t match all filenames beginning with a . (dot), or the / of a pathname. * To list all the hidden files having at least three characters after the dot, use ls .???* Escaping - The Backslash (\) * It’s generally accepted principle that filenames shouldn’t contain the shell metacharacters. * Image a file named chap* created with the > symbol: $ echo > chap* * If you use rm chap*, you will remove all files beginning with chap. Escaping - The Backslash (\) * The way to remove the file chap* is to use the backslash (\) as follows: $ rm chap\* * The use of the \ in removing any special character is called escaping or despecializing. * For example, to remove a file chap0[1-3], use: $ rm chap0\[1-3\] Escaping - The Backslash (\) * Apart from the wild cards, some characters are also considered special by the shell: | <> ' " * The new line character is also special to the shell. * When you enter a long chain of commands or a command with numerous arguments, you can split the command line by hitting [Enter] after the \. Quoting * When a command argument is enclosed in quotes, the meanings of all enclosed special characters are turned off: $ echo '*?[8-9]' * The argument above is said to be quoted. * The space is another character that has a special meaning to the shell. * To remove a file with the space in the filename, you can use quotes: $ rm 'good morning' Escaping and Quoting in echo * The \ is also used to emphasize a character. The \ combines with some character to represent an escape sequence. * These are escape sequence accepted by echo: - \t - A tab - \f - A formfeed (page skip) - \n - A newline Redirection * The UNIX commands are designed to accept a character stream without knowing its source and destination. * A stream is just a sequence of bytes that many commands see as input and output. * UNIX treats these streams as files, and a group of UNIX commands reads from and writes to these files. Redirection * The shell sets up three standard files (for input, output, and error), attaches them to a user’s terminal at the time of logging in, and close them when the user logs out. * The standard file for input is known as standard input and that for output is known as standard output. The error stream is known as standard error. Redirection * The shell has set some physical devices as defaults for them: - Standard input – The default source is the keyboard. - Standard output – The default source is the terminal. - Standard error – The default destination is the terminal. * These default sources can be redirected to come from or go to any disk or some other device by some special characters.