Lesson 8-Specifying Instructions to the Shell - PowerPoint PPT Presentation

About This Presentation
Title:

Lesson 8-Specifying Instructions to the Shell

Description:

Lesson 8-Specifying Instructions to the Shell Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. – PowerPoint PPT presentation

Number of Views:611
Avg rating:3.0/5.0
Slides: 50
Provided by: Hur84
Category:

less

Transcript and Presenter's Notes

Title: Lesson 8-Specifying Instructions to the Shell


1
Lesson 8-Specifying Instructions to the Shell
2
Overview
  • An overview of shell.
  • Execution of commands in a shell.
  • Shell command-line expansion.
  • Customizing the functioning of the shell.
  • Employing advanced user features.

3
An Overview of Shell
  • A shell interprets and executes the syntax of the
    command-lines in a specific way.
  • The kernel is the core program of UNIX/Linux,
    which schedules processes, allocates memory, and
    handles input/output and other peripherals.
  • User cannot directly communicate with the shell.

4
An Overview of Shell
  • The shell interacts with the kernel to execute a
    request.
  • The shell is the middleman between the user and
    the kernel.
  • A shell translates a users requests into kernel
    calls.
  • The login shell is started when a user logs in
    and exits when the user logs out.

5
An Overview of Shell
  • A shell is the interface between the user,
    utilities, the file system, and the kernel.
  • The shells primary function is to read the
    command-line, examine its component, and
    interpret it according to its rules.
  • The shell performs the given task and returns the
    prompt for further requests.

6
Execution of Commands in a Shell
  • Interacting with the shell.
  • Communicating with the shell.
  • Identifying utilities for output redirection.
  • Identifying utilities in pipelines.
  • Starting processes to run utilities.
  • Redirecting input and output.

7
Execution of Commands in a Shell
  • Passing arguments to processes.
  • Identifying tokens on the command-line.
  • The exit code status after a utility execution.
  • Using the model to interpret command-lines.
  • Changing the behavior of a command execution.

8
Interacting with the Shell
  • Entering a command from the keyboard is the basic
    way of communicating with the shell.
  • For each utility requested by the user, the shell
    starts a new child process to execute the code of
    that utility.
  • The child process inherits the environment
    variables like pid, user, etc.
  • The ps utility can be used for obtaining the
    process identification numbers.

9
Communicating with the Shell
  • The shell proceeds through a series of specific
    steps after a user issues commands.
  • The complete command-line is first interpreted by
    the shell.
  • The shell interprets the ENTER key as the
    completion of a command.
  • The shell interprets \ as an instruction not to
    interpret the special meaning of the single
    character that immediately follows it.

10
Communicating with the Shell
  • The commands entered at the shell prompt usually
    include several words or tokens.
  • The shell interprets some tokens as utilities and
    others as filenames.
  • The command line interprets the gt, , and lt
    as special characters that control the input and
    output of a file.

11
Communicating with the Shell
  • The shell uses white space to identify the words
    or tokens of a command-line.
  • The sign is recognized by the shell as the
    start of a new variable.

12
Identifying Utilities for Output Redirection
  • The shell interprets the first word in the
    command-line of the shell as a utility.
  • In a C shell, when assigning value to variables,
    it should not include any spaces around the
    sign.
  • The value of a set variable can be displayed by
    adding a sign ahead of the variable name.
  • The shell interprets the token following the pipe
    as a utility and the token following the
    redirection operator as a file.

13
Identifying Utilities in Pipelines
  • A pipeline is a set of one or more utilities that
    handle data independently.
  • The semicolon can be used to indicate the end
    of one pipeline.
  • A shell can run one pipeline after another on a
    single command-line by separating them with
    semicolons.
  • The first token after the semicolon begins a new
    pipeline, and hence must be a utility.

14
Identifying Utilities in Pipelines
  • The logical AND () operator can instruct the
    pipeline to run the next utility based on the
    success or failure of the preceding pipeline.
  • The command-line is successful only if both are
    executed.
  • A token following the operator is interpreted
    as a utility.
  • The logical OR () operator executes only one of
    the two utilities in the command-line.

15
Identifying Utilities in Pipelines
  • The shell can easily interpret a variable in all
    tokens since a sign precedes them.
  • The x option tells the shell to explain how it
    interprets the command-line before executing it.

16
Starting Processes to Run Utilities
  • The shell is an active process and runs in the
    foreground.
  • The resources allocated to a running process are
    called process space or process image.
  • The shell makes an exact copy of the process
    space, including environment variables, when
    running a utility.

