Previous Section  < Day Day Up >  Next Section

1.4. System Startup and the Login Shell

When you start up your system, the first process called is init. Each process has a process identification number associated with it, called the PID. Because init is the first process, its PID is 1. The init process initializes the system and then starts other processes to open terminal lines and set up the standard input (stdin), standard output (stdout), and standard error (stderr), which are all associated with the terminal. The standard input normally comes from the keyboard; the standard output and standard error go to the screen. At this point, a login prompt would appear on your terminal.

After you type your login name, you will be prompted for a password. The /bin/login program then verifies your identity by checking the first field in the passwd file. If your username is there, the next step is to run the password you typed through an encryption program to determine if it is indeed the correct password. Once your password is verified, the login program sets up an initial environment consisting of variables that define the working environment that will be passed on to the shell. The HOME, SHELL, USER, and LOGNAME variables are assigned values extracted from information in the passwd file. The HOME variable is assigned your home directory, and the SHELL variable is assigned the name of the login shell, which is the last entry in the passwd file. The USER and/or LOGNAME variables are assigned your login name. A search path variable is set so that commonly used utilities may be found in specified directories. When login has finished, it will execute the program found in the last entry of the passwd file. Normally, this program is a shell. If the last entry in the passwd file is /bin/csh, the C shell program is executed. If the last entry in the passwd file is /bin/bash or is null, the Bash shell starts up. If the last entry is /bin/ksh or /bin/pdksh, the Korn shell is executed. This shell is called the login shell.

After the shell starts up, it checks for any systemwide initialization files set up by the system administrator and then checks your home directory to see if there are any shell-specific initialization files there. If any of these files exist, they are executed. The initialization files are used to further customize the user environment. At this point you may start up a windowing environment such as CDE, OpenWindows, or Gnome. A virtual desktop will appear and depending on the configuration, a console and some pseudo terminals displaying a shell prompt. The shell is now waiting for your input.

1.4.1 Parsing the Command Line

When you type a command at the prompt, the shell reads a line of input and parses the command line, breaking the line into words, called tokens. Tokens are separated by spaces and tabs and the command line is terminated by a newline.[3] The shell then checks to see whether the first word is a built-in command or an executable program located somewhere on the disk. If it is built-in, the shell will execute the command internally. Otherwise, the shell will search the directories listed in the path variable to find out where the program resides. If the command is found, the shell will fork a new process and then execute the program. The shell will sleep (or wait) until the program finishes execution and then, if necessary, will report the status of the exiting program. A prompt will appear and the whole process will start again. The order of processing the command line is as follows:

[3] The process of breaking the line up into tokens is called lexical analysis.

  1. History substitution is performed (if applicable).

  2. Command line is broken up into tokens, or words.

  3. History is updated (if applicable).

  4. Quotes are processed.

  5. Alias substitution and functions are defined (if applicable).

  6. Redirection, background, and pipes are set up.

  7. Variable substitution ($user, $name, etc.) is performed.

  8. Command substitution (echo "Today is `date`") is performed.

  9. Filename substitution, called globbing (cat abc.??, rm *.c, etc.) is performed.

  10. Command is executed.

1.4.2 Types of Commands

When a command is executed, it is an alias, a function, a built-in command, or an executable program residing on disk. Aliases are abbreviations (nicknames) for existing commands and apply to the C, TC, Bash, and Korn shells. Functions apply to the Bourne (introduced with AT&T System V, Release 2.0), Bash, and Korn shells. They are groups of commands organized as separate routines. Aliases and functions are defined within the shell's memory. Built-in commands are internal routines in the shell, and executable programs reside on disk. The shell uses the path variable to locate the executable programs on disk and forks a child process before the command can be executed. This takes time. When the shell is ready to execute the command, it evaluates command types in the following order[4]:

[4] Numbers 3 and 4 are reversed for Bourne and Korn(88) shells. Number 3 does not apply for C and TC shells.

  1. Aliases

  2. Keywords

  3. Functions

  4. Built-in commands

  5. Executable programs

If, for example, the command is xyz the shell will check to see if xyz is an alias. If not, is it a built-in command or a function? If neither of those, it must be an executable program residing on the disk. In order to execute the command, the shell must then search the path to find out where the command is located in the directory structure in order to execute it. This process is illustrated in Figure 1.2.

Figure 1.2. The shell and command execution.


    Previous Section  < Day Day Up >  Next Section