intro unix/linux 08


Published on

specifying instructions to the shell

Published in: Business, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

intro unix/linux 08

  1. 1. Lesson 8-Specifying Instructions to the Shell
  2. 2. Overview <ul><li>An overview of shell. </li></ul><ul><li>Execution of commands in a shell. </li></ul><ul><li>Shell command-line expansion. </li></ul><ul><li>Customizing the functioning of the shell. </li></ul><ul><li>Employing advanced user features. </li></ul>
  3. 3. An Overview of Shell <ul><li>A shell interprets and executes the syntax of the command-lines in a specific way. </li></ul><ul><li>The kernel is the core program of UNIX/Linux, which schedules processes, allocates memory, and handles input/output and other peripherals. </li></ul><ul><li>User cannot directly communicate with the shell. </li></ul>
  4. 4. <ul><li>The shell interacts with the kernel to execute a request. </li></ul><ul><li>The shell is the middleman between the user and the kernel. </li></ul><ul><li>A shell translates a user’s requests into kernel calls. </li></ul><ul><li>The login shell is started when a user logs in and exits when the user logs out. </li></ul>An Overview of Shell
  5. 5. An Overview of Shell <ul><li>A shell is the interface between the user, utilities, the file system, and the kernel. </li></ul><ul><li>The shell’s primary function is to read the command-line, examine its component, and interpret it according to its rules. </li></ul><ul><li>The shell performs the given task and returns the prompt for further requests. </li></ul>
  6. 6. Execution of Commands in a Shell <ul><li>Interacting with the shell. </li></ul><ul><li>Communicating with the shell. </li></ul><ul><li>Identifying utilities for output redirection. </li></ul><ul><li>Identifying utilities in pipelines. </li></ul><ul><li>Starting processes to run utilities. </li></ul><ul><li>Redirecting input and output. </li></ul>
  7. 7. Execution of Commands in a Shell <ul><li>Passing arguments to processes. </li></ul><ul><li>Identifying tokens on the command-line. </li></ul><ul><li>The exit code status after a utility execution. </li></ul><ul><li>Using the model to interpret command-lines. </li></ul><ul><li>Changing the behavior of a command execution. </li></ul>
  8. 8. Interacting with the Shell <ul><li>Entering a command from the keyboard is the basic way of communicating with the shell. </li></ul><ul><li>For each utility requested by the user, the shell starts a new child process to execute the code of that utility. </li></ul><ul><li>The child process inherits the environment variables like pid, user, etc. </li></ul><ul><li>The “ps” utility can be used for obtaining the process identification numbers. </li></ul>
  9. 9. Communicating with the Shell <ul><li>The shell proceeds through a series of specific steps after a user issues commands. </li></ul><ul><li>The complete command-line is first interpreted by the shell. </li></ul><ul><li>The shell interprets the ENTER key as the completion of a command. </li></ul><ul><li>The shell interprets “” as an instruction not to interpret the special meaning of the single character that immediately follows it. </li></ul>
  10. 10. Communicating with the Shell <ul><li>The commands entered at the shell prompt usually include several words or tokens. </li></ul><ul><li>The shell interprets some tokens as utilities and others as filenames. </li></ul><ul><li>The command line interprets the “>”, “|”, and “<“ as special characters that control the input and output of a file. </li></ul>
  11. 11. <ul><li>The shell uses white space to identify the words or tokens of a command-line. </li></ul><ul><li>The “$” sign is recognized by the shell as the start of a new variable. </li></ul>Communicating with the Shell
  12. 12. Identifying Utilities for Output Redirection <ul><li>The shell interprets the first word in the command-line of the shell as a utility. </li></ul><ul><li>In a C shell, when assigning value to variables, it should not include any spaces around the “=“ sign. </li></ul><ul><li>The value of a set variable can be displayed by adding a $ sign ahead of the variable name. </li></ul><ul><li>The shell interprets the token following the pipe as a utility and the token following the redirection operator as a file. </li></ul>
  13. 13. Identifying Utilities in Pipelines <ul><li>A pipeline is a set of one or more utilities that handle data independently. </li></ul><ul><li>The “semicolon” can be used to indicate the end of one pipeline. </li></ul><ul><li>A shell can run one pipeline after another on a single command-line by separating them with semicolons. </li></ul><ul><li>The first token after the semicolon begins a new pipeline, and hence must be a utility. </li></ul>
  14. 14. Identifying Utilities in Pipelines <ul><li>The logical AND (&&) operator can instruct the pipeline to run the next utility based on the success or failure of the preceding pipeline. </li></ul><ul><li>The command-line is successful only if both are executed. </li></ul><ul><li>A token following the && operator is interpreted as a utility. </li></ul><ul><li>The logical OR (||) operator executes only one of the two utilities in the command-line. </li></ul>
  15. 15. Identifying Utilities in Pipelines <ul><li>The shell can easily interpret a variable in all tokens since a $ sign precedes them. </li></ul><ul><li>The “–x” option tells the shell to explain how it interprets the command-line before executing it. </li></ul>
  16. 16. Starting Processes to Run Utilities <ul><li>The shell is an active process and runs in the foreground. </li></ul><ul><li>The resources allocated to a running process are called process space or process image. </li></ul><ul><li>The shell makes an exact copy of the process space, including environment variables, when running a utility. </li></ul>
  17. 17. Starting Processes to Run Utilities <ul><li>A new child process space is an exact copy of the shell. </li></ul><ul><li>The child process inherits the input, output, error destination, and variable information from the parent. </li></ul>
  18. 18. Redirecting Input and Output <ul><li>The shell interprets the > as an instruction to redirect the output from the screen to a file. </li></ul><ul><li>When redirecting output to a file, a new file is created, depending on the shell and the noclobber variable set. </li></ul><ul><li>Existing files can be protected by setting the noclobber variable. </li></ul>
  19. 19. Redirecting Input and Output <ul><li>The csh shell can be instructed to overwrite an existing file by placing an exclamation point (!) before the redirection symbol (>). </li></ul><ul><li>In bash and ksh shells, the pipe following the redirect is an instruction to overwrite even if noclobber is set off. </li></ul><ul><li>The “–i” option, when used with the mv and cp utilities, protects files from accidental removal. </li></ul><ul><li>A utility, uses the keyboard as default input. </li></ul>
  20. 20. Redirecting Input and Output <ul><li>The input, the output and the error files all are connected to the default output, the monitor. </li></ul><ul><li>An error message is displayed on the screen if a command is not able to execute. </li></ul><ul><li>An error message can be redirected to a file by using the “2>” and a filename to the command-line. </li></ul>
  21. 21. <ul><li>The bash, ksh, and sh uses “>” or “1>” to redirect output to a file. </li></ul><ul><li>The standard error and output can both be redirected to the same file using the “>&” and specifying the filename in the command line. </li></ul>Redirecting Input and Output
  22. 22. Passing Arguments to Processes <ul><li>The “ls” utility interprets an argument as a file if it is not preceded by a minus sign. </li></ul><ul><li>Any tokens left over on the command-line when the shell has completed the interpretation are passed as an argument to the associated utility. </li></ul><ul><li>The shell also interprets command options as arguments. </li></ul><ul><li>Two or more option flags can be specified on the command-line as one argument. </li></ul>
  23. 23. Identifying Tokens on the Command-Line <ul><li>The “*” wild card character instructs the shell to include all the filenames. </li></ul><ul><li>The C shell variable path and the family variable PATH contain a list of directories that the respective shells search to locate the code for each requested utility. </li></ul><ul><li>The /bin directory usually contains all the executables. </li></ul>
  24. 24. Identifying Tokens on the Command-Line <ul><li>The strings utility ignores all machine code and outputs only the strings of ASCII characters that it finds. </li></ul><ul><li>The shell does not search the path to locate the utility if the absolute path of the utility is specified. </li></ul>
  25. 25. The Exit Code Status After a Utility Execution <ul><li>The shell interprets the variable “?” as the exit code of the last process. </li></ul><ul><li>Exit codes other than zero are error codes. </li></ul><ul><li>Every time a process completes its execution and exits, it informs its parent about the status of the exit code. </li></ul>
  26. 26. Using the Model to Interpret Command-Lines <ul><li>When redirecting output to a file and if the noclobber variable is set to off, no error message is displayed when the file is being overwritten. </li></ul><ul><li>A utility that requires a filename as an argument will start reading from the input if the filename is not specified. </li></ul><ul><li>A dash (-) argument instructs the sort utility to read from the input. </li></ul>
  27. 27. Changing the Behavior of a Command Execution <ul><li>When the shell executes a child process in the foreground, it waits for a child process to complete execution and then displays the prompt. </li></ul><ul><li>A command consisting of utilities, arguments, and redirection terminated by ENTER is called a job. </li></ul><ul><li>A job can be placed in the background by appending it with the & sign. </li></ul>
  28. 28. Changing the Behavior of a Command Execution <ul><li>The ps command can be used for listing all current processes. </li></ul><ul><li>A current process in the foreground can be suspended by pressing CTRL-Z. </li></ul><ul><li>The csh, tcsh, bash, and ksh shells allow a user to suspend a job midstream and return to it later. </li></ul><ul><li>The fg command allows a job to be brought back to the foreground. </li></ul>
  29. 29. Shell Command-Line Expansion <ul><li>Using shell characters to expand filenames. </li></ul><ul><li>Creating and using local variables. </li></ul><ul><li>Passing environment variables to child processes. </li></ul>
  30. 30. Using Shell Characters to Expand Filenames <ul><li>Some characters are interpreted by the shell as wildcard characters, while others can be used for specifying a range of characters. </li></ul><ul><li>The filename expansion of the filename-matching feature allows the selection of many filenames while entering only one name with special characters embedded. </li></ul><ul><li>The * and ? are interpreted by the shell as special characters. </li></ul>
  31. 31. Using Shell Characters to Expand Filenames <ul><li>The asterisk (*) character can be used for matching any number of characters, while the question mark (?) is used only for matching a single character. </li></ul><ul><li>Shell variable names and values are stored in the memory and are hence available regardless of the directory location. </li></ul>
  32. 32. Using Shell Characters to Expand Filenames <ul><li>The shell also allows a range of letters or characters to be specified with the help of square brackets. </li></ul><ul><li>The curly brace characters, “{“ and “}”, are also used by the bash shell and modern ksh shells for matching and creating multiple filenames from one pattern. </li></ul><ul><li>The curly braces match existing filenames if each match is specified in the braces, but does not expand ranges. </li></ul>
  33. 33. Creating and Using Local Variables <ul><li>Local and environmental are the two different kinds of variables identified by the shell. </li></ul><ul><li>The “set” or “env” command lists the variables that are set in the shell’s memory. </li></ul><ul><li>In a csh or tcsh shell, the set command is used for declaring a variable and assigning a value to it. </li></ul>
  34. 34. Creating and Using Local Variables <ul><li>In ksh, bash, or sh shell, a variable is directly defined and assigned a value without the set command. </li></ul><ul><li>The shell interprets the $ character as an instruction to locate in the shell’s memory a variable that has the name of the character string that follows the $. </li></ul><ul><li>The variable must be enclosed in single quotes if it includes any spaces. </li></ul>
  35. 35. Passing Environment Variables to Child Processes <ul><li>There are two types of variables - the local variable and the global variable. </li></ul><ul><li>The local variables of a shell are not passed to a child process. </li></ul><ul><li>In a C shell, the “setenv” command is used for setting an environmental variable. </li></ul><ul><li>The set, env, or “printenv” commands can be used for listing the environmental variables. </li></ul>
  36. 36. Passing Environment Variables to Child Processes <ul><li>The “unset” command can be used for removing a local variable. </li></ul><ul><li>An environmental variable can be removed with the help of the “unsetenv” command. </li></ul><ul><li>The “export” command is used for making a local variable available to a child process. </li></ul>
  37. 37. Passing Environment Variables to Child Processes <ul><li>An environmental variable modified by the child process is not reflected in the parent’s environmental variables. </li></ul><ul><li>The shell also allows a variable to be created and exported at the same time. </li></ul><ul><li>The variables set in a child process are lost once the child process exits. </li></ul><ul><li>The child shell takes the memory of the variable when it exits. </li></ul>
  38. 38. Customizing the Functioning of the Shell <ul><li>Using and modifying the search path. </li></ul><ul><li>Creating personalized shell prompts. </li></ul>
  39. 39. Using and Modifying the Search Path <ul><li>The “path” or “PATH” variable is searched when a user requests for a utility. </li></ul><ul><li>The path is a local variable and is usually assigned a value in the startup script. </li></ul><ul><li>The C shell also maintains a PATH environmental variable, which also holds the path, and passes it to child processes. </li></ul><ul><li>In the C shell, two variables are intertwined where change in one is automatically reflected in another. </li></ul>
  40. 40. Using and Modifying the Search Path <ul><li>A single dot is used for denoting the current directory and can be set in the path variable. </li></ul><ul><li>A colon (:) at the beginning or the end of a path string is interpreted by the sh family of shells as an instruction to search the current directory. </li></ul><ul><li>An empty field using two colons (::) can explicitly request the current directory, anywhere in the path, using a dot. </li></ul>
  41. 41. Creating Personalized Shell Prompts <ul><li>In a tcsh or csh shell, the prompt variable is used for modifying the display prompt. </li></ul><ul><li>The “man” and “info” pages describe the collection of variables that can be used in constructing a prompt. </li></ul><ul><li>The ksh and the bash shells use the value in the variable PS1 as its prompt. </li></ul>
  42. 42. Employing Advanced User Features <ul><li>Completing filenames: </li></ul><ul><ul><li>The variable filec in a tcsh shell, when set in the environment, instructs the shell to search for matching filenames. </li></ul></ul><ul><ul><li>When a shell cannot distinguish between two existing files, it either displays all matching files or simply flashes or produces beeps. </li></ul></ul><ul><ul><li>Filename completion can also be used for files, directories, and executables. </li></ul></ul>
  43. 43. Employing Advanced User Features <ul><li>Completing filenames (continued): </li></ul><ul><ul><li>The filename-completion variable can be set in the Korn shell by executing either the “set –o vi” or the “set –o vi-tabcomplete” command. </li></ul></ul><ul><ul><li>The “set –o vi” or “set +o posix” commands can be used for turning on the file-completion feature if it is not working. </li></ul></ul><ul><ul><li>Many C shells include filename completion, but use the ESC key to trigger completion of filenames. </li></ul></ul>
  44. 44. Employing Advanced User Features <ul><li>Evaluating shell variables: </li></ul><ul><ul><li>The bash and ksh shells also provide built-in variables that are useful in interacting with the shell. </li></ul></ul><ul><ul><li>The SECOND shell variable can be used for determining the number of seconds since the shell was started. </li></ul></ul><ul><ul><li>In a bash shell, the PROMPT_COMMAND variable allows a user to execute any command just before it displays the prompt. </li></ul></ul><ul><ul><li>A dot file is a run-control file for a specific utility or shell. </li></ul></ul>
  45. 45. Employing Advanced User Features <ul><li>Customizing shell startup files: </li></ul><ul><ul><li>The csh shell can be customized with the help of the .cshrc file in the /etc directory since it is always read at startup. </li></ul></ul><ul><ul><li>The bash shell reads the file .bashrc whenever it starts. </li></ul></ul>
  46. 46. Employing Advanced User Features <ul><li>Customizing shell startup files (continued): </li></ul><ul><ul><li>A system setup to start a ksh file reads the .kshrc file at startup. </li></ul></ul><ul><ul><li>The .kshrc file is not read if the ENV environmental variable is not set. </li></ul></ul><ul><ul><li>The ksh shell is programmed to read at startup whatever file is the value of the ENV variable. </li></ul></ul>
  47. 47. Summary <ul><li>All shells accomplish the primary task of interpreting the commands issued by a user. </li></ul><ul><li>A shell process executes code that resides in a file in a system directory. </li></ul><ul><li>A child process started by the shell for each utility execution inherits the input, output, and error destinations, as well as environmental variables. </li></ul>
  48. 48. Summary <ul><li>Redirecting input and output from the default destination to files and other utilities is one of the functions of a shell. </li></ul><ul><li>A command-line inside back quotes is interpreted and executed as a complete command line. </li></ul><ul><li>Local variables are not passed to child processes, while environmental variables are passed to child processes. </li></ul>
  49. 49. <ul><li>The most recent shells include file completion, which allows us to type part of a file, directory, or utility name. </li></ul><ul><li>A user can place instructions in the startup files to tailor how the shell functions. </li></ul>Summary