17
Starting Processes to Run Utilities
  • A new child process space is an exact copy of the
    shell.
  • The child process inherits the input, output,
    error destination, and variable information from
    the parent.

18
Redirecting Input and Output
  • The shell interprets the gt as an instruction to
    redirect the output from the screen to a file.
  • When redirecting output to a file, a new file is
    created, depending on the shell and the noclobber
    variable set.
  • Existing files can be protected by setting the
    noclobber variable.

19
Redirecting Input and Output
  • The csh shell can be instructed to overwrite an
    existing file by placing an exclamation point (!)
    before the redirection symbol (gt).
  • In bash and ksh shells, the pipe following the
    redirect is an instruction to overwrite even if
    noclobber is set off.
  • The i option, when used with the mv and cp
    utilities, protects files from accidental
    removal.
  • A utility, uses the keyboard as default input.

20
Redirecting Input and Output
  • The input, the output and the error files all are
    connected to the default output, the monitor.
  • An error message is displayed on the screen if a
    command is not able to execute.
  • An error message can be redirected to a file by
    using the 2gt and a filename to the command-line.

21
Redirecting Input and Output
  • The bash, ksh, and sh uses gt or 1gt to
    redirect output to a file.
  • The standard error and output can both be
    redirected to the same file using the gt and
    specifying the filename in the command line.

22
Passing Arguments to Processes
  • The ls utility interprets an argument as a file
    if it is not preceded by a minus sign.
  • Any tokens left over on the command-line when the
    shell has completed the interpretation are passed
    as an argument to the associated utility.
  • The shell also interprets command options as
    arguments.
  • Two or more option flags can be specified on the
    command-line as one argument.

23
Identifying Tokens on the Command-Line
  • The wild card character instructs the shell
    to include all the filenames.
  • 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.
  • The /bin directory usually contains all the
    executables.

24
Identifying Tokens on the Command-Line
  • The strings utility ignores all machine code and
    outputs only the strings of ASCII characters that
    it finds.
  • The shell does not search the path to locate the
    utility if the absolute path of the utility is
    specified.

25
The Exit Code Status After a Utility Execution
  • The shell interprets the variable ? as the exit
    code of the last process.
  • Exit codes other than zero are error codes.
  • Every time a process completes its execution and
    exits, it informs its parent about the status of
    the exit code.

26
Using the Model to Interpret Command-Lines
  • 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.
  • A utility that requires a filename as an
    argument will start reading from the input if the
    filename is not specified.
  • A dash (-) argument instructs the sort utility to
    read from the input.

27
Changing the Behavior of a Command Execution
  • When the shell executes a child process in the
    foreground, it waits for a child process to
    complete execution and then displays the prompt.
  • A command consisting of utilities, arguments, and
    redirection terminated by ENTER is called a job.
  • A job can be placed in the background by
    appending it with the sign.

28
Changing the Behavior of a Command Execution
  • The ps command can be used for listing all
    current processes.
  • A current process in the foreground can be
    suspended by pressing CTRL-Z.
  • The csh, tcsh, bash, and ksh shells allow a user
    to suspend a job midstream and return to it
    later.
  • The fg command allows a job to be brought back to
    the foreground.

29
Shell Command-Line Expansion
  • Using shell characters to expand filenames.
  • Creating and using local variables.
  • Passing environment variables to child processes.

30
Using Shell Characters to Expand Filenames
  • Some characters are interpreted by the shell as
    wildcard characters, while others can be used for
    specifying a range of characters.
  • The filename expansion of the filename-matching
    feature allows the selection of many filenames
    while entering only one name with special
    characters embedded.
  • The and ? are interpreted by the shell as
    special characters.

31
Using Shell Characters to Expand Filenames
  • The asterisk () character can be used for
    matching any number of characters, while the
    question mark (?) is used only for matching a
    single character.
  • Shell variable names and values are stored in the
    memory and are hence available regardless of the
    directory location.

32
Using Shell Characters to Expand Filenames
  • The shell also allows a range of letters or
    characters to be specified with the help of
    square brackets.
  • 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.
  • The curly braces match existing filenames if each
    match is specified in the braces, but does not
    expand ranges.

33
Creating and Using Local Variables
  • Local and environmental are the two different
    kinds of variables identified by the shell.
  • The set or env command lists the variables
    that are set in the shells memory.
  • In a csh or tcsh shell, the set command is used
    for declaring a variable and assigning a value to
    it.

