1.3. System Startup and the Login Shell

When you start up your system, the first process is called 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 another process to open terminal lines, and sets 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 (Login:) appears at your console. After you type your login name, you are prompted for a password (you will be given up to 10 chances to enter the correct password). The /bin/login program then verifies your identity by checking the first field in the /etc/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 to the shell. The HOME, SHELL, USER, and LOGNAME variables are assigned values extracted from information in the /etc/passwd file. The HOME variable is assigned your home directory; the SHELL variable is assigned the name of the login shell, the last entry in the passwd file. The USER and/or LOGNAME variables are assigned your login name. A PATH variable to help the shell find commonly used utilities is located in specified directories. It is a colon separated list initially set to: /usr/local/bin:/bin:/usr/bin. When login has finished, it will execute the program found in the last entry of the /etc/passwd file. Normally, this program is a shell. If the last entry in the /etc/passwd file is /bin/tcsh or bin/csh, the TC shell program is executed. If the last entry in the /etc/passwd file is /bin/bash,/bin/sh, or is null, the Bourne Again shell starts up. If the last entry is /bin/pdksh, the Public Domain Korn shell is executed. This shell is called the login shell.

After the shell starts up, it checks for any systemwide initialization files 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. After the commands in those files have been executed, a shell prompt appears on your console, unless a windowing program, such as X Windows or Gnome is launched, at which point a number of xterm or visual shell windows will appear. When you see the shell prompt, either at the console or in an xterm or other desktop window, the shell program is now waiting for your input.

1.3.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 or 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 stored on 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 program 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 (if set).

  2. Command line is broken up into tokens (words).

  3. History is updated.

  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 for today is `date`) is performed.

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

  10. Program execution.

1.3.2. Types of Commands

When a command is executed, it is an alias, a function, a built-in command, or an executable program 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 (bash)

  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 command residing on the disk. The shell then must search the path for the command.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.67.70