A shell is a program that sits on the kernel and acts as an agent or interface between the users and the kernel and hence the hardware. It is similar to the command.com in the MS-DOS environment. A shell is a command interpreter or a processor. As soon as the system is booted successfully, the shell presents a command prompt (#, $, %) at which the user can type in any Unix Command. After accepting the command, the shell generates the readily executable simple command line by parsing it evaluating variables, performs command substitution and identifies the path. This command line is passed on to the kernel for execution.
There are different types of shells available. Some of them are discussed here:
- Bourne shell (sh): This is the most common shell available on UNIX system and the first major shell to be developed. This shell is widely used. It has been named after its author, Stephen Bourne at AT&T Bell labs. This shell is distributed as standard shell on almost all Unix system.
2. C shell (csh): Bill Joy developed this shell at UCB (University of California, Berkeley) in 1978 as a part of the BSD release. It is called as C shell because its syntax and usage is very similar to the C programming language. Unfortunately this shell is not available on all machines. Shell scripts written in the C shell are not compatible with the bourne shell. One of the major disadvantage of C shell over the bourne shell is its capability to execute processes in the background.
3. Korn shell (ksh): This shell is developed by David. Korn at AT&T Bell labs. It is built on Bourne Shell. It also incorporate certain features of the C shell. It is one of the widely used shells. It can run bourne shell scripts without any modifications
4. Bourne Again shell (bash): This shell was developed by B. Fox and C Ramey at free software foundation. Certain Linux operating system variants come with this shell as its default shell.
Features of Korn Shell are as follows:
- Interactive exiting of command line.
- Better function definitions.
- Extensive pattern matching
Several features adopted from C shell are :
- Command History.
- Job control & their moving from background to foreground.
- Aliases for abbreviated command names
- Field (~) sign used as short hand for name of home directory
Features of C shell are as follows:
- Input & Output redirection
- Wildcards & meta characters for filename abbreviation
- Set of shell variables to customize your environment
C shell adds the following new features:
- Integer Arithmetic
- A History mechanism allowing you to recall previous command
- Aliasing your abbreviation used frequently without a shell script
- More flexible form of command substitution
- Job Control, i.e, Ability to switch between several processes and control their progress
- A built in set of operators based on 'C for writing shell scripts.
There are number of options available which are used in different shells:
- Global (noglob): The global option controls the expansion of wildcard tokens in a command. For example, when the global option is off, the list file (ls) command uses wildcards to match the files in a directory. Thus, the following command lists all files that start with the 'file' followed by one character:
$ Is file?
On the other hand, when the global option is on, wildcards become text characters and are not expanded. In this case, only the file named file? would be listed.
2. Print Commands (verbose and xtrace): There are two print options, verbose and xtrace that are used to print commands before they are executed. The verbose option prints the command before it is executed. The xtrace option expands the command arguments before it prints the command.
3. Command-Line Editor (vi and emacs): To specify that the emacs editor is to be used in the Kom shell, we turn on the emacs option. To specify that the vi editor is to be used in the Korn shell, we turn on the vi option. These options are valid only in the Korn shell.
4. Ignore End of File (ignoreeof): Normally, if end of file (ctrl+d) is entered at the command line, the shell terminates. To disable this action, we can turn on the ignore end of file option, ignoreeof. With this option, end of file generates an error message rather than terminating the shell.
5. No Clobber Redirection (noclobber): When output or errors are directed to a file that already exists, the current file is deleted and replaced by a new file. To prevent this action, we set the noclobber option.
The operating system tracks processes through a five digit ID number known as the pid or process ID. Each process in the system has a unique pid. Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any one time, no two processes with the same pid exist in the system because it is the pid that UNIX uses to track each process. (i) Starting a Process: There are two ways: (a) Foreground Processes (b) Background Processes (a) Foreground Processes: By default every process that we start runs in the foreground. It gets its input from the keyboard and sends its output to the Screen. We can see this happen with the Is command. If we want to list all the files in my current directory, we can use the following command: $ls ch*.doc This would display all the files whose name start with ch and ends with doc: ch01-1.doc ch010.doc ch02.doc ch03-2.doc ch04-1.doc ch040.doc ch05.doc ch06-2.doc ch01-2. doc ch02-1.doc The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input (which it does not), it waits for it from the keyboard While a program is running in foreground and taking much time, we cannot run any other commands (start any other processes) because prompt would not be available until program finishes its processing and comes out. (b) Background Processes: A background process runs without being connected to our keyboard. If the background process requires any keyboard input, it waits. The advantage of running a process in the background is that we can run other commands; we do not have to wait until it completes to start another! The simplest way to start a background process is to add an ampersand ( &) at the end of the command. $ls ch*.doc & This would also display all the files whose name start with ch and ends with .doc: ch01-1.doc ch010.doc ch02.doc ch03-2.doc ch04-1.doc ch040.doc ch05.doc ch06-2.doc ch01-2.dọc ch02-1.doc Here if the ls command wants any input (which it does not), it goes into a stop state until we move it into the foreground and give it the data from the keyboard. That first line contains information about the background process - the job number and process ID. We need to know the job number to manipulate it between background and foreground. If enter is pressed, then we get [1] + Done Is ch*.doc & $ The first line tells we that the ls command background process finishes successfully. The second is a prompt for another command, (ii) Listing Running Processes: It is easy to see our own processes by running the ps (process status) command as follows: $ps PID TTY TIME CMD 18358 ttyp3 00:00:00 sh 18361 ttyp3 00:01:31 abiword TTY ttyp3 00:00:00 ps One of the most commonly used flags for ps is the-f (f for full) option, which provides more information as shown in the following example -- $ps -f UID PID PPID C STIME TTY TIME CMD amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh amrood 6892 3662 4 10:51:50 pts/6 0:00 ps-f Here is the description of all the fields displayed by ps -f command:
Column Description UID User ID that this process belongs to (the person running it). PID Process ID PPID Parent process ID (the ID of the process that started it). C CPU utilization of process. STIME Process start time. TTY Terminal type associated with the process TIME CPU time taken by the process. CMD The command that started this process. here are other options which can be used along with ps command : Option Description -a Shows information about all users -x Shows information about processes without terminals. -u Shows additional information like -f option. -e Display extended information,
(iii) Stopping Processes: Ending a process can be done in several different ways. Often, from a console based command, sending a CTRL + C keystroke the default interrupt character) will exit the command. This works when process is running in foreground mode. If a process is running in background mode the first we would need to get its Job ID using ps command and after that we can use kill command to kill the process as follows: $ps –f
UID PID PPID C STIME TTY TIME CMD amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh amrood 6892 3662 4 10:51:50 pts/6 0:00 ps-f $kill 6738 Terminated Here kill command would terminate first_one process. If a process ignores a regular kill command, you can use kill -9 followed by the process ID as follows - $kill -9 6738 Terminated (iv) Parent and Child Processes: Each unix process has two ID numbers assigned to it: Process ID (pid) and Parent process ID (ppid). Each user process in the system has a parent process. Most of the commands that we run have the shell as their parent. Check ps-f example where this command listed both process ID and parent process ID |
Customization process: UNIX allows us to customize the shells and the environment we use. When we customize the environment, we can extend it to include subshells and programs that we create. There are four elements to customizing the shell and the environment. Depending on how we establish them, they can be temporary or permanent. Temporary customization lasts only for the current session. When a new session is started, the original settings are reestablished.
- Temporary Customization: Temporary customization can be used to change the shell environment and configuration for the complete current session or for only part of a session. Normally we customize our environment for only a part of the session, such as when we are working on something special. For example, if we are writing a script, it is handy to see the expanded commands as they are executed. We would do this by turning on the verbose option. When we are through writing the script, we should turn off the verbose option. Any option changed during the session is automatically reset to its default when we log on the next time
2. Permanent Customization: Permanent customization is achieved through startup and shutdown files. Startup files are system files that are used to customize the environment when a shell begins. We can add customization commands and set customization variables by adding commands to the startup file. Shutdown files are executed at logout time. Just like the startup files, we can add commands to clean up the environment when we log out.
The korn shell, bash shell and c shell uses different types of startup and shutdown file which is described as below:
(a) Korn Shell: The Korn Shell uses the three profile files :
(i) System Profile File: There is one system-level profile file, which is stored in the /etc directory. Maintained by the system administrator, it contains general commands and variable settings that are applied to every user of the system at login time. The system profile file is generally quite large and contains many advanced commands. The system profile is a read only file; its permissions are set so that only the system administrator can change it.
(ii) Personal Profile File: The personal profile. - profile, contains commands that are used to customize the startup shell. It is an optional file that is run immediately after the system profile file. Although it is a user file, it is often created by the system administrator to customize a new user's shell. If we make changes to it, we should make a backup copy and make changes to it.
(iii) Environment File: In addition, the Korn shell has an environmental file that is run whenever a new shell is started. It contains environmental variables that are to be exported to subshells and programs that run under the shell. Because it is executed each time a new shell is started, it is recommended that it be kept short.
The environment file does not have a predefined name. We can give it any name we desire. It must be stored in the home directory or in a subdirectory below the home directory. The locate the environment file, Korn requires that its absolute or relative path name be stored in the predefined variable, ENV.
(b) Bash Shell: For the system profile file, the Bash shell uses the same file as the Korn shell (/etc/profile). However for the personal profile file, it uses one of three files,
First it looks for a Bash profile file (~/.bash_profile). If it does not find a profile file, it looks for a login file (~/.bash_login). If it doesn't find a login file, it looks for a generic profile file (~/.profile). Whichever file the Bash shell finds, if any, it executes and ignores the rest.
The Bash environmental file uses the same concept as the Korn shell, except that the filename is stored in the BASH ENV variable.
The Korn shell does not have a logout file, but the Bash shell does. When the shell terminates, it looks for the logout file (~/.bash_logout) and executes it.
(c) C Shell : The C shell uses both startup and shutdown files. It has two startup files, ~/.login and ~/.cshrc and one shutdown file ~/.logout.
(i) Login File: The C shell login file (~/.login) is the equivalent of the Korn and Bash user profile file. It contains commands and variables that are executed when the user logs in. It is not exported to other shells nor is it executed if the C shell is started from another shell as a subshell.
(ii) Environment File: The C shell equivalent of the environmental file is the ~/.cshrc file. It contains the environmental settings that are to be exported to subshells. As an environmental file, it is executed whenever a new subshell is invoked.
(iii) Logout File: The C shell logout file, ~/.logout is run when we log out of the C shell. It contains commands and programs that are to be run at logout time. For example, it is the ideal place to store a script that automatically backs up critical files.
(iv) Other C Shell Files: The C shell may have other system files that are executed at login and logout time. They are found in the /etc/ directory as /etc/csh.cshrc, /etc/csh.login and /etc/esh.logout
References:
- Sumitabha Das: UNIX – Concepts and Applications, 4th Edition, Tata McGraw Hill, 2006.
- Behrouz A. Forouzan and Richard F. Gilberg: UNIX and Shell Programming, Cengage Learning, 2005.
- M.G. Venkateshmurthy: UNIX & Shell Programming, Pearson Education, 2005.