34
Creating and Using Local Variables
  • In ksh, bash, or sh shell, a variable is directly
    defined and assigned a value without the set
    command.
  • The shell interprets the character as an
    instruction to locate in the shells memory a
    variable that has the name of the character
    string that follows the .
  • The variable must be enclosed in single quotes if
    it includes any spaces.

35
Passing Environment Variables to Child Processes
  • There are two types of variables - the local
    variable and the global variable.
  • The local variables of a shell are not passed to
    a child process.
  • In a C shell, the setenv command is used for
    setting an environmental variable.
  • The set, env, or printenv commands can be used
    for listing the environmental variables.

36
Passing Environment Variables to Child Processes
  • The unset command can be used for removing a
    local variable.
  • An environmental variable can be removed with the
    help of the unsetenv command.
  • The export command is used for making a local
    variable available to a child process.

37
Passing Environment Variables to Child Processes
  • An environmental variable modified by the child
    process is not reflected in the parents
    environmental variables.
  • The shell also allows a variable to be created
    and exported at the same time.
  • The variables set in a child process are lost
    once the child process exits.
  • The child shell takes the memory of the variable
    when it exits.

38
Customizing the Functioning of the Shell
  • Using and modifying the search path.
  • Creating personalized shell prompts.

39
Using and Modifying the Search Path
  • The path or PATH variable is searched when a
    user requests for a utility.
  • The path is a local variable and is usually
    assigned a value in the startup script.
  • The C shell also maintains a PATH environmental
    variable, which also holds the path, and passes
    it to child processes.
  • In the C shell, two variables are intertwined
    where change in one is automatically reflected in
    another.

40
Using and Modifying the Search Path
  • A single dot is used for denoting the current
    directory and can be set in the path variable.
  • 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.
  • An empty field using two colons () can
    explicitly request the current directory,
    anywhere in the path, using a dot.

41
Creating Personalized Shell Prompts
  • In a tcsh or csh shell, the prompt variable is
    used for modifying the display prompt.
  • The man and info pages describe the
    collection of variables that can be used in
    constructing a prompt.
  • The ksh and the bash shells use the value in the
    variable PS1 as its prompt.

42
Employing Advanced User Features
  • Completing filenames
  • The variable filec in a tcsh shell, when set in
    the environment, instructs the shell to search
    for matching filenames.
  • When a shell cannot distinguish between two
    existing files, it either displays all matching
    files or simply flashes or produces beeps.
  • Filename completion can also be used for files,
    directories, and executables.

43
Employing Advanced User Features
  • Completing filenames (continued)
  • 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.
  • The set o vi or set o posix commands can be
    used for turning on the file-completion feature
    if it is not working.
  • Many C shells include filename completion, but
    use the ESC key to trigger completion of
    filenames.

44
Employing Advanced User Features
  • Evaluating shell variables
  • The bash and ksh shells also provide built-in
    variables that are useful in interacting with the
    shell.
  • The SECOND shell variable can be used for
    determining the number of seconds since the shell
    was started.
  • In a bash shell, the PROMPT_COMMAND variable
    allows a user to execute any command just before
    it displays the prompt.
  • A dot file is a run-control file for a specific
    utility or shell.

45
Employing Advanced User Features
  • Customizing shell startup files
  • The csh shell can be customized with the help of
    the .cshrc file in the /etc directory since it is
    always read at startup.
  • The bash shell reads the file .bashrc whenever it
    starts.

46
Employing Advanced User Features
  • Customizing shell startup files (continued)
  • A system setup to start a ksh file reads the
    .kshrc file at startup.
  • The .kshrc file is not read if the ENV
    environmental variable is not set.
  • The ksh shell is programmed to read at startup
    whatever file is the value of the ENV variable.

47
Summary
  • All shells accomplish the primary task of
    interpreting the commands issued by a user.
  • A shell process executes code that resides in a
    file in a system directory.
  • A child process started by the shell for each
    utility execution inherits the input, output, and
    error destinations, as well as environmental
    variables.

48
Summary
  • Redirecting input and output from the default
    destination to files and other utilities is one
    of the functions of a shell.
  • A command-line inside back quotes is interpreted
    and executed as a complete command line.
  • Local variables are not passed to child
    processes, while environmental variables are
    passed to child processes.

49
Summary
  • The most recent shells include file completion,
    which allows us to type part of a file,
    directory, or utility name.
  • A user can place instructions in the startup
    files to tailor how the shell functions.
Write a Comment
User Comments (0)
About PowerShow.com