|
||||||
|
|
UNIX shellsA Unix shell is a program that interprets user-issued commands and passes them to the operating system kernel for execution. Unix provides three shells, the Bourne shell, the Korn shell, and the C shell. Unix utilities are available under each type of shell. Shell commands and capabilities differ, but each type of shell provides features such as parameter processing, variable definition, and loop structures. The Bourne shell (sh) is named for its author and is distributed with AT&T Unix system software. The Korn shell (ksh, also known as the K shell) is also named after its author; it is similar to the Bourne shell, but has many extended features. The C shell (csh) was developed at the University of California at Berkeley and has a syntax similar to that of the C programming language; it also has many extended features, but it is not compatible with the Bourne shell. All three shells have lengthy on-line manual pages that describe details that are not included here. For example, the manual pages for the Korn shell can be requested with % man ksh In this manual, the character % is shown as the interactive prompt from the C shell, and $ as the prompt from the Korn shell or Bourne shell. The C shell prompt is normally shown in examples, unless the topic is specific to another shell. When you log in to Unix, a shell process will start up for interactive input. You have a choice of working under any of the three command interpreters. Your login shell type is stored in the system password file. The default for new users is the Bourne shell, sh, but it can be changed with the chsh (change shell) command. chsh does not affect your current session, but takes effect the next time you log in. If you want subsequent logins to be under a C shell, Korn shell, or Bourne shell, the commands are, respectively, $ chsh loginname
/bin/csh where loginname is the user name you are logged in under. Regardless of which shell you are working under, you can invoke new shell processes with the command csh, ksh, or sh.
Recalling CommandsKorn Shell Command History The Korn shell allows the user to edit and reissue previous commands, using a special mode similar to a one-line vi editor. The terminal must be set up to run the editor, and you must also set the value of the EDITOR variable in .profile to /usr/bin/vi . The vi editor is described in Appendix A. At the Korn shell prompt you can press the ESCAPE key to enter command editing mode. The 'k' key moves you "up" through previous commands, and the 'j' key moves you back "down" to more recent commands. You can insert and change the text of the command with a subset of the vi commands. When you are finished editing the command, yyyyyyyyyyyyyexecutes it. C Shell Command History The C shell history mechanism keeps track of shell commands issued and allows you to list and reissue previous commands. To display the command history list, enter % history n The parameter n is optional and specifies the number of commands to be displayed. You can display your history list in reverse order, starting at the most recent command, by typing % history -r A command can be recalled by number with % !n where n is the event number (or use !-n for the nth comand from the most recent). To reissue the last command, type % !! You can also specify commands by name. For example, % !cf77 will issue the most recent command that began with cf77. You can modify a recent command before re-executing it. For a full explanation, see the History Substitution chapter of the man page for csh.
Tailoring the EnvironmentUnix shells allow users to tailor their working environments in many ways. Commands can be placed in scripts that are executed automatically at log in for purposes such as defining command aliases and environment variables and execution of certain programs. See Chapter 4 for more information about shell scripts. The Bourne Shell and Korn Shell .profile Files The general environment for the Bourne shell and Korn shell is established by the system administrator in the file /etc/profile, which is executed whenever a user logs in under these shells. A user's private profile can be placed in a file named .profile in his home directory. Both the Bourne and Korn shells allow the user to define shell functions (see examples below). The string $* indicates where the function parameters should be placed. The Korn shell also permits command aliases, but the Bourne shell does not. Comments can be preceded with the # character. Environment changes must be exported with the export command in order to affect subshells.
Sample .profile
File
Every user is provided a default .profile file, but is free to modify it to suit his or her preferences. However, users should be careful about modifying the profile, since unexpected things can occur if the wrong commands are inserted. Under the Bourne or Korn shell you can implement commands in .profile so that they affect the current environment with $ . .profile
C Shell .login and .cshrc Files The general environment for the C shell is established by the system administrator in the file /etc/cshrc, which is executed whenever a user logs in under the C shell. Users can modify their own environments by placing commands in scripts named .cshrc and .login in their home directories. The file .logout in the home directory can be created to store commands to be executed each time the user logs out. The file .cshrc is executed each time a new C shell process is created. It affects any child processes of the user's login process. The .cshrc file can contain variable definitions, aliases for commands, and other types of C shell commands. The file .login is executed only by the user's login shell process, not by subsequent C shell processes.
Sample .cshrc File # This
file is similar to the default .cshrc file on the
Every user is provided with default .cshrc and .login files, but is free to modify them. Users should be careful about modifying the files, since unexpected things can occur if the wrong commands are inserted. Under the C shell you can implement commands in .cshrc so that they affect your current environment with % source .cshrc
| |||||
| |||